-
Notifications
You must be signed in to change notification settings - Fork 69
Make wiki-links work in VSCode's Markdown preview #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
018aaec
0d4f5fb
37720bc
4ff3890
835be0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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); | ||
|
|
||
|
|
@@ -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) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these function name changes just cleanup to match the names in |
||
| 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) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are these |
||
| 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', | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will the new version work with the piped syntax |
||
| 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>`; | ||
| } | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
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?