diff --git a/package-lock.json b/package-lock.json index 8164721..b2f376e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,8 +8,8 @@ "name": "vscode-markdown-notes", "version": "0.0.25", "dependencies": { - "@thomaskoppelaar/markdown-it-wikilinks": "^1.3.0", - "github-slugger": "^1.3.0" + "github-slugger": "^1.3.0", + "markdown-it-regexp": "^0.4.0" }, "devDependencies": { "@babel/core": "^7.10.1", @@ -2532,16 +2532,6 @@ "@sinonjs/commons": "^1.7.0" } }, - "node_modules/@thomaskoppelaar/markdown-it-wikilinks": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/@thomaskoppelaar/markdown-it-wikilinks/-/markdown-it-wikilinks-1.3.0.tgz", - "integrity": "sha512-yJTwX9dxwnjw7Kxi6hyP4gY3fLu6EMoTQRWSSGA3iCBZGCRhwCmYs4KIguFSZfgYag329MELnGguuGIy/lNRFQ==", - "dependencies": { - "extend": "^3.0.0", - "markdown-it-regexp": "^0.4.0", - "sanitize-filename": "^1.6.1" - } - }, "node_modules/@types/babel__core": { "version": "7.1.12", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.12.tgz", @@ -4883,7 +4873,8 @@ "node_modules/extend": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", - "integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==" + "integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==", + "dev": true }, "node_modules/extend-shallow": { "version": "3.0.2", @@ -9572,7 +9563,7 @@ "node_modules/markdown-it-regexp": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/markdown-it-regexp/-/markdown-it-regexp-0.4.0.tgz", - "integrity": "sha1-1k1xPuzsVc5M/esyF1DswJniwtw=" + "integrity": "sha512-0XQmr46K/rMKnI93Y3CLXsHj4jIioRETTAiVnJnjrZCEkGaDOmUxTbZj/aZ17G5NlRcVpWBYjqpwSlQ9lj+Kxw==" }, "node_modules/markdown-it/node_modules/argparse": { "version": "2.0.1", @@ -12130,14 +12121,6 @@ "node": ">=0.10.0" } }, - "node_modules/sanitize-filename": { - "version": "1.6.3", - "resolved": "https://registry.npmjs.org/sanitize-filename/-/sanitize-filename-1.6.3.tgz", - "integrity": "sha512-y/52Mcy7aw3gRm7IrcGDFx/bCk4AhRh2eI9luHOQM86nZsqwiRkkq2GekHXBBD+SmPidc8i2PqtYZl+pWJ8Oeg==", - "dependencies": { - "truncate-utf8-bytes": "^1.0.0" - } - }, "node_modules/sax": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", @@ -13084,14 +13067,6 @@ "url": "https://github.com/sponsors/wooorm" } }, - "node_modules/truncate-utf8-bytes": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/truncate-utf8-bytes/-/truncate-utf8-bytes-1.0.2.tgz", - "integrity": "sha1-QFkjkJWS1W94pYGENLC3hInKXys=", - "dependencies": { - "utf8-byte-length": "^1.0.1" - } - }, "node_modules/ts-jest": { "version": "26.4.4", "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-26.4.4.tgz", @@ -13686,11 +13661,6 @@ "node": ">=0.10.0" } }, - "node_modules/utf8-byte-length": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/utf8-byte-length/-/utf8-byte-length-1.0.4.tgz", - "integrity": "sha1-9F8VDExm7uloGGUFq5P8u4rWv2E=" - }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", @@ -16230,16 +16200,6 @@ "@sinonjs/commons": "^1.7.0" } }, - "@thomaskoppelaar/markdown-it-wikilinks": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/@thomaskoppelaar/markdown-it-wikilinks/-/markdown-it-wikilinks-1.3.0.tgz", - "integrity": "sha512-yJTwX9dxwnjw7Kxi6hyP4gY3fLu6EMoTQRWSSGA3iCBZGCRhwCmYs4KIguFSZfgYag329MELnGguuGIy/lNRFQ==", - "requires": { - "extend": "^3.0.0", - "markdown-it-regexp": "^0.4.0", - "sanitize-filename": "^1.6.1" - } - }, "@types/babel__core": { "version": "7.1.12", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.12.tgz", @@ -18070,7 +18030,8 @@ "extend": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", - "integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==" + "integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==", + "dev": true }, "extend-shallow": { "version": "3.0.2", @@ -21735,7 +21696,7 @@ "markdown-it-regexp": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/markdown-it-regexp/-/markdown-it-regexp-0.4.0.tgz", - "integrity": "sha1-1k1xPuzsVc5M/esyF1DswJniwtw=" + "integrity": "sha512-0XQmr46K/rMKnI93Y3CLXsHj4jIioRETTAiVnJnjrZCEkGaDOmUxTbZj/aZ17G5NlRcVpWBYjqpwSlQ9lj+Kxw==" }, "mdast-util-from-markdown": { "version": "1.2.0", @@ -23577,14 +23538,6 @@ } } }, - "sanitize-filename": { - "version": "1.6.3", - "resolved": "https://registry.npmjs.org/sanitize-filename/-/sanitize-filename-1.6.3.tgz", - "integrity": "sha512-y/52Mcy7aw3gRm7IrcGDFx/bCk4AhRh2eI9luHOQM86nZsqwiRkkq2GekHXBBD+SmPidc8i2PqtYZl+pWJ8Oeg==", - "requires": { - "truncate-utf8-bytes": "^1.0.0" - } - }, "sax": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", @@ -24340,14 +24293,6 @@ "integrity": "sha512-rvuRbTarPXmMb79SmzEp8aqXNKcK+y0XaB298IXueQ8I2PsrATcPBCSPyK/dDNa2iWOhKlfNnOjdAOTBU/nkFA==", "dev": true }, - "truncate-utf8-bytes": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/truncate-utf8-bytes/-/truncate-utf8-bytes-1.0.2.tgz", - "integrity": "sha1-QFkjkJWS1W94pYGENLC3hInKXys=", - "requires": { - "utf8-byte-length": "^1.0.1" - } - }, "ts-jest": { "version": "26.4.4", "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-26.4.4.tgz", @@ -24812,11 +24757,6 @@ "integrity": "sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ==", "dev": true }, - "utf8-byte-length": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/utf8-byte-length/-/utf8-byte-length-1.0.4.tgz", - "integrity": "sha1-9F8VDExm7uloGGUFq5P8u4rWv2E=" - }, "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", diff --git a/package.json b/package.json index 07e09d0..8d87921 100644 --- a/package.json +++ b/package.json @@ -206,8 +206,8 @@ "watch": "tsc -watch -p ./" }, "dependencies": { - "@thomaskoppelaar/markdown-it-wikilinks": "^1.3.0", - "github-slugger": "^1.3.0" + "github-slugger": "^1.3.0", + "markdown-it-regexp": "^0.4.0" }, "devDependencies": { "@babel/core": "^7.10.1", diff --git a/src/MarkdownRenderingPlugin.ts b/src/MarkdownRenderingPlugin.ts index fdb5264..9be7f0e 100644 --- a/src/MarkdownRenderingPlugin.ts +++ b/src/MarkdownRenderingPlugin.ts @@ -1,12 +1,14 @@ import { MarkdownDefinitionProvider } from './MarkdownDefinitionProvider'; import { NoteWorkspace } from './NoteWorkspace'; import { RefType, refFromWikiLinkText } from './Ref'; +import { workspace } from 'vscode'; + // See also: https://github.com/tomleesm/markdown-it-wikilinks // Function that returns a filename based on the given wikilink. // Initially uses filesForWikiLinkRefFromCache() to try and find a matching file. // If this fails, it will attempt to make a (relative) link based on the label given. -export function PageNameGenerator(label: string) { +function generatePageNameFromLabel(label: string) { const ref = refFromWikiLinkText(label); const results = MarkdownDefinitionProvider.filesForWikiLinkRefFromCache(ref, null); @@ -18,19 +20,20 @@ export function PageNameGenerator(label: string) { label = NoteWorkspace.stripExtension(label); // Either use the first result of the cache, or in the case that it's empty use the label to create a path - let path: string = - results.length != 0 ? results[0].path : NoteWorkspace.noteFileNameFromTitle(label); + let path = results.length != 0 ? + workspace.asRelativePath(results[0].path, false) : + NoteWorkspace.noteFileNameFromTitle(label); return path; } // Transformation that only gets applied to the page name (ex: the "test-file.md" part of [[test-file.md | Description goes here]]). -export function postProcessPageName(pageName: string) { +function postProcessPageName(pageName: string) { return NoteWorkspace.stripExtension(pageName); } // Transformation that only gets applied to the link label (ex: the " Description goes here" part of [[test-file.md | Description goes here]]) -export function postProcessLabel(label: string) { +function postProcessLabel(label: string) { // Trim whitespaces label = label.trim(); @@ -49,15 +52,54 @@ export function postProcessLabel(label: string) { case 'label': return label; } + return label; } export function pluginSettings(): any { - return require('@thomaskoppelaar/markdown-it-wikilinks')({ - generatePageNameFromLabel: PageNameGenerator, - postProcessPageName: postProcessPageName, - postProcessLabel: postProcessLabel, - uriSuffix: `.${NoteWorkspace.defaultFileExtension()}`, - description_then_file: NoteWorkspace.pipedWikiLinksSyntax() == 'desc|file', - separator: NoteWorkspace.pipedWikiLinksSeparator(), - }); + // The code below was adapted from @thomaskoppelaar/markdown-it-wikilinks (375ce4650c), + // which was a forked version of @jsepia/markdown-it-wikilinks. + return require("markdown-it-regexp")( + new RegExp("\\[\\[([^sep\\]]+)(sep[^sep\\]]+)?\\]\\]".replace(/sep/g, NoteWorkspace.pipedWikiLinksSeparator())), + (match: any, utils: any) => { + let label = ''; + let pageName = ''; + let href = ''; + let htmlAttrs = []; + let htmlAttrsString = ''; + const isSplit = !!match[2]; + if (isSplit) { + if (NoteWorkspace.pipedWikiLinksSyntax() == 'desc|file') { + label = match[1]; + pageName = generatePageNameFromLabel(match[2].replace(new RegExp(NoteWorkspace.pipedWikiLinksSeparator()), '')); + } else { + label = match[2].replace(new RegExp(NoteWorkspace.pipedWikiLinksSeparator()), ''); + pageName = generatePageNameFromLabel(match[1]); + } + + } + else { + label = match[1]; + pageName = generatePageNameFromLabel(label); + } + + label = postProcessLabel(label); + pageName = postProcessPageName(pageName); + + // make sure none of the values are empty + if (!label || !pageName) { + return match.input; + } + + pageName = pageName.replace(/^\/+/g, ''); + href = "/" + pageName + `.${NoteWorkspace.defaultFileExtension()}`; + href = utils.escape(href); + + htmlAttrs.push(`href="${href}"`); + // The following line is necessary for the wiki-links to work on VSCode's Markdown preview + htmlAttrs.push(`data-href="${href}"`); + htmlAttrsString = htmlAttrs.join(' '); + + return `${label}`; + } + ); }