All files / markdown / plugins / ruby.ts

50.00% Branches 2/4
100.00% Functions 2/2
76.47% Lines 13/17
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
 
 
 
 
 
 
 
 
 
 
 
 
 
x20
x1
 
 
x1
 
 
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1















I


I









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

/**
 * Add support for {@link https://developer.mozilla.org/docs/Web/HTML/Element/ruby | ruby} text.
 *
 * ```md
 * {漢字}^(kanji)
 * ```
 * ```html
 * <ruby>漢字<rp>(</rp><rt>kanji</rt><rp>)</rp></ruby>
 * ```
 */
export default function ruby(engine: MarkdownIt): void {
  engine.inline.ruler.after("escape", "ruby", (state, silent) => {
    if (state.src[state.pos] !== "{")
      return false
    const match = /^\{([^}\n]+)\}\^\(([^)\n]+)\)/.exec(state.src.slice(state.pos, state.posMax))
    if (!match)
      return false
    if (!silent)
      state.push("ruby", "ruby", 0).meta = { base: match[1], annotation: match[2] }
    state.pos += match[0].length
    return true
  })
  engine.renderer.rules.ruby = (tokens, index) => {
    const { base, annotation } = tokens[index].meta as { base: string; annotation: string }
    return `<ruby>${engine.utils.escapeHtml(base)}<rp>(</rp><rt>${engine.utils.escapeHtml(annotation)}</rt><rp>)</rp></ruby>`
  }
}