Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement an api to ignore text in certain tags #1047

Merged
merged 1 commit into from
Mar 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/userGuide/usingPlugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,26 @@ This allows files specified by the `src` attributes of `<puml>` tags to be watch
}
```

#### Special tags

By default, content in html tags are parsed as html and markdown.

However, you might want to create a plugin that has certain special tags containing conflicting syntax
you do not wish to be parsed as html or markdown.

You can implement the `getSpecialTags` method to blacklist the content in these special tags from parsing,
removing such potential conflicts.

- `getSpecialTags(pluginContext)`: Called during initial site generation to blacklist special tags.
- `pluginContext`: User provided parameters for the plugin. This can be specified in the `site.json`.
- Should return an array of string tag names to be blacklisted, with each tag name being at least 2 characters long.

<box type="important">
Note however, that variable interpolation syntax <code>{<span>{</span> variable_name <span>}</span>}</code> will act as per normal.
Meaning, the user would still be able to use variables in your special tags!
</box>


### Advanced: Default plugins

MarkBind has a set of default plugins that it uses to carry out some of its features. These are enabled by default for every project and should be left alone.
Expand Down
12 changes: 6 additions & 6 deletions src/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ class Page {
+ `${pageNavHeadingHTML}\n`
+ '</nav>\n'
+ '</div>\n'
+ '</nav>\n', { indent_size: 2 });
+ '</nav>\n', Page.htmlBeautifyOptions);
}
}

Expand Down Expand Up @@ -756,10 +756,10 @@ class Page {
if (pageSection.length === 0) {
return;
}
this.pageSectionsHtml[section] = htmlBeautify($.html(section), { indent_size: 2 })
this.pageSectionsHtml[section] = htmlBeautify($.html(section), Page.htmlBeautifyOptions)
.trim();
pageSection.remove();
this.content = htmlBeautify($.html(), { indent_size: 2 });
this.content = htmlBeautify($.html(), Page.htmlBeautifyOptions);
}

collectAllPageSections() {
Expand Down Expand Up @@ -804,7 +804,7 @@ class Page {
.then(result => markbinder.processDynamicResources(this.sourcePath, result))
.then(result => MarkBind.unwrapIncludeSrc(result))
.then((result) => {
this.content = htmlBeautify(result, { indent_size: 2 });
this.content = htmlBeautify(result, Page.htmlBeautifyOptions);

const newBaseUrl = Page.calculateNewBaseUrl(this.sourcePath, this.rootPath, this.baseUrlMap);
const baseUrl = newBaseUrl ? `${this.baseUrl}/${newBaseUrl}` : this.baseUrl;
Expand All @@ -823,7 +823,7 @@ class Page {

return fs.outputFileAsync(this.resultPath, htmlBeautify(
this.template.render(this.prepareTemplateData()),
{ indent_size: 2 },
Page.htmlBeautifyOptions,
));
})
.then(() => {
Expand Down Expand Up @@ -1080,7 +1080,7 @@ class Page {
baseUrl,
hostBaseUrl,
});
return fs.outputFileAsync(resultPath, htmlBeautify(content, { indent_size: 2 }));
return fs.outputFileAsync(resultPath, htmlBeautify(content, Page.htmlBeautifyOptions));
})
.then(() => {
// Recursion call to resolve nested dependency
Expand Down
33 changes: 33 additions & 0 deletions src/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const Promise = require('bluebird');
const ProgressBar = require('progress');
const walkSync = require('walk-sync');
const MarkBind = require('./lib/markbind/src/parser');
const injectHtmlParser2SpecialTags = require('./lib/markbind/src/patches/htmlparser2');
const injectMarkdownItSpecialTags = require(
'./lib/markbind/src/lib/markdown-it-shared/markdown-it-escape-special-tags');

const _ = {};
_.difference = require('lodash/difference');
Expand Down Expand Up @@ -525,6 +528,7 @@ class Site {
.then(() => this.collectBaseUrl())
.then(() => this.collectUserDefinedVariablesMap())
.then(() => this.collectPlugins())
.then(() => this.collectPluginSpecialTags())
.then(() => this.buildAssets())
.then(() => this.buildSourceFiles())
.then(() => this.copyMarkBindAsset())
Expand Down Expand Up @@ -736,6 +740,35 @@ class Site {
.forEach(plugin => this.loadPlugin(plugin, true));
}

/**
* Collects the special tags of the site's plugins, and injects them into the parsers.
*/
collectPluginSpecialTags() {
const tagsToIgnore = new Set();

Object.values(this.plugins).forEach((plugin) => {
if (!plugin.getSpecialTags) {
return;
}

plugin.getSpecialTags(plugin.pluginsContext)
.forEach((tagName) => {
if (!tagName) {
return;
}

tagsToIgnore.add(tagName.toLowerCase());
});
});

injectHtmlParser2SpecialTags(tagsToIgnore);
injectMarkdownItSpecialTags(tagsToIgnore);
Page.htmlBeautifyOptions = {
indent_size: 2,
content_unformatted: ['pre', ...tagsToIgnore],
};
}

/**
* Renders all pages specified in site configuration file to the output folder
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const markdownIt = require('../markdown-it');

/*
Custom patch for the api to escape content in certain special tags
Adapted from the default markdown-it html_block rule and replaces it.
Copy link
Contributor Author

@ang-zeyu ang-zeyu Mar 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, should have really highlighted this line more.
The specific file is \node_modules\markdown-it\lib\rules_block\html_block

75% of this file ( all of the comments ) is essentially the same as it.
I left all the lines from the original file with the exact formatting so its easier to see what’s patched.
The changes here are essentially just using the collected special tags to form a new regex to replace the first one in the original rule.

The htmlparser2 patch has a lot of changes though, 'thankfully' 😅

*/

function escape_plugin(md, tagsToIgnore) {
const block_names = require('markdown-it/lib/common/html_blocks');
const HTML_OPEN_CLOSE_TAG_RE = require('markdown-it/lib/common/html_re').HTML_OPEN_CLOSE_TAG_RE;

const specialTagsRegex = Array.from(tagsToIgnore)
.concat(['script|pre|style'])
.join('|');
const startingSpecialTagRegex = new RegExp(`^<(${specialTagsRegex})(?=(\\s|>|$))`, 'i');
const endingSpecialTagRegex = new RegExp(`<\\/(${specialTagsRegex})>`, 'i');

const HTML_SEQUENCES = [
[ startingSpecialTagRegex, endingSpecialTagRegex, true ],
[ /^<!--/, /-->/, true ],
[ /^<\?/, /\?>/, true ],
[ /^<![A-Z]/, />/, true ],
[ /^<!\[CDATA\[/, /\]\]>/, true ],
[ new RegExp('^</?(' + block_names.join('|') + ')(?=(\\s|/?>|$))', 'i'), /^$/, true ],
[ new RegExp(HTML_OPEN_CLOSE_TAG_RE.source + '\\s*$'), /^$/, false ]
];


function escape_special_tags(state, startLine, endLine, silent) {
let i, nextLine, token, lineText,
pos = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine];

// if it's indented more than 3 spaces, it should be a code block
if (state.sCount[startLine] - state.blkIndent >= 4) { return false; }

if (!state.md.options.html) { return false; }

if (state.src.charCodeAt(pos) !== 0x3C/* < */) { return false; }

lineText = state.src.slice(pos, max);

for (i = 0; i < HTML_SEQUENCES.length; i++) {
if (HTML_SEQUENCES[i][0].test(lineText)) { break; }
}

if (i === HTML_SEQUENCES.length) { return false; }

if (silent) {
// true if this sequence can be a terminator, false otherwise
return HTML_SEQUENCES[i][2];
}

nextLine = startLine + 1;

// If we are here - we detected HTML block.
// Let's roll down till block end.
if (!HTML_SEQUENCES[i][1].test(lineText)) {
for (; nextLine < endLine; nextLine++) {
if (state.sCount[nextLine] < state.blkIndent) { break; }

pos = state.bMarks[nextLine] + state.tShift[nextLine];
max = state.eMarks[nextLine];
lineText = state.src.slice(pos, max);

if (HTML_SEQUENCES[i][1].test(lineText)) {
if (lineText.length !== 0) { nextLine++; }
break;
}
}
}

state.line = nextLine;

token = state.push('html_block', '', 0);
token.map = [ startLine, nextLine ];
token.content = state.getLines(startLine, nextLine, state.blkIndent, true);

return true;
}

md.block.ruler.at('html_block', escape_special_tags, {
alt: [ 'paragraph', 'reference', 'blockquote' ]
});
}

/**
* Sets up the plugin with the provided tag names to ignore.
* Replaces any previously injected tags.
*/
function injectTags(tagsToIgnore) {
markdownIt.use(escape_plugin, tagsToIgnore);
}

module.exports = injectTags;
Loading