Problem: metadata links are not parsed and rendered
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Solution: create parser and renderer for metadata links.
This commit is contained in:
parent
74b1220710
commit
1f6a22966f
|
@ -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: '',
|
||||||
|
|
54
editor/src/metadatalinks.js
Normal file
54
editor/src/metadatalinks.js
Normal 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);
|
||||||
|
}
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user