All checks were successful
continuous-integration/drone/push Build is passing
Solution: create parser and renderer for metadata links.
79 lines
1.9 KiB
JavaScript
79 lines
1.9 KiB
JavaScript
'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 = 'wikilinks'
|
|
|
|
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 linkParser(id, state, silent) {
|
|
let input = state.src.slice(state.pos);
|
|
|
|
const match = /^#?\[\[/.exec(input)
|
|
if (!match) {
|
|
return false
|
|
}
|
|
|
|
let prefixLength = match[0].length
|
|
let p = state.pos + prefixLength
|
|
let open = 2
|
|
while (p < state.src.length - 1 && open > 0) {
|
|
if (state.src[p] === '[' && state.src[p + 1] === '[') {
|
|
open += 2
|
|
p += 2
|
|
} else if (state.src[p] === ']' && state.src[p + 1] === ']') {
|
|
open -= 2
|
|
p += 2
|
|
} else {
|
|
p++
|
|
}
|
|
}
|
|
|
|
if (open === 0) {
|
|
if (!silent) {
|
|
let link = state.src.slice(state.pos + prefixLength, p - 2)
|
|
let token = state.push(id, '', 0)
|
|
token.meta = {match: link, tag: prefixLength === 3}
|
|
}
|
|
state.pos = p
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
Plugin.prototype.parse = function (state, silent) {
|
|
return linkParser(this.id, state, silent)
|
|
}
|
|
|
|
Plugin.prototype.render = function (tokens, id, options, env) {
|
|
let {match: link, tag} = tokens[id].meta
|
|
if (tag) {
|
|
if (link === 'TODO') {
|
|
return '<input type="checkbox" class="checkbox">';
|
|
} else if (link === 'DONE') {
|
|
return '<input type="checkbox" class="checkbox" checked>';
|
|
}
|
|
}
|
|
return '<a href="'+this.options.relativeBaseURL+encodeURIComponent(link.replace(/ +/g, '_')) + '" class="wiki-link">' + (tag ? '#' : '') + link + '</a>';
|
|
}
|
|
|
|
export default (options) => {
|
|
return Plugin(options);
|
|
}
|