All files / markdown / plugins / uncomments.ts

100.00% Branches 5/5
100.00% Functions 2/2
100.00% Lines 13/13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x20
x3
x3
x5
x11
x11
x11
x11
x11
x3
x3
x3
x3


























// Imports
import type { MarkdownIt } from "../types.ts"

/**
 * Remove HTML comments.
 *
 * Code blocks are left untouched.
 *
 * ```md
 * foo<!-- baz -->bar
 * ```
 * ```html
 * <p>foobar</p>
 * ```
 */
export default function uncomments(engine: MarkdownIt): void {
  engine.core.ruler.push("uncomments", (state) => {
    const walk = (tokens: typeof state.tokens) => {
      for (const token of tokens) {
        if (token.children)
          walk(token.children)
        if (["html_block", "html_inline", "text"].includes(token.type))
          token.content = token.content.replace(/<!--[\s\S]*?-->/g, "")
      }
    }
    walk(state.tokens)
  })
}