markdown-it regex plugin example
by Arve Seljebu
HTML
<script src="https://wzrd.in/standalone/markdown-it@latest"></script>
<script src="https://wzrd.in/standalone/markdown-it-regexp@latest"></script>
<script src="https://wzrd.in/standalone/html@latest"></script>
<pre id="pre"></pre>
JavaScript
// globals are from browserify as a service (wzrd.in)
var md = markdownIt()
var Plugin = markdownItRegexp
var plugin = Plugin(
// regexp to match
/@(\w+)/,
// this function will be called when something matches
function(match, utils) {
var url = 'http://example.org/u/' + match[1]
return '<a href="' + utils.escape(url) + '">'
+ utils.escape(match[1])
+ '</a>'
}
)
// use the regex plugin
md.use(plugin)
// render markdown
var res = md.render("hello @user")
// print to window
pre.textContent = html.prettyPrint(res)