0.4.0 • Published 10 years ago
markdown-it-hashtag v0.4.0
markdown-it-hashtag
hashtag (
#tag) plugin for markdown-it markdown parser.
#hashtag => <a href="/tags/hashtag" class="tag">#hashtag</a>
Install
node.js, bower:
npm install markdown-it-hashtag --save
bower install markdown-it-hashtag --saveUse
Basic
var md = require('markdown-it')()
.use(require('markdown-it-hashtag'));
md.render('#hashtag'); // => '<p><a href="/tags/hashtag" class="tag">#hashtag</a></p>'Differences in browser. If you load the script directly into the page, without
package system, module will add itself globally as window.markdownitHashtag.
Advanced
You can specify the RegExp for hashtags and specify the allowed preceding content. You can also modify the output of the renderer. Here is an example with default values:
var md = require('markdown-it')()
.use(require('markdown-it-hashtag'),{
// pattern for hashtags with normal string escape rules
hashtagRegExp: '\\w+',
// pattern for allowed preceding content
preceding: '^|\\s'
});
md.renderer.rules.hashtag_open = function(tokens, idx) {
var tagName = tokens[idx].content.toLowerCase();
return '<a href="/tags/' + tagName + '" class="tag">';
}
md.renderer.rules.hashtag_text = function(tokens, idx) {
return '#' + tokens[idx].content;
}
md.renderer.rules.hashtag_close = function { return '</a>'; }
md.render('#hashtag'); // => '<p><a href="/tags/hashtag" class="tag">#hashtag</a></p>'