Problem: metadata links are not parsed and rendered
All checks were successful
continuous-integration/drone/push Build is passing

Solution: create parser and renderer for metadata links.
This commit is contained in:
Peter Stuifzand 2021-11-14 23:36:34 +01:00
parent 74b1220710
commit 1f6a22966f
3 changed files with 57 additions and 1 deletions

View File

@ -1,6 +1,6 @@
import MarkdownIt from "markdown-it"; import MarkdownIt from "markdown-it";
//import MarkdownItWikilinks from "./wikilinks";
import MarkdownItWikilinks2 from "./wikilinks2"; import MarkdownItWikilinks2 from "./wikilinks2";
import MarkdownItMetadata from "./metadatalinks";
import MarkdownItMark from "markdown-it-mark"; import MarkdownItMark from "markdown-it-mark";
import MarkdownItKatex from "markdown-it-katex"; import MarkdownItKatex from "markdown-it-katex";
@ -21,6 +21,7 @@ MD.use(MarkdownItWikilinks2({
class: 'wiki-link' class: 'wiki-link'
}, },
})) }))
MD.use(MarkdownItMetadata())
// MD.use(MarkdownItWikilinks({ // MD.use(MarkdownItWikilinks({
// baseURL: document.querySelector('body').dataset.baseUrl, // baseURL: document.querySelector('body').dataset.baseUrl,
// uriSuffix: '', // uriSuffix: '',

View File

@ -0,0 +1,54 @@
'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 = 'metadata'
return self
}
util.inherits(Plugin, Function)
Plugin.prototype.init = function (md) {
md.inline.ruler.before('text', this.id, this.parse.bind(this), {})
md.renderer.rules[this.id] = this.render.bind(this)
}
export function metadataLinkParser(id, state, silent) {
let input = state.src;
if (state.pos === 0 && input.match(/^([A-Za-z0-9 ]+)::/)) {
let [key, sep, value] = input.split(/::( |$)/)
console.log(key, value)
if (key.length === 0) return false;
if (!silent) {
let token = state.push(id, '', 0)
token.meta = {key, value, tag: 'metadata'}
}
state.pos = key.length + 3;
return true
}
return false
}
Plugin.prototype.parse = function (state, silent) {
return metadataLinkParser(this.id, state, silent)
}
Plugin.prototype.render = function (tokens, id, options, env) {
let {match: link, tag} = tokens[id].meta
if (tag === 'metadata') {
let {key, value} = tokens[id].meta
return '<span class="metadata-key"><a href="/edit/'+key.replace(/ +/g, '_')+'">'+key+'</a></span>: ';
}
}
export default (options) => {
return Plugin(options);
}

View File

@ -23,6 +23,7 @@ Plugin.prototype.init = function (md) {
export function linkParser(id, state, silent) { export function linkParser(id, state, silent) {
let input = state.src.slice(state.pos); let input = state.src.slice(state.pos);
const match = /^#?\[\[/.exec(input) const match = /^#?\[\[/.exec(input)
if (!match) { if (!match) {
return false return false