---
title: "MarkdownStats"
description: "Word and character counts of a Markdown or MDX body, added as `_stats` by `markdown.stats`."
canonical: "https://www.qino.works/docs/api/types/markdown-stats"
---

# 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.

```ts title="qino/collections/posts.ts"
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

```ts title="@qino/cms"
import type { MarkdownStats } from "@qino/cms";

type MarkdownStats = {
  wordCount: number;
  proseCharacterCount: number;
  sourceCharacterCount: number;
};
```

## Fields

| Field                  | Description                                             |
| ---------------------- | ------------------------------------------------------- |
| `wordCount`            | Word-like segments in the extracted prose               |
| `proseCharacterCount`  | Grapheme count of the prose after whitespace collapsing |
| `sourceCharacterCount` | Grapheme count of the body, including syntax and code   |

## 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`:

```ts title="qino/collections/posts.ts"
views: (view) => ({
  default: view({
    augment: (post) => ({
      readingMinutes: Math.ceil(post._stats.wordCount / 220),
    }),
  }),
}),
```
