marked icon indicating copy to clipboard operation
marked copied to clipboard

Rendering .md files in html using marked.js

Open minimatest opened this issue 2 years ago • 1 comments

I am trying to render a markdown file (.md) in a <body> component of an html page.

The documentation presents the following usage:

marked.parse('# Marked in the browser\n\nRendered by **marked**.');

Which works well for what I want to do (it renders the markdown in html format). However, I'd like the input into the marked.parse call to be a .md file such that I don't have to write all of the content between simple quote barriers.

I tried:

marked.parse(src='file.md');

and

marked.parse('href=file.md');

but this didn't work.

Any advice?

minimatest avatar Jun 10 '22 21:06 minimatest

You will need to write a function that fetches the file and sends the text into marked.

Something like:

async function markedFile(file) {
  const res = await fetch(file)
  const md = await res.text()
  return marked.parse(md)
}
const html = await markedFile('/page.md');

UziTech avatar Jun 10 '22 22:06 UziTech