#Using/rendering markdown in frontmatter?

7 messages · Page 1 of 1 (latest)

rain rover
#

I have blog posts in my content collections with descriptions where I use Markdown (e.g., Super _interesting_ [post](https://myblog.com/this_post) )

I want to be able to

  • render that frontmatter as formatted markdown when using in HTML
  • extract a plaintext version for things like opengraph descriptions.

Is that possible?

plain trout
#

You can do that using a markdown processing library like marked

import { marked } from 'marked';

// This is the html result of the markdown 
const renderedMarkdown = marked.parse("Super _interesting_ [post](https://myblog.com/this_post) ");

// turns the html to plain text
// the regex is just good enough and could fail in
// some cases I didn't think of
// use https://www.npmjs.com/package/html-to-text for a more
// battle tested plain text conversion
const plainText = renderedMarkdown.replace(/<\/?[^>]+(>|$)/g, "");
rain rover
#

thanks!

#

how would i show the html result of the markdown in an Astro component?

#

using a Fragment?

plain trout