All files / markdown / plugins / markers.ts

83.33% Branches 5/6
100.00% Functions 1/1
88.89% Lines 16/18
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
29
30
31
32
33
34
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x20
x4
 
 
x7
x7
x7
x3
x3
x3
x3
x3
x3
x3
x3
x3
x4
x4



















I













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

/**
 * Add support for markers.
 *
 * Markers may be assigned a single-letter attribute (e.g. for colors) using the `=x=foo=` syntax.
 *
 * ```md
 * ==foo==
 * =r=bar=
 * ```
 * ```html
 * <p><mark>foo</mark></p>
 * <p><mark r="">bar</mark></p>
 * ```
 */
export default function markers(engine: MarkdownIt): void {
  engine.inline.ruler.after("escape", "markers", (state, silent) => {
    if (state.src[state.pos] !== "=")
      return false
    const match = /^=(?:([a-z])=|=)([^=\s](?:[^=\n]*[^=\s])?)=(=)?/.exec(state.src.slice(state.pos, state.posMax))
    if (!match)
      return false
    if (!silent) {
      const token = state.push("mark_open", "mark", 1)
      if (match[1])
        token.attrSet(match[1], "")
      state.push("text", "", 0).content = match[2]
      state.push("mark_close", "mark", -1)
    }
    state.pos += match[0].length
    return true
  })
}