diff --git a/editor/package-lock.json b/editor/package-lock.json index 94bf4c0..e053d7f 100644 --- a/editor/package-lock.json +++ b/editor/package-lock.json @@ -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", diff --git a/editor/package.json b/editor/package.json index 3478fa1..c021d39 100644 --- a/editor/package.json +++ b/editor/package.json @@ -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", diff --git a/editor/src/index.js b/editor/src/index.js index f285da3..2690c21 100644 --- a/editor/src/index.js +++ b/editor/src/index.js @@ -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) { diff --git a/editor/src/styles.scss b/editor/src/styles.scss index b4b1517..98eb996 100644 --- a/editor/src/styles.scss +++ b/editor/src/styles.scss @@ -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 { diff --git a/editor/src/wikilinks.js b/editor/src/wikilinks.js new file mode 100644 index 0000000..b133f49 --- /dev/null +++ b/editor/src/wikilinks.js @@ -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 `${tag}${label}` + } + ) +}