Skip to content

Commit

Permalink
Add test for special tags
Browse files Browse the repository at this point in the history
  • Loading branch information
crphang committed Mar 9, 2020
1 parent ea36316 commit f4ca1c2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/lib/markbind/nunjuckUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ function preEscapeRawTags(pageData) {
}

module.exports = {
renderEscaped(nunjucks, pageData, variableMap = {}) {
renderEscaped(nunjucks, pageData, variableMap = {}, options = {}) {
const escapedPage = preEscapeRawTags(pageData);
return nunjucks.renderString(escapedPage, variableMap);
return nunjucks.renderString(escapedPage, variableMap, options);
},

removeNunjucksEscapes(pageData) {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/markbind/src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ class Parser {
{
...pageVariables,
...userDefinedVariables,
},
{
path: actualFilePath,
});
this._extractInnerVariables(fileContent, context, config);
Expand Down Expand Up @@ -628,8 +630,7 @@ class Parser {
// and let the hostBaseUrl value be injected later.
hostBaseUrl: '{{hostBaseUrl}}',
baseUrl: `{{hostBaseUrl}}/${newBaseUrl}`,
path: filePath,
});
}, { path: filePath });
node.children = cheerio.parseHTML(rendered, true);
cheerio.prototype.options.xmlMode = true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const cheerio = module.parent.require('cheerio');

const ESCAPE_REGEX = new RegExp('{% *raw *%}(.*?){% *endraw *%}', 'gs');

function removeEscapeTags(_, p1) {
return p1;
}

/*
Simple test plugin that whitelists <testtag> as a special tag.
If encountered, it wraps the text node inside with some indication text as to
Expand All @@ -17,11 +23,20 @@ function preRender(content) {
$(testElement).text(wrappedText);
});

const escapedNunjucks = $('mustache');
escapedNunjucks.each((index, element) => {
const unwrappedText = $(element).text();
const unescapedText = unwrappedText.replace(ESCAPE_REGEX, removeEscapeTags);
const transformedText = unescapedText.replace(/{/g, '!success').replace(/}/g, 'success!');
$(element).text(transformedText);
});


return $.html();
}


module.exports = {
preRender,
getSpecialTags: () => ['testtag'],
getSpecialTags: () => ['testtag', 'mustache'],
};
8 changes: 8 additions & 0 deletions test/functional/test_site_special_tags/expected/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ <h2 id="so-far-as-to-comply-with-the-commonmark-spec">So far as to comply with t
<p>There should be text between this and the next <code>&lt;hr&gt;</code> tag, since it is a special tag. All text should appear in the browser window as a single line, save for the comment which the browser still interprets. (but will be in the
expected output)
</p>
<mustache>


!success!success This should be enclosed in success string success!success!

!success This should also be enclosed in success strings success!success!success!

</mustache>
<testtag>!success

<these>
Expand Down
9 changes: 9 additions & 0 deletions test/functional/test_site_special_tags/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ There should be text between this and the next `<hr>` tag, since it is a special
All text should appear in the browser window as a single line,
save for the comment which the browser still interprets. (but will be in the expected output)

<mustache>
{%raw%}

{{ This should be enclosed in success string }}

{ This should also be enclosed in success strings }}}
{%endraw%}
</mustache>

<testtag>
<these>
some text
Expand Down

0 comments on commit f4ca1c2

Please sign in to comment.