Skip to content

Latest commit

 

History

History
197 lines (171 loc) · 7.15 KB

reading-time.mdx

File metadata and controls

197 lines (171 loc) · 7.15 KB
title Add reading time
description Build a Markdown processor plugin to add reading time to your Markdown or MDX files.
i18nReady true
type recipe

import { Steps } from '@astrojs/starlight/components'; import PackageManagerTabs from '/components/tabs/PackageManagerTabs.astro'; import MarkdownProcessorTabs from '/components/tabs/MarkdownProcessorTabs.astro';

Create a mdast plugin which adds a reading time property to the frontmatter of your Markdown or MDX files. Use this property to display the reading time for each page.

Recipe

1. Install the following packages depending on the [Markdown processor](/en/guides/markdown-content/#markdown-processors) used:
<MarkdownProcessorTabs>
  <Fragment slot="satteri">
    - [`reading-time`](https://www.npmjs.com/package/reading-time) to calculate minutes read
    - [`@astrojs/markdown-satteri`](https://www.npmjs.com/package/@astrojs/markdown-satteri) to configure [the `satteri()` processor](/en/guides/markdown-content/#setting-up-a-markdown-processor)
    - [`satteri`](https://www.npmjs.com/package/satteri) to create a [Sätteri `mdast` plugin](https://satteri.bruits.org/docs/plugins/#mdast-plugins)

    <PackageManagerTabs>
      <Fragment slot="npm">
      ```shell
      npm install reading-time @astrojs/markdown-satteri satteri
      ```
      </Fragment>
      <Fragment slot="pnpm">
      ```shell
      pnpm add reading-time @astrojs/markdown-satteri satteri
      ```
      </Fragment>
      <Fragment slot="yarn">
      ```shell
      yarn add reading-time @astrojs/markdown-satteri satteri
      ```
      </Fragment>
    </PackageManagerTabs>
  </Fragment>
  <Fragment slot="unified">
    - [`reading-time`](https://www.npmjs.com/package/reading-time) to calculate minutes read
    - [`mdast-util-to-string`](https://www.npmjs.com/package/mdast-util-to-string) to extract all text from your markdown
    - [`@astrojs/markdown-remark`](https://www.npmjs.com/package/@astrojs/markdown-remark) to use [the `unified()` processor](/en/guides/markdown-content/#setting-up-a-markdown-processor)

    <PackageManagerTabs>
      <Fragment slot="npm">
      ```shell
      npm install reading-time mdast-util-to-string @astrojs/markdown-remark
      ```
      </Fragment>
      <Fragment slot="pnpm">
      ```shell
      pnpm add reading-time mdast-util-to-string @astrojs/markdown-remark
      ```
      </Fragment>
      <Fragment slot="yarn">
      ```shell
      yarn add reading-time mdast-util-to-string @astrojs/markdown-remark
      ```
      </Fragment>
    </PackageManagerTabs>
  </Fragment>
</MarkdownProcessorTabs>
  1. Create a mdast plugin.

    This plugin retrieves the Markdown file's text. This text is then passed to the reading-time package to calculate the reading time in minutes.

    ```js title="src/mdast/mdast-reading-time.ts" import getReadingTime from "reading-time"; import { defineMdastPlugin } from "satteri";
     export const mdastReadingTimePlugin = defineMdastPlugin({
       name: "mdast-reading-time",
       after(root, context) {
         const textOnPage = context.textContent(root);
         const readingTime = getReadingTime(textOnPage);
    
         if (context.data.astro !== undefined) {
           // readingTime.text will give us minutes read as a friendly string,
           // i.e. "3 min read"
           context.data.astro.frontmatter.minutesRead = readingTime.text;
         }
       },
     });
     ```
    
    ```js title="remark-reading-time.mjs" import getReadingTime from "reading-time"; import { toString } from "mdast-util-to-string";
     export function remarkReadingTime() {
       return function (tree, { data }) {
         const textOnPage = toString(tree);
         const readingTime = getReadingTime(textOnPage);
         // readingTime.text will give us minutes read as a friendly string,
         // i.e. "3 min read"
         data.astro.frontmatter.minutesRead = readingTime.text;
       };
     }
     ```
    
  2. Add the plugin to your config:

    ```js title="astro.config.mjs" {1,3,7-9} import { satteri } from "@astrojs/markdown-satteri"; import { defineConfig } from "astro/config"; import { mdastReadingTimePlugin } from "./src/mdast/mdast-reading-time";
     export default defineConfig({
       markdown: {
         processor: satteri({
           mdastPlugins: [mdastReadingTimePlugin],
         }),
       },
     });
     ```
    
    ```js title="astro.config.mjs" {1,3,7-9} import { unified } from "@astrojs/markdown-remark"; import { defineConfig } from "astro/config"; import { remarkReadingTime } from "./remark-reading-time.mjs";
     export default defineConfig({
       markdown: {
         processor: unified({
           remarkPlugins: [remarkReadingTime],
         }),
       },
     });
     ```
    

    Now all Markdown documents will have a calculated minutesRead property in their frontmatter.

  3. Display Reading Time

    If your blog posts are stored in a content collection, access the remarkPluginFrontmatter from the render() function. Then, render minutesRead in your template wherever you would like it to appear.

    ---
    import { getCollection, render } from "astro:content";
    
    export async function getStaticPaths() {
      const blog = await getCollection("blog");
      return blog.map((entry) => ({
        params: { slug: entry.id },
        props: { entry },
      }));
    }
    
    const { entry } = Astro.props;
    const { Content, remarkPluginFrontmatter } = await render(entry);
    ---
    
    <html>
      <head>
        <meta charset="utf-8" />
        <title>{entry.data.title}</title>
      </head>
      <body>
        <title>{entry.data.title}</title>
        <p>{remarkPluginFrontmatter.minutesRead}</p>
        <Content />
      </body>
    </html>

    If you're using a Markdown layout, use the minutesRead frontmatter property from Astro.props in your layout template.

    ---
    const { minutesRead } = Astro.props.frontmatter;
    ---
    
    <html>
      <head>
        <meta charset="utf-8" />
      </head>
      <body>
        <p>{minutesRead}</p>
        <slot />
      </body>
    </html>