Improve parsing of wiki links

This commit is contained in:
Peter Stuifzand 2020-06-09 23:44:12 +02:00
parent 1f8340ca3c
commit 6b760d19a8
5 changed files with 93 additions and 14 deletions

View File

@ -8605,9 +8605,9 @@
}
},
"wiki-list-editor": {
"version": "0.7.2",
"resolved": "https://registry.npmjs.org/wiki-list-editor/-/wiki-list-editor-0.7.2.tgz",
"integrity": "sha512-Rw5Cn+BztW+q/JRhOKjOSwZ4Wyx/Llv/8YuxYRDkMPV/buCUethIAYeywoStaoSxkXft3ONHcXq8Hgu9AFwtjw==",
"version": "0.7.3",
"resolved": "https://registry.npmjs.org/wiki-list-editor/-/wiki-list-editor-0.7.3.tgz",
"integrity": "sha512-JZTHH0b5Q2O4UO+nBesOUP1C4W3awh4mfMtsga8j1sR65P+I5MIBj/ymrvtZCGVYI2PL3RDA3aGWBsfp7s2u4w==",
"requires": {
"dragula": "^3.7.2",
"he": "^1.2.0",

View File

@ -26,7 +26,7 @@
"node-sass": "^4.14.1",
"sass-loader": "^7.3.1",
"style-loader": "^1.0.0",
"wiki-list-editor": "^0.7.2"
"wiki-list-editor": "^0.7.3"
},
"scripts": {
"test": "node_modules/.bin/mocha -r esm",

View File

@ -1,6 +1,6 @@
import listEditor from 'wiki-list-editor';
import MarkdownIt from 'markdown-it';
import MarkdownItWikilinks from 'markdown-it-wikilinks';
import MarkdownItWikilinks from './wikilinks';
import MarkdownItMark from 'markdown-it-mark';
import axios from 'axios';
import qs from 'querystring'
@ -178,7 +178,10 @@ if (holder) {
MD.use(MarkdownItWikilinks({
baseURL: holder.dataset.baseUrl,
uriSuffix: '',
relativeBaseURL: '/edit/'
relativeBaseURL: '/edit/',
htmlAttributes: {
class: 'wiki-link'
}
})).use(MarkdownItMark)
const options = {
transform(text, callback) {

View File

@ -12,20 +12,14 @@ body {
font-family: 'Inter', sans-serif;
}
.content a::before {
.content a.wiki-link::before {
content: "[[";
color: #ccc;
}
.content a::after {
.content a.wiki-link::after {
content: "]]";
color: #ccc;
}
.content a.tag::before {
content: "";
}
.content a.tag::after {
content: "";
}
@supports (font-variation-settings: normal) {
html {

82
editor/src/wikilinks.js Normal file
View File

@ -0,0 +1,82 @@
'use strict'
const Plugin = require('markdown-it-regexp')
const extend = require('extend')
const sanitize = require('sanitize-filename')
module.exports = (options) => {
const defaults = {
baseURL: '/',
relativeBaseURL: './',
makeAllLinksAbsolute: false,
uriSuffix: '.html',
htmlAttributes: {
},
generatePageNameFromLabel: (label) => {
return label
},
postProcessPageName: (pageName) => {
pageName = pageName.trim()
pageName = pageName.split('/').map(sanitize).join('/')
pageName = pageName.replace(/\s+/, '_')
return pageName
},
postProcessLabel: (label) => {
label = label.trim()
return label
}
}
options = extend(true, defaults, options)
function isAbsolute(pageName) {
return options.makeAllLinksAbsolute || pageName.charCodeAt(0) === 0x2F/* / */
}
function removeInitialSlashes(str) {
return str.replace(/^\/+/g, '')
}
return Plugin(
/(#?)\[\[([:\w\s/]+)\]\]/,
(match, utils) => {
let label = ''
let pageName = ''
let href = ''
let htmlAttrs = []
let htmlAttrsString = ''
let tag = match[1]
label = match[2]
pageName = options.generatePageNameFromLabel(label)
label = options.postProcessLabel(label)
pageName = options.postProcessPageName(pageName)
// make sure none of the values are empty
if (!label || !pageName) {
return match.input
}
if (isAbsolute(pageName)) {
pageName = removeInitialSlashes(pageName)
href = options.baseURL + pageName + options.uriSuffix
}
else {
href = options.relativeBaseURL + pageName + options.uriSuffix
}
href = utils.escape(href)
htmlAttrs.push(`href="${href}"`)
for (let attrName in options.htmlAttributes) {
const attrValue = options.htmlAttributes[attrName]
htmlAttrs.push(`${attrName}="${attrValue}"`)
}
htmlAttrsString = htmlAttrs.join(' ')
return `<a ${htmlAttrsString}>${tag}${label}</a>`
}
)
}