const fileHandler = require('./file-handler')

const templateDir = './src/templates/'

const templates = {
    base: {
        html: fileHandler.readFile(templateDir + 'base.html'),
        replacements: ['title', 'nav', 'content', 'headers'],
        defaults: {title: '', nav: '', content: '', headers: ''}
    },
    nav: {
        html: fileHandler.readFile(templateDir + 'nav.html'),
        replacements: [],
        defaults: {}
    },
    blog: {
        html: fileHandler.readFile(templateDir + 'blog.html'),
        replacements: ['article', 'previous', 'next'],
        defaults: {article: '', previous: '', next: ''}
    },
    blogList: {
        html: fileHandler.readFile(templateDir + 'blog-list.html'),
        replacements: ['list'],
        defaults: {list: ''}
    },
    blogListItem: {
        html: fileHandler.readFile(templateDir + 'blog-list-item.html'),
        replacements: ['file', 'title', 'date', 'snip'],
        defaults: {}
    },
    blogRssBase: {
        html: fileHandler.readFile(templateDir + 'blog-rss-base.xml'),
        replacements: ['list'],
        defaults: {}
    },
    blogRssItem: {
        html: fileHandler.readFile(templateDir + 'blog-rss-item.xml'),
        replacements: ['title', 'link', 'snip', 'date'],
        defaults: {}
    }
}

const applyTemplate = (template, data) => {
    let html = template.html
    template.replacements.forEach(x => html = html.replace(new RegExp(`:${x}:`, 'g'), (data[x] || template.defaults[x])))
    return html
}

const applyBaseTemplate = (data) => {
    let title = data.title ? `Allan Stewart - ${data.title}` : 'Allan Stewart'
    return applyTemplate(templates.base, {nav: templates.nav.html, title, content: data.content, headers: data.headers})
}

const applyBlogTemplate = (data) => {
    return applyBaseTemplate({content: applyTemplate(templates.blog, data), title: data.title, headers: data.headers})
}

const applyBlogListTemplate = (data) => {
    let list = data.list.map(x => applyTemplate(templates.blogListItem, x)).join('\n')
    return applyBaseTemplate({content: applyTemplate(templates.blogList, {list}), title: 'Blog'})
}

const applyBlogRssTemplate = (data) => {
    let list = data.list.map(x => applyTemplate(templates.blogRssItem, x)).join('\n')
    return applyTemplate(templates.blogRssBase, {list})
}

module.exports = {
    applyBaseTemplate,
    applyBlogTemplate,
    applyBlogListTemplate,
    applyBlogRssTemplate
}
@@ -1409,32 +1409,25 @@
 l = 
-html.
 replace
-(new RegExp(
+All(html, 
 %60:$%7B
@@ -1435,14 +1435,8 @@
 %7D:%60,
- 'g'),
  (da
@@ -1483,24 +1483,192 @@
 urn html%0A%7D%0A%0A
+const replaceAll = (original, match, replacement) =%3E %7B%0A    if (match.length %3C 1) %7B%0A        return original%0A    %7D%0A%0A    return original.split(match).join(replacement)%0A%7D%0A%0A
 const applyB

Fix replacement to no longer match $&