all-posts page, page titles, index page
This commit is contained in:
38
src/plugins.ts
Normal file
38
src/plugins.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import type { Root, Paragraph, PhrasingContent } from 'mdast';
|
||||
import type { VFile } from 'vfile';
|
||||
import { visit } from 'unist-util-visit';
|
||||
import { toString } from 'mdast-util-to-string';
|
||||
import { writeFileSync } from 'node:fs';
|
||||
|
||||
|
||||
export function remarkDescription() {
|
||||
return (tree: Root, vfile: VFile) => {
|
||||
if (vfile.basename == 'advent-of-languages-2024-04.mdx') {
|
||||
writeFileSync('tree.json', JSON.stringify(tree, undefined, 4));
|
||||
}
|
||||
|
||||
let description: string | null = null;
|
||||
|
||||
visit(tree, 'paragraph', (node: Paragraph) => {
|
||||
if (description !== null) return;
|
||||
description = summarize(node);
|
||||
});
|
||||
|
||||
// Astro exposes this as `remarkPluginFrontmatter` from render()
|
||||
const astro = (vfile.data.astro ??= { frontmatter: {} }) as { frontmatter: Record<string, unknown> };
|
||||
astro.frontmatter.description = description;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a paragraph node to a plain-text string, stripping any
|
||||
* MDX JSX elements (e.g. <Sidenote>) so their content doesn't
|
||||
* leak into the summary.
|
||||
*/
|
||||
function summarize(par: Paragraph): string {
|
||||
const filtered = par.children.filter(
|
||||
(child: PhrasingContent) => !(child.type as string).startsWith('mdxJsx')
|
||||
);
|
||||
return toString({ type: 'paragraph', children: filtered });
|
||||
}
|
||||
Reference in New Issue
Block a user