Since 0.16.0
MarkdownStats
Word and character counts of a Markdown or MDX body, added as `_stats` by `markdown.stats`.
Set markdown: { stats: true } on a Markdown or MDX collection, tree, or item, and every entry gets a _stats field. Related entries get it too when their primitive sets markdown.stats. JSON primitives can't set it.
export const postCollection = qino.defineCollection({
directory: "posts",
extension: ".md",
schema: PostSchema,
markdown: { stats: true },
});
const post = await postCollection.getEntry("hello");
post._stats.wordCount; // number
Type
import type { MarkdownStats } from "@qino/cms";
type MarkdownStats = {
wordCount: number;
proseCharacterCount: number;
sourceCharacterCount: number;
};
Fields
What counts as prose
The body is parsed with remark, GFM, and MDX. Text nodes are kept. Code blocks, inline code, HTML, MDX expressions, MDX components, import or export statements, and {#id} heading ids are excluded. Link and image URLs are excluded. Line breaks become newlines and block nodes are joined with newlines, then whitespace is collapsed. If MDX parsing fails, such as on an HTML comment, the body is re-parsed as plain Markdown.
Words are segmented with Intl.Segmenter, and characters are counted as graphemes rather than UTF-16 code units, so multi-byte scripts and emoji count once each.
The Markdown parsers are only loaded when a primitive sets markdown.stats.
Reading time
_stats has no reading time. Derive it in a view's augment:
views: (view) => ({
default: view({
augment: (post) => ({
readingMinutes: Math.ceil(post._stats.wordCount / 220),
}),
}),
}),