Skip to content

Commit

Permalink
escapes single element content
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbmeyer committed Jun 4, 2018
1 parent 2344173 commit 911d9f8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build/build_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ describe("documentjs/lib/generators/html/build", function(){
var renderer = results[0];
var result = renderer(docObject);

assert.equal(result, "<html><p>This is <a href=\"something.html\" title=\"something\"><something/></a></p>\n\n</html>");
assert.equal(result, "<html><p>This is <a href=\"something.html\" title=\"something\">&lt;something/&gt;</a></p>\n\n</html>");
});
});
});
24 changes: 19 additions & 5 deletions build/make_default_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ var striptags = require('striptags');
var DocMapInfo = require("../doc-map-info");
var unescapeHTML = require("unescape-html");


function escapeOnlySingleElements(text) {
//This could be an alternate way of detecting if markdown escaped
//var html = stmd_to_html("<p>"+text+"</p>\n");
//if(html === "<p>"+text+"</p>\n") { }
if(/^<\w+\/?>$/.test(text)) {
return escape(text);
} else {
return text;
}
}

// Helper helpers

var sortChildren = function(child1, child2) {
Expand Down Expand Up @@ -200,11 +212,11 @@ module.exports = function(docMap, config, getCurrent, Handlebars){
* @body
*
* ## Use
*
*
* ```
* {{{generatedWarning}}}
* ```
*
*
* MUST use triple-braces to escape HTML so it is hidden in a comment.
*
* Creates a warning that looks like this:
Expand All @@ -230,7 +242,7 @@ module.exports = function(docMap, config, getCurrent, Handlebars){
},
/**
* @function bit-docs-generate-html/build/make_default_helpers.makeTitle makeTitle
*
*
* Given the [bit-docs/types/docObject] context, returns a "pretty"
* name that is used in the sidebar and the page header.
*/
Expand Down Expand Up @@ -288,13 +300,15 @@ module.exports = function(docMap, config, getCurrent, Handlebars){
//if there is anything in the hashParts, append it to the end of the url
href = urlTo(name) + (hashParts.length >= 1 ? ("#" + hashParts.join("#")) : "");

return '<a href="' + href + '" title="' + stripMarkdown(description) + '">' + linkText + '</a>';


return '<a href="' + href + '" title="' + stripMarkdown(description) + '">' + escapeOnlySingleElements( linkText ) + '</a>';
}

if (httpRegExp.test(name)) {
linkText = parts && parts[2] ? parts[2] : name;
href = name;
return '<a href="' + href + '" title="' + escape(linkText) + '">' + linkText + '</a>';
return '<a href="' + href + '" title="' + escape(linkText) + '">' + escapeOnlySingleElements( linkText ) + '</a>';
}

return match;
Expand Down
21 changes: 21 additions & 0 deletions build/make_default_helpers_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"homepage": "https://github.com/bit-docs/bit-docs-generate-html#readme",
"dependencies": {
"can-string": "0.0.5",
"enpeem": "^2.1.0",
"escape-html": "^1.0.3",
"fs-extra": "0.30.0",
Expand Down

0 comments on commit 911d9f8

Please sign in to comment.