Improve links and checkboxes
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
Use the markdown parser to handle links and checkboxes instead of internal javascript. Also add catch clauses to the promises in the editor.master
parent
a2c83d87b6
commit
70f1b62646
@ -0,0 +1,75 @@
|
||||
'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) {
|
||||
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) {
|
||||
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)
|
||||
}
|
||||
|
||||
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="/' + link.replace(' ', '_') + '" class="wiki-link">' + link + '</a>';
|
||||
}
|
||||
|
||||
export default (options) => {
|
||||
return Plugin(options);
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Wiki - A wiki with editor
|
||||
* Copyright (c) 2021 Peter Stuifzand
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import assert from 'assert'
|
||||
import MarkdownIt from "markdown-it";
|
||||
import MarkdownItWikilinks2 from "../src/wikilinks2";
|
||||
|
||||
const MD = new MarkdownIt({
|
||||
linkify: false,
|
||||
highlight: function (str, lang) {
|
||||
if (lang === 'mermaid') {
|
||||
return '<div class="mermaid">' + str + '</div>';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
})
|
||||
|
||||
MD.use(MarkdownItWikilinks2({
|
||||
baseURL: 'http://localhost/',
|
||||
uriSuffix: '',
|
||||
relativeBaseURL: '/edit/',
|
||||
htmlAttributes: {
|
||||
class: 'wiki-link'
|
||||
},
|
||||
}))
|
||||
|
||||
describe('MD', function () {
|
||||
it('parseLinks', function () {
|
||||
assert.deepStrictEqual(MD.renderInline("#[[TODO]]"), '<input type="checkbox" class="checkbox">')
|
||||
assert.deepStrictEqual(MD.renderInline("#[[TODO]] #[[DONE]]"), '<input type="checkbox" class="checkbox"> <input type="checkbox" class="checkbox" checked>')
|
||||
})
|
||||
it('parseLinks 2', function () {
|
||||
assert.deepStrictEqual(MD.renderInline("#[[TODO]] #[[DONE]]"), '<input type="checkbox" class="checkbox"> <input type="checkbox" class="checkbox" checked>')
|
||||
})
|
||||
it('parseLinks 3', function () {
|
||||
assert.deepStrictEqual(MD.renderInline("test #[[TODO]] test2"), 'test <input type="checkbox" class="checkbox"> test2')
|
||||
})
|
||||
it('parseLinks 4', function () {
|
||||
assert.deepStrictEqual(MD.renderInline("test [[test]] [[test2]] [[test3]]"), 'test <a href="/test" class="wiki-link">test</a> <a href="/test2" class="wiki-link">test2</a> <a href="/test3" class="wiki-link">test3</a>')
|
||||
})
|
||||
it('parseLinks 5', function () {
|
||||
assert.deepStrictEqual(MD.renderInline("test [[test]]"), 'test <a href="/test" class="wiki-link">test</a>')
|
||||
})
|
||||
it('parseLinks 6', function () {
|
||||
assert.deepStrictEqual(MD.renderInline("test [[test]] [[test2]]"), 'test <a href="/test" class="wiki-link">test</a> <a href="/test2" class="wiki-link">test2</a>')
|
||||
})
|
||||
})
|
@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Wiki - A wiki with editor
|
||||
* Copyright (c) 2021 Peter Stuifzand
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import assert from 'assert'
|
||||
import {linkParser} from '../src/wikilinks2'
|
||||
|
||||
describe('linkParser', function () {
|
||||
it('parse', function () {
|
||||
let state = {src: '', pos: 0, tokens: []};
|
||||
state.__proto__.push = function (id, s, x) {
|
||||
let token = {id, s, x};
|
||||
this.tokens.push(token)
|
||||
return token
|
||||
}
|
||||
assert.deepStrictEqual(linkParser('test', state), false);
|
||||
assert.deepStrictEqual(state, {
|
||||
src: '',
|
||||
pos: 0,
|
||||
tokens: []
|
||||
})
|
||||
})
|
||||
|
||||
it('parse 2', function () {
|
||||
let state = {src: '[[Link]]', pos: 0, tokens: []};
|
||||
state.__proto__.push = function (id, s, x) {
|
||||
let token = {id, s, x};
|
||||
this.tokens.push(token)
|
||||
return token
|
||||
}
|
||||
assert.deepStrictEqual(linkParser('test', state), true);
|
||||
assert.deepStrictEqual(state, {
|
||||
src: '[[Link]]',
|
||||
pos: 8,
|
||||
tokens: [{
|
||||
id: 'test',
|
||||
s: '',
|
||||
x: 0,
|
||||
meta: {match:'Link', tag: false}
|
||||
}]
|
||||
})
|
||||
})
|
||||
|
||||
it('parse 3', function () {
|
||||
let state = {src: '1234[[Link]]', pos: 4, tokens: []};
|
||||
state.__proto__.push = function (id, s, x) {
|
||||
let token = {id, s, x};
|
||||
this.tokens.push(token)
|
||||
return token
|
||||
}
|
||||
assert.deepStrictEqual(linkParser('test', state), true);
|
||||
assert.deepStrictEqual(state, {
|
||||
src: '1234[[Link]]',
|
||||
pos: 12,
|
||||
tokens: [{
|
||||
id: 'test',
|
||||
s: '',
|
||||
x: 0,
|
||||
meta: {match:'Link', tag: false}
|
||||
}]
|
||||
})
|
||||
})
|
||||
|
||||
it('parse 4', function () {
|
||||
let state = {src: '1234#[[TODO]]', pos: 4, tokens: []};
|
||||
state.__proto__.push = function (id, s, x) {
|
||||
let token = {id, s, x};
|
||||
this.tokens.push(token)
|
||||
return token
|
||||
}
|
||||
assert.deepStrictEqual(linkParser('test', state), true);
|
||||
assert.deepStrictEqual(state, {
|
||||
src: '1234#[[TODO]]',
|
||||
pos: 13,
|
||||
tokens: [{
|
||||
id: 'test',
|
||||
s: '',
|
||||
x: 0,
|
||||
meta: {match:'TODO', tag: true}
|
||||
}]
|
||||
})
|
||||
})
|
||||
|
||||
it('parse text and two links', function () {
|
||||
let state = {src: '1234 [[Link]] [[Link2]]', pos: 5, tokens: []};
|
||||
state.__proto__.push = function (id, s, x) {
|
||||
let token = {id, s, x};
|
||||
this.tokens.push(token)
|
||||
return token
|
||||
}
|
||||
assert.deepStrictEqual(linkParser('test', state), true);
|
||||
assert.deepStrictEqual(state, {
|
||||
src: '1234 [[Link]] [[Link2]]',
|
||||
pos: 13,
|
||||
tokens: [{
|
||||
id: 'test',
|
||||
s: '',
|
||||
x: 0,
|
||||
meta: {match:'Link', tag: false}
|
||||
}]
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in new issue