pull/138/head
Zef Hemel 2022-11-27 08:48:01 +01:00
parent 2603fdb939
commit 3b1802399a
4 changed files with 72 additions and 22 deletions

View File

@ -1,6 +1,10 @@
import { parse } from "./parse_tree.ts"; import { parse } from "./parse_tree.ts";
import buildMarkdown from "./parser.ts"; import buildMarkdown from "./parser.ts";
import { findNodeOfType, renderToText } from "../plug-api/lib/tree.ts"; import {
collectNodesOfType,
findNodeOfType,
renderToText,
} from "../plug-api/lib/tree.ts";
import { assertEquals, assertNotEquals } from "../test_deps.ts"; import { assertEquals, assertNotEquals } from "../test_deps.ts";
const sample1 = `--- const sample1 = `---
@ -11,6 +15,8 @@ tags:
--- ---
# This is a doc # This is a doc
Here is a [[wiki link]] and a [[wiki link|alias]].
Supper`; Supper`;
const sampleInvalid1 = `--- const sampleInvalid1 = `---
@ -25,9 +31,21 @@ Deno.test("Test parser", () => {
lang, lang,
sample1, sample1,
); );
console.log("tree", JSON.stringify(tree, null, 2)); // console.log("tree", JSON.stringify(tree, null, 2));
// Check if rendering back to text works // Check if rendering back to text works
assertEquals(renderToText(tree), sample1); assertEquals(renderToText(tree), sample1);
// Find wiki link and wiki link alias
const links = collectNodesOfType(tree, "WikiLink");
assertEquals(links.length, 2);
const nameNode = findNodeOfType(links[0], "WikiLinkPage");
assertEquals(nameNode?.children![0].text, "wiki link");
// Check if alias is parsed properly
const aliasNode = findNodeOfType(links[1], "WikiLinkAlias");
assertEquals(aliasNode?.children![0].text, "alias");
// Find frontmatter
let node = findNodeOfType(tree, "FrontMatter"); let node = findNodeOfType(tree, "FrontMatter");
assertNotEquals(node, undefined); assertNotEquals(node, undefined);
tree = parse(lang, sampleInvalid1); tree = parse(lang, sampleInvalid1);

View File

@ -20,10 +20,10 @@ import {
mdExtensionSyntaxConfig, mdExtensionSyntaxConfig,
} from "./markdown_ext.ts"; } from "./markdown_ext.ts";
export const pageLinkRegex = /^\[\[([^\]]+)\]\]/; export const pageLinkRegex = /^\[\[([^\]\|]+)(\|([^\]]+))?\]\]/;
const WikiLink: MarkdownConfig = { const WikiLink: MarkdownConfig = {
defineNodes: ["WikiLink", "WikiLinkPage", { defineNodes: ["WikiLink", "WikiLinkPage", "WikiLinkAlias", {
name: "WikiLinkMark", name: "WikiLinkMark",
style: t.processingInstruction, style: t.processingInstruction,
}], }],
@ -38,11 +38,25 @@ const WikiLink: MarkdownConfig = {
) { ) {
return -1; return -1;
} }
const [_fullMatch, page, pipePart, label] = match;
const endPos = pos + match[0].length; const endPos = pos + match[0].length;
let aliasElts: any[] = [];
if (pipePart) {
const pipeStartPos = pos + 2 + page.length;
aliasElts = [
cx.elt("WikiLinkMark", pipeStartPos, pipeStartPos + 1),
cx.elt(
"WikiLinkAlias",
pipeStartPos + 1,
pipeStartPos + 1 + label.length,
),
];
}
return cx.addElement( return cx.addElement(
cx.elt("WikiLink", pos, endPos, [ cx.elt("WikiLink", pos, endPos, [
cx.elt("WikiLinkMark", pos, pos + 2), cx.elt("WikiLinkMark", pos, pos + 2),
cx.elt("WikiLinkPage", pos + 2, endPos - 2), cx.elt("WikiLinkPage", pos + 2, pos + 2 + page.length),
...aliasElts,
cx.elt("WikiLinkMark", endPos - 2, endPos), cx.elt("WikiLinkMark", endPos - 2, endPos),
]), ]),
); );
@ -222,6 +236,7 @@ export default function buildMarkdown(mdExtensions: MDExt[]): Language {
styleTags({ styleTags({
WikiLink: ct.WikiLinkTag, WikiLink: ct.WikiLinkTag,
WikiLinkPage: ct.WikiLinkPageTag, WikiLinkPage: ct.WikiLinkPageTag,
WikiLinkAlias: ct.WikiLinkPageTag,
// CommandLink: ct.CommandLinkTag, // CommandLink: ct.CommandLinkTag,
// CommandLinkName: ct.CommandLinkNameTag, // CommandLinkName: ct.CommandLinkNameTag,
Task: ct.TaskTag, Task: ct.TaskTag,

View File

@ -1,3 +1,4 @@
import { pageLinkRegex } from "../../common/parser.ts";
import { import {
Decoration, Decoration,
DecorationSet, DecorationSet,
@ -29,39 +30,54 @@ class CleanWikiLinkPlugin {
// let parentRange: [number, number]; // let parentRange: [number, number];
iterateTreeInVisibleRanges(view, { iterateTreeInVisibleRanges(view, {
enter: ({ type, from, to }) => { enter: ({ type, from, to }) => {
if (type.name === "WikiLinkPage") { if (type.name === "WikiLink") {
// Adding 2 on each side due to [[ and ]] that are outside the WikiLinkPage node // Adding 2 on each side due to [[ and ]] that are outside the WikiLinkPage node
if (isCursorInRange(view.state, [from - 2, to + 2])) { if (isCursorInRange(view.state, [from, to])) {
return; return;
} }
// Add decoration to hide the prefix [[ // Add decoration to hide the prefix [[
widgets.push( widgets.push(
invisibleDecoration.range( invisibleDecoration.range(
from - 2,
from, from,
from + 2,
), ),
); );
// Add decoration to hide the postfix [[ // Add decoration to hide the postfix [[
widgets.push( widgets.push(
invisibleDecoration.range( invisibleDecoration.range(
to - 2,
to, to,
to + 2,
), ),
); );
// Now check if there's a "/" inside // Now check if this page has an alias
const text = view.state.sliceDoc(from, to); const text = view.state.sliceDoc(from, to);
if (text.indexOf("/") === -1) { const match = pageLinkRegex.exec(text);
return; if (!match) return;
const [_fullMatch, page, pipePart] = match;
if (!pipePart) {
// No alias, let's check if there's a slash in the page name
if (text.indexOf("/") === -1) {
return;
}
// Add a inivisible decoration to hide the path prefix
widgets.push(
invisibleDecoration.range(
from + 2, // +2 to skip the [[
from + text.lastIndexOf("/") + 1,
),
);
} else {
// Alias is present, so we hide the part before the pipe
widgets.push(
invisibleDecoration.range(
from + 2,
from + page.length + 3, // 3 is for the [[ and the |
),
);
} }
// Add a inivisible decoration to hide the path prefix
widgets.push(
invisibleDecoration.range(
from,
from + text.lastIndexOf("/") + 1,
),
);
} }
}, },
}); });

View File

@ -1,11 +1,12 @@
An attempt at documenting of the changes/new features introduced in each An attempt at documenting the changes/new features introduced in each
release. release.
--- ---
## 0.2.2 ## 0.2.2
* Removed `publish` out of builtins, this will be available via [silverbullet-publish](https://github.com/silverbulletmd/silverbullet-publish) later.
* Added `invokeFunction` command to run arbitrary functions from the CLI. * New page link aliasing syntax (Obsidian compatible) is here: `[[page link|alias]]` e.g. [[CHANGELOG|this is a link to this changelog]].
* Added `invokeFunction` `silverbullet` CLI sub-command to run arbitrary plug functions from the CLI.
--- ---