Compare commits

...

2 Commits

Author SHA1 Message Date
67af38f785 Implement "silent" for markdown
All checks were successful
continuous-integration/drone/push Build is passing
2021-10-29 22:59:33 +02:00
ccc3ac7c66 Apply relativeBaseURL to parse urls 2021-10-29 22:43:34 +02:00
3 changed files with 36 additions and 10 deletions

View File

@ -21,7 +21,7 @@ Plugin.prototype.init = function (md) {
md.renderer.rules[this.id] = this.render.bind(this) md.renderer.rules[this.id] = this.render.bind(this)
} }
export function linkParser(id, state) { 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) {
@ -44,9 +44,11 @@ export function linkParser(id, state) {
} }
if (open === 0) { if (open === 0) {
if (!silent) {
let link = state.src.slice(state.pos + prefixLength, p - 2) let link = state.src.slice(state.pos + prefixLength, p - 2)
let token = state.push(id, '', 0) let token = state.push(id, '', 0)
token.meta = {match: link, tag: prefixLength === 3} token.meta = {match: link, tag: prefixLength === 3}
}
state.pos = p state.pos = p
return true return true
} }
@ -55,7 +57,7 @@ export function linkParser(id, state) {
} }
Plugin.prototype.parse = function (state, silent) { Plugin.prototype.parse = function (state, silent) {
return linkParser(this.id, state) return linkParser(this.id, state, silent)
} }
Plugin.prototype.render = function (tokens, id, options, env) { Plugin.prototype.render = function (tokens, id, options, env) {
@ -67,7 +69,7 @@ Plugin.prototype.render = function (tokens, id, options, env) {
return '<input type="checkbox" class="checkbox" checked>'; return '<input type="checkbox" class="checkbox" checked>';
} }
} }
return '<a href="/' + link.replace(' ', '_') + '" class="wiki-link">' + (tag ? '#' : '') + link + '</a>'; return '<a href="'+this.options.relativeBaseURL+encodeURIComponent(link.replace(' ', '_')) + '" class="wiki-link">' + (tag ? '#' : '') + link + '</a>';
} }
export default (options) => { export default (options) => {

View File

@ -51,16 +51,20 @@ describe('MD', function () {
assert.deepStrictEqual(MD.renderInline("test #[[TODO]] test2"), 'test <input type="checkbox" class="checkbox"> test2') assert.deepStrictEqual(MD.renderInline("test #[[TODO]] test2"), 'test <input type="checkbox" class="checkbox"> test2')
}) })
it('parseLinks 4', function () { 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>') assert.deepStrictEqual(MD.renderInline("test [[test]] [[test2]] [[test3]]"), 'test <a href="/edit/test" class="wiki-link">test</a> <a href="/edit/test2" class="wiki-link">test2</a> <a href="/edit/test3" class="wiki-link">test3</a>')
}) })
it('parseLinks 5', function () { it('parseLinks 5', function () {
assert.deepStrictEqual(MD.renderInline("test [[test]]"), 'test <a href="/test" class="wiki-link">test</a>') assert.deepStrictEqual(MD.renderInline("test [[test]]"), 'test <a href="/edit/test" class="wiki-link">test</a>')
}) })
it('parseLinks 6', function () { 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>') assert.deepStrictEqual(MD.renderInline("test [[test]] [[test2]]"), 'test <a href="/edit/test" class="wiki-link">test</a> <a href="/edit/test2" class="wiki-link">test2</a>')
}) })
it('parseLinks tag', function () { it('parseLinks tag', function () {
assert.deepStrictEqual(MD.renderInline("test #[[test]]"), 'test <a href="/test" class="wiki-link">#test</a>') assert.deepStrictEqual(MD.renderInline("test #[[test]]"), 'test <a href="/edit/test" class="wiki-link">#test</a>')
})
it('parseLinks double url', function () {
assert.deepStrictEqual(MD.renderInline("[[test [[link]] test2]]"), '<a href="/edit/test_%5B%5Blink%5D%5D%20test2" class="wiki-link">test [[link]] test2</a>')
}) })
}) })

View File

@ -114,4 +114,24 @@ describe('linkParser', function () {
}] }]
}) })
}) })
it('parse text and two links', function () {
let state = {src: '1234 [[hello [[world]] 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 [[hello [[world]] Link2]]',
pos: 30,
tokens: [{
id: 'test',
s: '',
x: 0,
meta: {match:'hello [[world]] Link2', tag: false}
}]
})
})
}) })