Astro Repo

Astro Utilities Javascript

1 min read

Reading Time

How to add reading time to Astro Content Collections

utils.js
/*
 * @param mdx page content
 *
 * @return time to read rounded up.
 */
export function readingTime(s) {
  const wpm = 200;
  const n = s
    .replace(/[^\w\s]/gi, "")
    .replaceAll("\r", "")
    .replaceAll("\n", "")
    .split(" ").length;

  return Math.ceil(n / wpm);
}

On this page