initial feed implementation
This commit is contained in:
@ -1,19 +1,48 @@
|
||||
import { visit } from 'unist-util-visit';
|
||||
import { toString } from 'mdast-util-to-string';
|
||||
|
||||
import fs from 'node:fs';
|
||||
|
||||
// build table of contents and inject into frontmatter
|
||||
export function localRemark() {
|
||||
return (tree, vfile) => {
|
||||
let toc = [];
|
||||
let description = null;
|
||||
|
||||
visit(tree, 'heading', node => {
|
||||
toc.push({
|
||||
text: toString(node),
|
||||
depth: node.depth,
|
||||
});
|
||||
visit(tree, ['heading', 'paragraph'], node => {
|
||||
// build table of contents and inject into frontmatter
|
||||
if (node.type === 'heading') {
|
||||
toc.push({
|
||||
text: toString(node),
|
||||
depth: node.depth,
|
||||
});
|
||||
}
|
||||
|
||||
// inject description (first 25 words of the first paragraph)
|
||||
if (node.type === 'paragraph' && description === null) {
|
||||
description = summarize(node);
|
||||
}
|
||||
});
|
||||
|
||||
vfile.data.fm.toc = toc;
|
||||
vfile.data.fm.description = description;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// convert paragraph to single string after stripping everything between html tags
|
||||
function summarize(par) {
|
||||
let newChildren = [];
|
||||
let push = true;
|
||||
for (const child of par.children) {
|
||||
if (child.type === 'html') {
|
||||
push = !push;
|
||||
continue;
|
||||
}
|
||||
if (push) {
|
||||
newChildren.push(child);
|
||||
}
|
||||
}
|
||||
|
||||
return toString({type: 'paragraph', children: newChildren});
|
||||
}
|
||||
|
Reference in New Issue
Block a user