Skip to content

Instantly share code, notes, and snippets.

@srestraj
Last active April 15, 2022 10:23
Show Gist options
  • Save srestraj/672cb6a6cd2d2708739436b24cd131ac to your computer and use it in GitHub Desktop.
Save srestraj/672cb6a6cd2d2708739436b24cd131ac to your computer and use it in GitHub Desktop.
render markdown with @nuxtjs/markdownit

Render markdown for your feed module inside nuxt.config.js

Pre-requisite: You should have @nuxtjs/markdownit and @nuxtjs/feed installed

npm i @nuxtjs/markdownit @nuxtjs/feed

Then, configure your nuxt.config.js as follows

// nuxt markdown setup
const md = require('markdown-it')()

export default {
 ....
  modules: [
    '@nuxtjs/markdownit',
    '@nuxtjs/feed',
  ],
  markdownit: {
    injected: true,
    html: true,
  },
  feed: [
    {
      path: '/feed.xml',
      async create(feed) {
        feed.options = {
          ...
        }
        const posts = await ...

          posts.items.forEach(async (post) => {
            const url = `https://yoursite.com/path/${post.slug}`
            feed.addItem({
              title: post.title,
              id: url,
              link: url,
              description: md.render(post.description),
              published: new Date(post.publishDate),
              image: `${post.imageURL}`,
              content: `
                <p>
                  <img
                    alt="${post.title}"
                    src="${post.imageURL}"
                  >
                </p>
                ${md.render(post.content)}`,

            })
        })
      },
      cacheTime: 1000 * 60 * 15,
      type: 'rss2',
      // data: ['Some additional data']
    },
  ],
 ....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment