-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
29 lines (24 loc) · 857 Bytes
/
index.js
File metadata and controls
29 lines (24 loc) · 857 Bytes
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
import { visit } from "unist-util-visit"
import { h } from "hastscript"
function codeTitle(opt) {
const className = (opt && opt.className) || "rehype-code-title"
return function (tree) {
visit(tree, { tagName: "pre" }, visitor)
function visitor(node, index) {
if (node.children && node.children.length > 0) {
const { properties } = node.children[0]
if (properties.className && properties.className.length > 0) {
const [lang, filename] = properties.className[0]
.split(":")
.map((e) => e.trim())
properties.className = lang
if (!filename) return
const title = h("h1", filename)
const container = h("div", { class: className }, [title, node])
tree.children[index] = container
}
}
}
}
}
export {codeTitle as default}