react-code-dataset / chakra-ui /apps /www /lib /remark-code-title.ts
Devendra174's picture
Upload folder using huggingface_hub
1e92f2d verified
raw
history blame contribute delete
779 Bytes
import { visit } from "unist-util-visit"
export function remarkCodeTitle() {
return (tree: any, file: any) => {
visit(tree, "code", (node, index, parent) => {
const metaString = `${node.lang ?? ""} ${node.meta ?? ""}`.trim()
if (!metaString) return
const [title] = metaString.match(/(?<=title=("|'))(.*?)(?=("|'))/) ?? [""]
if (!title && metaString.includes("title=")) {
file.message("Invalid title", node, "remark-code-title")
return
}
if (!title) return
parent.children.splice(index, 1, {
type: "paragraph",
children: [node],
data: {
hName: "code-block",
hProperties: {
title,
lang: node.lang,
},
},
})
})
}
}