forked from StackExchange/Stacks-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(html-comment): fix html comments from being visible in the rich e…
…ditor fixes StackExchange#195
- Loading branch information
Showing
7 changed files
with
184 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import MarkdownIt from "markdown-it/lib"; | ||
import StateBlock from "markdown-it/lib/rules_block/state_block"; | ||
|
||
const HTML_COMMENT_OPEN_TAG = /<!--/; | ||
const HTML_COMMENT_CLOSE_TAG = /-->/; | ||
|
||
function getLineText(state: StateBlock, line: number): string { | ||
const pos = state.bMarks[line] + state.tShift[line]; | ||
const max = state.eMarks[line]; | ||
return state.src.slice(pos, max).trim(); | ||
} | ||
|
||
function html_comment( | ||
state: StateBlock, | ||
startLine: number, | ||
endLine: number, | ||
silent: boolean | ||
) { | ||
if (!state.md.options.html) { | ||
return false; | ||
} | ||
|
||
let lineText = getLineText(state, startLine); | ||
|
||
// check if the open tag "<!--" is the first element in the line | ||
if (!HTML_COMMENT_OPEN_TAG.test(lineText.slice(0, 4))) { | ||
return false; | ||
} | ||
|
||
let nextLine = startLine + 1; | ||
while (nextLine < endLine) { | ||
if (HTML_COMMENT_CLOSE_TAG.test(lineText)) { | ||
break; | ||
} | ||
lineText = getLineText(state, nextLine); | ||
nextLine++; | ||
} | ||
|
||
// check if the first close tag "-->" occurence is the last element in the line | ||
if (HTML_COMMENT_CLOSE_TAG.exec(lineText).index + 3 !== lineText.length) { | ||
return false; | ||
} | ||
|
||
if (silent) { | ||
return true; | ||
} | ||
|
||
state.line = nextLine; | ||
|
||
const token = state.push("html_comment", "", 0); | ||
token.map = [startLine, nextLine]; | ||
token.content = state.getLines(startLine, nextLine, state.blkIndent, true); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Parses out HTML comments blocks | ||
* (HTML comments inlined with other text/elements are not parsed by this plugin) | ||
* @param md | ||
*/ | ||
export function htmlComment(md: MarkdownIt): void { | ||
md.block.ruler.before("html_block", "html_comment", html_comment); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import MarkdownIt from "markdown-it/lib"; | ||
import { htmlComment } from "../../../src/shared/markdown-it/html-comment"; | ||
|
||
function createParser() { | ||
const instance = new MarkdownIt("default", { html: true }); | ||
instance.use(htmlComment); | ||
return instance; | ||
} | ||
|
||
describe("html-comment markdown-it plugin", () => { | ||
it("should add the html comment block rule to the instance", () => { | ||
const instance = createParser(); | ||
const blockRulesNames = instance.block.ruler | ||
.getRules("") | ||
.map((r) => r.name); | ||
expect(blockRulesNames).toContain("html_comment"); | ||
}); | ||
|
||
it("should detect single line html comment blocks", () => { | ||
const singleLineComment = "<!-- an html comment -->"; | ||
const instance = createParser(); | ||
const tokens = instance.parse(singleLineComment, {}); | ||
|
||
expect(tokens).toHaveLength(1); | ||
expect(tokens[0].type).toBe("html_comment"); | ||
expect(tokens[0].content).toBe(singleLineComment); | ||
expect(tokens[0].map).toEqual([0, 1]); | ||
}); | ||
|
||
it("should detect multiline html comment blocks", () => { | ||
const multilineComment = `<!-- an html comment\n over multiple lines -->`; | ||
const instance = createParser(); | ||
const tokens = instance.parse(multilineComment, {}); | ||
|
||
expect(tokens).toHaveLength(1); | ||
expect(tokens[0].type).toBe("html_comment"); | ||
expect(tokens[0].content).toBe(multilineComment); | ||
expect(tokens[0].map).toEqual([0, 2]); | ||
}); | ||
|
||
it("should detect indented html comment blocks", () => { | ||
const indentedComment = ` <!--\n an html comment\n 2 space indented\n -->`; | ||
const instance = createParser(); | ||
const tokens = instance.parse(indentedComment, {}); | ||
|
||
expect(tokens).toHaveLength(1); | ||
expect(tokens[0].type).toBe("html_comment"); | ||
expect(tokens[0].content).toBe(indentedComment); | ||
expect(tokens[0].map).toEqual([0, 4]); | ||
}); | ||
|
||
it.each([ | ||
"other text <!-- an html comment -->", | ||
"<!-- an html comment --> other text", | ||
"<!-- an html comment --> <div>other element</div> <!-- an html comment -->", | ||
])( | ||
"should ignore html comments inlined with other element/text (test #%#)", | ||
(inlinedHtmlComment) => { | ||
const instance = createParser(); | ||
const tokens = instance.parse(inlinedHtmlComment, {}); | ||
|
||
expect(tokens.map((t) => t.type)).not.toContain("html_comment"); | ||
} | ||
); | ||
}); |
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