From 27579e841e38ecf38fc24efb300ff850d0fb9765 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Mon, 17 Jan 2022 20:56:37 +0100 Subject: [PATCH] Problem: tags are not parsed on client side Solution: parse tags in markdown --- editor/src/markdown-tag.js | 56 ++++++++++++++++++++++++++++++++++++++ editor/src/markdown.js | 6 +++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 editor/src/markdown-tag.js diff --git a/editor/src/markdown-tag.js b/editor/src/markdown-tag.js new file mode 100644 index 0000000..9bca554 --- /dev/null +++ b/editor/src/markdown-tag.js @@ -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 '' + '#' + link + ''; +} + +export default (options) => { + return Plugin(options); +} diff --git a/editor/src/markdown.js b/editor/src/markdown.js index 2d59b92..69b20aa 100644 --- a/editor/src/markdown.js +++ b/editor/src/markdown.js @@ -3,6 +3,7 @@ import MarkdownItWikilinks2 from "./wikilinks2"; import MarkdownItMetadata from "./metadatalinks"; import MarkdownItMark from "markdown-it-mark"; import MarkdownItKatex from "markdown-it-katex"; +import MarkdownItTag from "./markdown-tag"; const MD = new MarkdownIt({ linkify: true, @@ -30,6 +31,9 @@ MD.use(MarkdownItMetadata()) // class: 'wiki-link' // }, // })) -// MD.use(MarkdownItMark).use(MarkdownItKatex) +MD.use(MarkdownItMark).use(MarkdownItKatex) +MD.use(MarkdownItTag({ + relativeBaseURL: '/edit/', +})) export default MD;