Skip to content

Commit

Permalink
fix: Get rid of 'safe' filter in layout
Browse files Browse the repository at this point in the history
  • Loading branch information
sneas committed Apr 19, 2017
1 parent f913101 commit d71697a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Create you own layout file by copying minimum HTML required:
<html>
<head></head>
<body>
{{ content | safe }}
{{ content }}
</body>
</html>
```
Expand Down
2 changes: 1 addition & 1 deletion demo/src/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head></head>
<body>
<div class="container">
{{ content | safe }}
{{ content }}
</div>
</body>
</html>
14 changes: 9 additions & 5 deletions src/assemblers/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('-', ' ');
Expand Down Expand Up @@ -40,17 +42,19 @@ 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
});
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($('<title></title>').text(options.title));
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/assemblers/views/layout.njk
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<html>
<head></head>
<body>
{{ content | safe }}
{{ content }}
</body>
</html>

0 comments on commit d71697a

Please sign in to comment.