5

I want to render Markdown with JavaScript using the markdown-it library from here: https://github.com/markdown-it/markdown-it

I included markdown it from the CDN with this:

<script type="text/javascript" src="https://cdn.jsdelivr.net/markdown-it/8.2.2/markdown-it.min.js"></script>

Then I followed the instructions in the README, here is the relevant part of my code:

var md = window.markdownit();
document.getElementByID("content").innerHTML = md.render("# Header");

However, it isn't working and I am getting this error in the console:

Uncaught TypeError: e.src is undefined
    exports https://cdn.jsdelivr.net/markdown-it/8.2.2/markdown-it.min.js:3
    process https://cdn.jsdelivr.net/markdown-it/8.2.2/markdown-it.min.js:2
    parse https://cdn.jsdelivr.net/markdown-it/8.2.2/markdown-it.min.js:2
    render https://cdn.jsdelivr.net/markdown-it/8.2.2/markdown-it.min.js:2
    <anonymous> http://localhost/script.js:110

How do I fix this issue?

EDIT: I decided to use markdown-js instead. I'm leaving this question in case it is useful for somebody else.

1 Answer 1

3

I am able to use it with following code

<!doctype html>
<html>
<head>

<script src="https://cdnjs.cloudflare.com/ajax/libs/markdown-it/13.0.1/markdown-it.min.js" integrity="sha512-SYfDUYPg5xspsG6OOpXU366G8SZsdHOhqk/icdrYJ2E/WKZxPxze7d2HD3AyXpT7U22PZ5y74xRpqZ6A2bJ+kQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>


<script>
window.onload = function() {
  var md = window.markdownit();
  var div = document.getElementsByClassName('markdown');
  for(var i = 0; i < div.length; i++) {
    var content = div[i].innerHTML;
    document.getElementsByClassName('markdown')[i].innerHTML = md.render(content);
  }
}
</script>

</head>
<body>


<div class="markdown">
# h1 Heading
## h2 Heading
### h3 Heading
#### h4 Heading
##### h5 Heading
###### h6 Heading

+ one
+ two
  + two and half
+ three
  + three and half
    + three point seventy five
</div>

</body>
</html>

You can put any markdown between <div class="markdown"> and </div>.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there how to enable Highlight.js to markdown-it and to add some plugins for markdown-it?
I'd like to know that too, @Oo'-. Worth a new question...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.