From 018aaec5db587cbdb7e511203b53664b35734b56 Mon Sep 17 00:00:00 2001 From: Shuhei Yoshida Date: Wed, 22 Feb 2023 22:31:28 +0900 Subject: [PATCH 1/5] generate page paths relative to a workspace folder --- src/MarkdownRenderingPlugin.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/MarkdownRenderingPlugin.ts b/src/MarkdownRenderingPlugin.ts index fdb5264..0b7d7b0 100644 --- a/src/MarkdownRenderingPlugin.ts +++ b/src/MarkdownRenderingPlugin.ts @@ -1,6 +1,8 @@ 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. @@ -18,8 +20,9 @@ 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; } From 0d4f5fbd5be1c201f65aaa3a97051c16ea947a5c Mon Sep 17 00:00:00 2001 From: Shuhei Yoshida Date: Wed, 22 Feb 2023 22:32:14 +0900 Subject: [PATCH 2/5] add a-tag's data-href attr --- src/MarkdownRenderingPlugin.ts | 58 +++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/src/MarkdownRenderingPlugin.ts b/src/MarkdownRenderingPlugin.ts index 0b7d7b0..ec11646 100644 --- a/src/MarkdownRenderingPlugin.ts +++ b/src/MarkdownRenderingPlugin.ts @@ -8,7 +8,7 @@ import { workspace } from 'vscode'; // 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); @@ -28,12 +28,12 @@ export function PageNameGenerator(label: string) { } // 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(); @@ -52,15 +52,51 @@ 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(), - }); + 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}"`); + htmlAttrs.push(`data-href="${href}"`); + htmlAttrsString = htmlAttrs.join(' '); + + return `${label}`; + } + ); } From 37720bc73d177a1ae5f47d0475f81ee10e5fe2e1 Mon Sep 17 00:00:00 2001 From: Shuhei Yoshida Date: Wed, 22 Feb 2023 22:35:39 +0900 Subject: [PATCH 3/5] uninstall @thomaskoppelaar/markdown-it-wikilinks --- package-lock.json | 79 +++-------------------------------------------- package.json | 1 - 2 files changed, 4 insertions(+), 76 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8164721..273c8c4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,6 @@ "name": "vscode-markdown-notes", "version": "0.0.25", "dependencies": { - "@thomaskoppelaar/markdown-it-wikilinks": "^1.3.0", "github-slugger": "^1.3.0" }, "devDependencies": { @@ -2532,16 +2531,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 +4872,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", @@ -9569,11 +9559,6 @@ "markdown-it": "bin/markdown-it.js" } }, - "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=" - }, "node_modules/markdown-it/node_modules/argparse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", @@ -12130,14 +12115,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 +13061,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 +13655,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 +16194,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 +18024,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", @@ -21732,11 +21687,6 @@ } } }, - "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=" - }, "mdast-util-from-markdown": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/mdast-util-from-markdown/-/mdast-util-from-markdown-1.2.0.tgz", @@ -23577,14 +23527,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 +24282,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 +24746,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..5460af5 100644 --- a/package.json +++ b/package.json @@ -206,7 +206,6 @@ "watch": "tsc -watch -p ./" }, "dependencies": { - "@thomaskoppelaar/markdown-it-wikilinks": "^1.3.0", "github-slugger": "^1.3.0" }, "devDependencies": { From 4ff3890fb3284e5aa56b9c1bce2d0b65dde3b1b8 Mon Sep 17 00:00:00 2001 From: Shuhei Yoshida Date: Wed, 22 Feb 2023 22:37:11 +0900 Subject: [PATCH 4/5] install markdown-it-regexp --- package-lock.json | 13 ++++++++++++- package.json | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 273c8c4..b2f376e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,8 @@ "name": "vscode-markdown-notes", "version": "0.0.25", "dependencies": { - "github-slugger": "^1.3.0" + "github-slugger": "^1.3.0", + "markdown-it-regexp": "^0.4.0" }, "devDependencies": { "@babel/core": "^7.10.1", @@ -9559,6 +9560,11 @@ "markdown-it": "bin/markdown-it.js" } }, + "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": "sha512-0XQmr46K/rMKnI93Y3CLXsHj4jIioRETTAiVnJnjrZCEkGaDOmUxTbZj/aZ17G5NlRcVpWBYjqpwSlQ9lj+Kxw==" + }, "node_modules/markdown-it/node_modules/argparse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", @@ -21687,6 +21693,11 @@ } } }, + "markdown-it-regexp": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/markdown-it-regexp/-/markdown-it-regexp-0.4.0.tgz", + "integrity": "sha512-0XQmr46K/rMKnI93Y3CLXsHj4jIioRETTAiVnJnjrZCEkGaDOmUxTbZj/aZ17G5NlRcVpWBYjqpwSlQ9lj+Kxw==" + }, "mdast-util-from-markdown": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/mdast-util-from-markdown/-/mdast-util-from-markdown-1.2.0.tgz", diff --git a/package.json b/package.json index 5460af5..8d87921 100644 --- a/package.json +++ b/package.json @@ -206,7 +206,8 @@ "watch": "tsc -watch -p ./" }, "dependencies": { - "github-slugger": "^1.3.0" + "github-slugger": "^1.3.0", + "markdown-it-regexp": "^0.4.0" }, "devDependencies": { "@babel/core": "^7.10.1", From 835be0b3091fdb7232fe9bf04dca975ed888c6f5 Mon Sep 17 00:00:00 2001 From: Shuhei Yoshida Date: Thu, 23 Feb 2023 17:18:54 +0900 Subject: [PATCH 5/5] add comments --- src/MarkdownRenderingPlugin.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/MarkdownRenderingPlugin.ts b/src/MarkdownRenderingPlugin.ts index ec11646..9be7f0e 100644 --- a/src/MarkdownRenderingPlugin.ts +++ b/src/MarkdownRenderingPlugin.ts @@ -56,6 +56,8 @@ function postProcessLabel(label: string) { } export function pluginSettings(): any { + // 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) => { @@ -93,7 +95,8 @@ export function pluginSettings(): any { href = utils.escape(href); htmlAttrs.push(`href="${href}"`); - htmlAttrs.push(`data-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}`;