@signalwire/docusaurus-plugin-llms-txt
Version:
Generate Markdown versions of Docusaurus HTML pages and an llms.txt index file
46 lines (45 loc) • 1.96 kB
JavaScript
import { visit } from 'unist-util-visit';
/**
* Rehype plugin that flattens <ul>/<ol> lists that appear *inside* table cells.
* Each list item becomes text prefixed with a dash or index and separated by <br />.
* This keeps tables valid in Markdown while preserving line-breaks and inline markup.
*/
const rehypeTables = function () {
return function transformer(tree) {
visit(tree, 'element', (node) => {
if (node.tagName !== 'td' && node.tagName !== 'th')
return;
visit(node, 'element', (listNode, listIndex, listParent) => {
if (!(listNode.tagName === 'ul' || listNode.tagName === 'ol') ||
!listParent ||
typeof listIndex !== 'number') {
return undefined;
}
const listType = listNode.tagName;
const listItems = (listNode.children || []).filter((c) => c.type === 'element' && c.tagName === 'li');
if (listItems.length === 0)
return undefined;
const replacement = [];
listItems.forEach((li, idx) => {
const prefix = listType === 'ol' ? `${idx + 1}. ` : '- ';
replacement.push({ type: 'text', value: prefix });
if (li.children?.length) {
replacement.push(...li.children);
}
if (idx < listItems.length - 1) {
replacement.push({
type: 'element',
tagName: 'br',
properties: {},
children: [],
});
}
});
listParent.children.splice(listIndex, 1, ...replacement);
return false;
});
});
return tree;
};
};
export default rehypeTables;