all-posts page, page titles, index page

This commit is contained in:
2026-03-30 05:27:41 -04:00
parent 5ffb51d5ce
commit fd8ed38572
6 changed files with 126 additions and 7 deletions

38
src/plugins.ts Normal file
View 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 });
}