parent
3d249dde05
commit
27579e841e
@ -0,0 +1,56 @@
|
||||
'use strict'
|
||||
|
||||
var util = require('util')
|
||||
|
||||
function Plugin(options) {
|
||||
var self = function (md) {
|
||||
self.options = options
|
||||
self.init(md)
|
||||
}
|
||||
|
||||
self.__proto__ = Plugin.prototype
|
||||
self.id = 'markdown-tag'
|
||||
|
||||
return self
|
||||
}
|
||||
|
||||
util.inherits(Plugin, Function)
|
||||
|
||||
Plugin.prototype.init = function (md) {
|
||||
md.inline.ruler.push(this.id, this.parse.bind(this))
|
||||
md.renderer.rules[this.id] = this.render.bind(this)
|
||||
}
|
||||
|
||||
export function tagParser(id, state, silent) {
|
||||
let input = state.src.slice(state.pos);
|
||||
|
||||
const match = /^#[^ ]+/.exec(input)
|
||||
if (!match) {
|
||||
return false
|
||||
}
|
||||
|
||||
console.log(match)
|
||||
const prefixLength = match[0].length
|
||||
if (!silent) {
|
||||
console.log(state.src, state.pos, prefixLength)
|
||||
let link = state.src.slice(state.pos + 1, state.pos + prefixLength)
|
||||
let token = state.push(id, '', 0)
|
||||
token.meta = {match: link, tag: true}
|
||||
console.log(token)
|
||||
}
|
||||
state.pos += prefixLength
|
||||
return true
|
||||
}
|
||||
|
||||
Plugin.prototype.parse = function (state, silent) {
|
||||
return tagParser(this.id, state, silent)
|
||||
}
|
||||
|
||||
Plugin.prototype.render = function (tokens, id, options, env) {
|
||||
let {match: link} = tokens[id].meta
|
||||
return '<a href="' + this.options.relativeBaseURL + encodeURIComponent(link) + '" class="wiki-link">' + '#' + link + '</a>';
|
||||
}
|
||||
|
||||
export default (options) => {
|
||||
return Plugin(options);
|
||||
}
|
Loading…
Reference in new issue