2021-05-17 14:29:16 +00:00
|
|
|
import { visit } from 'unist-util-visit';
|
2022-06-03 12:26:39 +00:00
|
|
|
import { toHtml } from 'hast-util-to-html';
|
2022-04-30 00:07:09 +00:00
|
|
|
import Slugger from 'github-slugger';
|
|
|
|
|
|
|
|
import type { MarkdownHeader, RehypePlugin } from './types.js';
|
2021-05-17 14:29:16 +00:00
|
|
|
|
|
|
|
export default function createCollectHeaders() {
|
2022-04-30 00:07:09 +00:00
|
|
|
const headers: MarkdownHeader[] = [];
|
|
|
|
const slugger = new Slugger();
|
2021-05-17 14:29:16 +00:00
|
|
|
|
2022-04-30 00:07:09 +00:00
|
|
|
function rehypeCollectHeaders(): ReturnType<RehypePlugin> {
|
|
|
|
return function (tree) {
|
2021-12-22 21:11:05 +00:00
|
|
|
visit(tree, (node) => {
|
|
|
|
if (node.type !== 'element') return;
|
|
|
|
const { tagName } = node;
|
|
|
|
if (tagName[0] !== 'h') return;
|
|
|
|
const [_, level] = tagName.match(/h([0-6])/) ?? [];
|
|
|
|
if (!level) return;
|
|
|
|
const depth = Number.parseInt(level);
|
2021-08-25 12:17:45 +00:00
|
|
|
|
2021-12-22 21:11:05 +00:00
|
|
|
let text = '';
|
2022-05-24 22:02:11 +00:00
|
|
|
let isJSX = false;
|
2022-06-03 12:26:39 +00:00
|
|
|
visit(node, (child, _, parent) => {
|
|
|
|
if (child.type === 'element' || parent == null) {
|
2022-05-24 22:02:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (child.type === 'raw') {
|
|
|
|
if (child.value.startsWith('\n<') || child.value.endsWith('>\n')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-05-24 22:03:29 +00:00
|
|
|
if (child.type === 'text' || child.type === 'raw') {
|
2022-06-03 12:26:39 +00:00
|
|
|
if (new Set(['code', 'pre']).has(parent.tagName)) {
|
|
|
|
text += child.value;
|
|
|
|
} else {
|
|
|
|
text += child.value.replace(/\{/g, '${');
|
|
|
|
isJSX = isJSX || child.value.includes('{');
|
|
|
|
}
|
2022-05-24 22:02:11 +00:00
|
|
|
}
|
2021-12-22 21:11:05 +00:00
|
|
|
});
|
2021-08-25 12:17:45 +00:00
|
|
|
|
2021-12-22 21:11:05 +00:00
|
|
|
node.properties = node.properties || {};
|
2022-04-30 00:07:09 +00:00
|
|
|
if (typeof node.properties.id !== 'string') {
|
2022-05-24 22:02:11 +00:00
|
|
|
if (isJSX) {
|
2022-06-03 12:26:39 +00:00
|
|
|
// HACK: serialized JSX from internal plugins, ignore these for slug
|
|
|
|
const raw = toHtml(node.children, { allowDangerousHtml: true })
|
|
|
|
.replace(/\n(<)/g, '<')
|
|
|
|
.replace(/(>)\n/g, '>');
|
2022-05-24 22:02:11 +00:00
|
|
|
// HACK: for ids that have JSX content, use $$slug helper to generate slug at runtime
|
2022-06-03 12:26:39 +00:00
|
|
|
node.properties.id = `$$slug(\`${text}\`)`;
|
2022-05-24 22:02:11 +00:00
|
|
|
(node as any).type = 'raw';
|
2022-05-24 22:03:29 +00:00
|
|
|
(
|
|
|
|
node as any
|
|
|
|
).value = `<${node.tagName} id={${node.properties.id}}>${raw}</${node.tagName}>`;
|
2022-05-24 22:02:11 +00:00
|
|
|
} else {
|
|
|
|
node.properties.id = slugger.slug(text);
|
|
|
|
}
|
2022-04-30 00:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
headers.push({ depth, slug: node.properties.id, text });
|
2021-12-22 21:11:05 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2021-08-25 12:17:45 +00:00
|
|
|
|
2021-12-22 21:11:05 +00:00
|
|
|
return {
|
|
|
|
headers,
|
|
|
|
rehypeCollectHeaders,
|
|
|
|
};
|
2021-05-17 14:29:16 +00:00
|
|
|
}
|