All files / markdown / plugins / highlighting.ts

100.00% Branches 2/2
100.00% Functions 2/2
100.00% Lines 8/8
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
 
 
x20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x20
x3
x4
x4
x3
x3
x3























// Imports
import type { MarkdownIt } from "../types.ts"
import hljs from "highlight.js/lib/common"

/**
 * Highlight code blocks.
 *
 * ````md
 * ```ts
 * const foo = 'bar'
 * ```
 * ````
 * ```html
 * <pre>
 *   <code class="hljs language-ts"><span class="hljs-keyword">const</span> foo = <span class="hljs-string">'bar'</span></code>
 * </pre>
 * ```
 */
export default function highlighting(engine: MarkdownIt): void {
  engine.options.highlight = (code, language) => {
    if ((!language) || (!hljs.getLanguage(language)))
      return ""
    return `<pre><code class="hljs language-${engine.utils.escapeHtml(language)}">${hljs.highlight(code, { language }).value}</code></pre>`
  }
}