Reputation: 302
I'm trying to figure out how to parse markdown content that is already in a div:
<div id="markdown-content">
## Heading
</div>
I've been looking at jquery markdown parsers on github such Markdown-it, but can't find any with docs that show how to parse existing content. Markdown-it.js seems to be popular, but their only browser usage example is:
var md = window.markdownit();
var result = md.render('# markdown-it rulezz!');
I'm not sure how to apply that to parsing content that's already in a div, is it possible to do this in a simple way?
Upvotes: 3
Views: 3214
Reputation: 196
This method will parse an input string and return a list of block tokens.
var md = window.markdownit();
var result = md.parse( document.querySelector('#markdown-content').textContent, {} );
See https://markdown-it.github.io/markdown-it/#MarkdownIt.parse
Upvotes: 0
Reputation: 3128
All you have to do is get the markdown text and parse it. Then put it somewhere. You could even put it back where you got it from.
var markdown = $('#markdown-content').html();
var html = md.render(markdown);
$('#output').html(html);
Upvotes: 4