Support Ukraine 🇺🇦Help Ukrainian ArmyHumanitarian Assistance to Ukrainians

How to generate an excerpt from markdown text?

thoughtful

Jan 06 2020 at 07:00 GMT

I have markdown posts that I want to generate excerpts for.

Here's what I wrote so far:

function getMarkdownExcerpt(markdown, maxExcerptLength = 120) {
  const excerpt = markdown.slice(0, maxExcerptLength)

  if (markdown.length > maxExcerptLength) {
    return excerpt + '...'
  }

  return excerpt
}

However, the problem is that the final excerpt includes the front matter and the markdown special characters, which I don't want.

const markdown =
`---
title: Code example
slug: code-example
---

This **is** an _example_ with markdown text and some \`code\`:

\`\`\`
const x = 3 * 60
\`\`\`

The problem is that the excerpt is broken.

I want just the content text.
`

const excerpt = getMarkdownExcerpt(markdown)

console.log(excerpt)
// Output:
// ---
// title: Code example
// slug: code-example
// ---
//
// This **is** an _example_ with markdown text and some `code`:
//
// ```
// const ...

So, how can I generate an excerpt made just of content text (no front matter and no markdown special characters)?

1 Answer

Mike The Programmer

Jan 06 2020 at 07:25 GMT

First, you need to remove the front matter and extract just the content. This can be done using the front-matter npm package.

Second, you need to strip markdown special characters from the content. This can be done using the remove-markdown package.

npm install front-matter remove-markdown

Then, you can modify your getMarkdownExcerpt function as follows:

const frontMatter = require('front-matter')
const removeMd = require('remove-markdown')

function getMarkdownExcerpt(markdown, maxExcerptLength = 120) {
  const parsedMarkdown = frontMatter(markdown)
  let contentText = removeMd(parsedMarkdown.body)
  // Trim and normalize whitespace in content text
  contentText = contentText.trim().replace(/\s+/g, ' ')
  const excerpt = contentText.slice(0, maxExcerptLength)

  if (contentText.length > maxExcerptLength) {
    return excerpt + '...'
  }

  return excerpt
}

const excerpt = getMarkdownExcerpt(markdown)

console.log(excerpt)
// Output:
// This is an example with markdown text and some code: const x = 3 * 60 The problem is that the excerpt is broken. I want ...
claritician © 2022