Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 8 additions & 68 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@
"watch": "tsc -watch -p ./"
},
"dependencies": {
"@thomaskoppelaar/markdown-it-wikilinks": "^1.3.0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to understand other implications of this change. Your goal is to make it so the wiki links are clickable inside the vscode markdown preview -- but does this break any other use cases? Eg, is this going to break things somehow for html generated for other purposes / for viewing outside of the Markdown Prview?

"github-slugger": "^1.3.0"
"github-slugger": "^1.3.0",
"markdown-it-regexp": "^0.4.0"
},
"devDependencies": {
"@babel/core": "^7.10.1",
Expand Down
68 changes: 55 additions & 13 deletions src/MarkdownRenderingPlugin.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these function name changes just cleanup to match the names in pluginSettings?

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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these export removed?

function postProcessLabel(label: string) {
// Trim whitespaces
label = label.trim();

Expand All @@ -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',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the new version work with the piped syntax 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 `<a ${htmlAttrsString}>${label}</a>`;
}
);
}