From 1f6a22966f14e662dfdf465b3740ec48a6d161a4 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sun, 14 Nov 2021 23:36:34 +0100 Subject: [PATCH] Problem: metadata links are not parsed and rendered Solution: create parser and renderer for metadata links. --- editor/src/markdown.js | 3 ++- editor/src/metadatalinks.js | 54 +++++++++++++++++++++++++++++++++++++ editor/src/wikilinks2.js | 1 + 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 editor/src/metadatalinks.js diff --git a/editor/src/markdown.js b/editor/src/markdown.js index 81f3658..cbfb9dc 100644 --- a/editor/src/markdown.js +++ b/editor/src/markdown.js @@ -1,6 +1,6 @@ import MarkdownIt from "markdown-it"; -//import MarkdownItWikilinks from "./wikilinks"; import MarkdownItWikilinks2 from "./wikilinks2"; +import MarkdownItMetadata from "./metadatalinks"; import MarkdownItMark from "markdown-it-mark"; import MarkdownItKatex from "markdown-it-katex"; @@ -21,6 +21,7 @@ MD.use(MarkdownItWikilinks2({ class: 'wiki-link' }, })) +MD.use(MarkdownItMetadata()) // MD.use(MarkdownItWikilinks({ // baseURL: document.querySelector('body').dataset.baseUrl, // uriSuffix: '', diff --git a/editor/src/metadatalinks.js b/editor/src/metadatalinks.js new file mode 100644 index 0000000..a18d70e --- /dev/null +++ b/editor/src/metadatalinks.js @@ -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 ''+key+': '; + } +} + +export default (options) => { + return Plugin(options); +} diff --git a/editor/src/wikilinks2.js b/editor/src/wikilinks2.js index 011bf3b..788fac3 100644 --- a/editor/src/wikilinks2.js +++ b/editor/src/wikilinks2.js @@ -23,6 +23,7 @@ Plugin.prototype.init = function (md) { export function linkParser(id, state, silent) { let input = state.src.slice(state.pos); + const match = /^#?\[\[/.exec(input) if (!match) { return false