a case study in simplicity: allow dashes in markdown wiki links

This commit is contained in:
Jeremy Kauffman 2018-09-30 17:59:49 -04:00
parent 6c87e69cbe
commit c019f2421f
2 changed files with 15 additions and 13 deletions

View file

@ -35,17 +35,7 @@ const md = require("markdown-it")({
if (finalString.match(numberRegex)) finalString = `_${finalString}`;
return finalString;
}
})
.use(require("markdown-it-wikilinks")({
makeAllLinksAbsolute: true,
baseURL: "/glossary#",
uriSuffix: "",
htmlAttributes: {
class: "wikilink"
}
}));
});
// E X P O R T
@ -78,7 +68,7 @@ module.exports = exports = (state, emit) => { // eslint-disable-line
const markdownFile = fs.readFileSync(`./documents/${path}.md`, "utf-8");
const markdownFileDetails = fm(markdownFile);
const renderedMarkdown = md.render(markdownFileDetails.body);
const updatedMarkdown = partialFinder(renderedMarkdown);
const updatedMarkdown = wikiFinder(partialFinder(renderedMarkdown));
if (markdownFileDetails.attributes.meta) {
const customMetadata = {};
@ -146,3 +136,16 @@ function partialFinder(markdownBody) {
return markdownBody;
}
function wikiFinder(markdownBody) {
return markdownBody.replace(/\[\[([\w\s/-]+)\]\]/g, (match, p1) => {
const label = p1.trim(),
href = encodeURI("/glossary#" + label.replace(/\s+/g, '-'));
return label ?
`<a href="${href}" class="link--glossary">${label}</a>` :
match.input;
}
);
}

View file

@ -32,7 +32,6 @@
"make-promises-safe": "^1.1.0",
"markdown-it": "^8.4.2",
"markdown-it-anchor": "^5.0.2",
"markdown-it-wikilinks": "^1.0.1",
"nanocomponent": "^6.5.2",
"prismjs": "^1.15.0",
"redis": "^2.8.0",