-
Notifications
You must be signed in to change notification settings - Fork 133
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/lib/markbind/src/lib/markdown-it-shared/markdown-it-escape-special-tags.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
|
||
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 = [ | ||
ang-zeyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[ 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 ] | ||
]; | ||
|
||
ang-zeyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function escape_special_tags(state, startLine, endLine, silent) { | ||
ang-zeyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; } | ||
ang-zeyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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; } | ||
ang-zeyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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++) { | ||
ang-zeyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; | ||
} | ||
} | ||
} | ||
|
||
ang-zeyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
state.line = nextLine; | ||
|
||
token = state.push('html_block', '', 0); | ||
token.map = [ startLine, nextLine ]; | ||
ang-zeyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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' 😅