Table of Contents
Test cases
The current implementation (due to ast2md
) causes Table of Contents, wiki-links and normal links to be saved incorrectly, as well as transcluded code:
🔥 Emoji in file name. ⚠️
Push & Pull
Increase or decrease a sections nesting level.
const {visit, selectPosition} = lit.utils.unist
const {toMarkdown, ungroupSections}
= lit.parser.utils
const selectPos = selectPosition.selectAll
const unGroup = ungroupSections()()
const ast2md = tree => toMarkdown(unGroup(tree))
const tree = lit.ast
const test = 'heading'
const push = (node) => node.depth += 1
const pull = (node) => node.depth -= 1
visit(tree,test,push)
// return selectPos.toString()
return ast2md(tree)
const { visit, filter, select, selectPosition } = lit.utils.unist;
let found;
const { toMarkdown, ungroupSections } = lit.parser.utils;
const ast2md = (ast) => {
const unGroup = ungroupSections()();
const tree = unGroup(ast);
const md = toMarkdown(tree);
return md;
};
const secFromCell = (pos) => {
const secs = selectPosition.selectAll("section", pos, lit.ast);
const last = secs.slice(-1)[0];
return last.position;
};
const pos = secFromCell(this.position);
const withPos = (tree, pos, visitor) =>
visit(
lit.ast,
() => true,
(node) => {
if (node?.position?.start?.offset === pos.start?.offset) {
console.log("found match: ", node.type);
visitor(node);
return visit.SKIP;
}
}
);
withPos(lit.ast, pos, (node) => {
visit(node, "heading", (node) => {
node.depth += 1;
});
});
return ast2md(lit.ast);
Extract
Other
const { visit } = lit.utils.unist;
const { toMarkdown, ungroupSections } = lit.parser.utils;
const ast2md = (ast) => {
const unGroup = ungroupSections()();
const tree = unGroup(ast);
const md = toMarkdown(tree);
return md;
};
visit(lit.ast, "heading", (node) => {
node.depth += 1;
});
return ast2md(lit.ast);