Skip to content
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

Merged preview extension #86

Merged
merged 5 commits into from
Sep 17, 2020
Merged
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
43 changes: 42 additions & 1 deletion package-lock.json

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

17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"Programming Languages"
],
"contributes": {
"markdown.markdownItPlugins": true,
"languages": [
{
"id": "markdown-notes",
Expand Down Expand Up @@ -134,6 +135,21 @@
"type": "boolean",
"default": true,
"description": "Trigger suggest on both insertion AND replacement of new character inside a wiki-link. Defaults true. Set false to only trigger suggest on insertion. See PR #69 for details."
},
"vscodeMarkdownNotes.previewShowFileExtension": {
"type": "boolean",
"default": false,
"description": "Specifies whether or not to show the linked file's extension in the preview."
},
"vscodeMarkdownNotes.previewLabelStyling": {
"type": "string",
"default": "[[label]]",
"enum": [
"[[label]]",
"[label]",
"label"
],
"description": "Changes how the link to a file should be shown, either with or without brackets."
}
}
},
Expand Down Expand Up @@ -187,6 +203,7 @@
"unified": "^9.0.0",
"unist-util-find": "^1.0.1",
"unist-util-visit": "^2.0.2",
"@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.

Should this be a separate repo, or should some of this code be moved in as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe it's best to keep this separate - We can override practically all settings / functions from within require('@thomaskoppelaar/markdown-it-wikilinks')({}), and people may want to use this themselves for exporting / rendering for their own personal use cases.

"vsce": "^1.75.0",
"vscode-test": "^1.3.0"
}
Expand Down
63 changes: 63 additions & 0 deletions src/MarkdownRenderingPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { MarkdownDefinitionProvider } from './MarkdownDefinitionProvider';
import { NoteWorkspace } from './NoteWorkspace';
import { RefType } from './Ref';

// 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) {
const ref = {
type: RefType.WikiLink,
word: label,
hasExtension: false,
range: undefined
}
const results = MarkdownDefinitionProvider.filesForWikiLinkRefFromCache(ref, null);

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);

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) {
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) {
// Trim whitespaces
label = label.trim();

// De-slugify label into whitespaces
label = label.split(NoteWorkspace.slugifyChar()).join(" ");

if (NoteWorkspace.previewShowFileExtension()) {
label += `.${NoteWorkspace.defaultFileExtension()}`;
}

switch (NoteWorkspace.previewLabelStyling()) {
case "[[label]]":
return `[[${label}]]`;
case "[label]":
return `[${label}]`;
case "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(),
});
}
28 changes: 28 additions & 0 deletions src/NoteWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ enum PipedWikiLinksSyntax {
descFile = 'desc|file',
}

enum PreviewLabelStyling {
brackets = "[[label]]",
bracket = "[label]",
none = "label"
}

type Config = {
createNoteOnGoToDefinitionWhenMissing: boolean;
defaultFileExtension: string;
Expand All @@ -41,6 +47,8 @@ type Config = {
pipedWikiLinksSyntax: PipedWikiLinksSyntax;
pipedWikiLinksSeparator: string;
newNoteDirectory: string;
previewLabelStyling: PreviewLabelStyling;
previewShowFileExtension: boolean;
};
// This class contains:
// 1. an interface to some of the basic user configurable settings or this extension
Expand Down Expand Up @@ -76,6 +84,8 @@ export class NoteWorkspace {
pipedWikiLinksSyntax: PipedWikiLinksSyntax.descFile,
pipedWikiLinksSeparator: '\\|',
newNoteDirectory: NoteWorkspace.NEW_NOTE_SAME_AS_ACTIVE_NOTE,
previewLabelStyling: PreviewLabelStyling.brackets,
previewShowFileExtension: false,
};
static DOCUMENT_SELECTOR = [
// { scheme: 'file', language: 'markdown' },
Expand All @@ -84,6 +94,9 @@ export class NoteWorkspace {
{ language: 'mdx' },
];

// Cache object to store results from noteFiles() in order to provide a synchronous method to the preview renderer.
static noteFileCache: vscode.Uri[] = [];

static cfg(): Config {
let c = vscode.workspace.getConfiguration('vscodeMarkdownNotes');
return {
Expand All @@ -103,6 +116,8 @@ export class NoteWorkspace {
pipedWikiLinksSyntax: c.get('pipedWikiLinksSyntax') as PipedWikiLinksSyntax,
pipedWikiLinksSeparator: c.get('pipedWikiLinksSeparator') as string,
newNoteDirectory: c.get('newNoteDirectory') as string,
previewLabelStyling: c.get('previewLabelStyling') as PreviewLabelStyling,
previewShowFileExtension: c.get('previewShowFileExtension') as boolean,
};
}

Expand Down Expand Up @@ -138,6 +153,14 @@ export class NoteWorkspace {
return this.cfg().newNoteDirectory;
}

static previewLabelStyling(): string {
return this.cfg().previewLabelStyling;
}

static previewShowFileExtension(): boolean {
return this.cfg().previewShowFileExtension;
}

static rxTagNoAnchors(): RegExp {
// NB: MUST have g flag to match multiple words per line
// return /\#[\w\-\_]+/i; // used to match tags that appear within lines
Expand Down Expand Up @@ -421,6 +444,11 @@ export class NoteWorkspace {
let files = (await vscode.workspace.findFiles('**/*')).filter(
(f) => f.scheme == 'file' && f.path.match(that.rxFileExtensions())
);
this.noteFileCache = files;
return files;
}

static noteFilesFromCache(): Array<vscode.Uri> {
return this.noteFileCache;
}
}
10 changes: 10 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { MarkdownFileCompletionItemProvider } from './MarkdownFileCompletionItem
import { NoteWorkspace } from './NoteWorkspace';
import { NoteParser } from './NoteParser';
import { getRefAt, RefType } from './Ref';
import { pluginSettings } from './MarkdownRenderingPlugin';
// import { debug } from 'util';
// import { create } from 'domain';

Expand Down Expand Up @@ -62,4 +63,13 @@ export function activate(context: vscode.ExtensionContext) {
const treeView = vscode.window.createTreeView('vscodeMarkdownNotesBacklinks', {
treeDataProvider: backlinksTreeDataProvider,
});

// See: https://code.visualstudio.com/api/extension-guides/markdown-extension
// For more information on how this works.
return {
extendMarkdownIt(md: any) {
thomaskoppelaar marked this conversation as resolved.
Show resolved Hide resolved
return md.use(
pluginSettings()
)}
};
}