From d71697a6f8fa4ebcc5e78bee05786b4f547f0716 Mon Sep 17 00:00:00 2001 From: Dima Snisarenko Date: Wed, 19 Apr 2017 15:41:51 +0200 Subject: [PATCH] fix: Get rid of 'safe' filter in layout --- README.md | 2 +- demo/src/layout.html | 2 +- src/assemblers/templates.js | 14 +++++++++----- src/assemblers/views/layout.njk | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index eb8badf..c7382a1 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ Create you own layout file by copying minimum HTML required: - {{ content | safe }} + {{ content }} ``` diff --git a/demo/src/layout.html b/demo/src/layout.html index 68f2076..2573f23 100644 --- a/demo/src/layout.html +++ b/demo/src/layout.html @@ -3,7 +3,7 @@
- {{ content | safe }} + {{ content }}
\ No newline at end of file diff --git a/src/assemblers/templates.js b/src/assemblers/templates.js index a17c2f3..fb46e53 100644 --- a/src/assemblers/templates.js +++ b/src/assemblers/templates.js @@ -7,6 +7,8 @@ import nunjucks from 'nunjucks'; import hljs from 'highlight.js'; import cheerio from 'cheerio'; +let nunjucksEnv; + function refineTree(item, rootDir) { //Remove firsts numbers and extensions var prettyName = item.name.replace(/^\d+\./, '').split('.').shift().replace('-', ' '); @@ -40,9 +42,11 @@ function compileTree(item, outputDir, tree, options = {}) { current: item }); - const navigationHtml = nunjucks.render('navigation.njk', params); - const contentHtml = nunjucks.render('content.njk', params); - const layoutHtml = nunjucks.render(options.layout, {content: contentHtml}); + const navigationHtml = nunjucksEnv.render('navigation.njk', params); + const contentHtml = nunjucksEnv.render('content.njk', params); + nunjucksEnv.opts.autoescape = false; + const layoutHtml = nunjucksEnv.render(options.layout, {content: contentHtml}); + nunjucksEnv.opts.autoescape = true; const $ = cheerio.load(layoutHtml, { decodeEntities: false @@ -50,7 +54,7 @@ function compileTree(item, outputDir, tree, options = {}) { const head = $('head'); const body = $('body'); - head.prepend(nunjucks.render('css.njk', params)); + head.prepend(nunjucksEnv.render('css.njk', params)); if (head.find('title').length === 0) { head.prepend($('').text(options.title)); @@ -102,7 +106,7 @@ export default function(inputDir, outputDir, options = {}) { } }); - (new nunjucks.configure([path.join(__dirname, 'views'), ''])) + nunjucksEnv = (new nunjucks.configure([path.join(__dirname, 'views'), ''])) .addFilter('template', path => fs.readFileSync(path).toString()) .addFilter('highlight', code => hljs.highlight('htmlbars', code, true, false).value); diff --git a/src/assemblers/views/layout.njk b/src/assemblers/views/layout.njk index 5fc91bb..bf7db39 100644 --- a/src/assemblers/views/layout.njk +++ b/src/assemblers/views/layout.njk @@ -2,6 +2,6 @@ - {{ content | safe }} + {{ content }} \ No newline at end of file