This commit is contained in:
Zef Hemel
2022-11-27 08:48:01 +01:00
parent 2603fdb939
commit 3b1802399a
4 changed files with 72 additions and 22 deletions
+20 -2
View File
@@ -1,6 +1,10 @@
import { parse } from "./parse_tree.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";
const sample1 = `---
@@ -11,6 +15,8 @@ tags:
---
# This is a doc
Here is a [[wiki link]] and a [[wiki link|alias]].
Supper`;
const sampleInvalid1 = `---
@@ -25,9 +31,21 @@ Deno.test("Test parser", () => {
lang,
sample1,
);
console.log("tree", JSON.stringify(tree, null, 2));
// console.log("tree", JSON.stringify(tree, null, 2));
// Check if rendering back to text works
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");
assertNotEquals(node, undefined);
tree = parse(lang, sampleInvalid1);
+18 -3
View File
@@ -20,10 +20,10 @@ import {
mdExtensionSyntaxConfig,
} from "./markdown_ext.ts";
export const pageLinkRegex = /^\[\[([^\]]+)\]\]/;
export const pageLinkRegex = /^\[\[([^\]\|]+)(\|([^\]]+))?\]\]/;
const WikiLink: MarkdownConfig = {
defineNodes: ["WikiLink", "WikiLinkPage", {
defineNodes: ["WikiLink", "WikiLinkPage", "WikiLinkAlias", {
name: "WikiLinkMark",
style: t.processingInstruction,
}],
@@ -38,11 +38,25 @@ const WikiLink: MarkdownConfig = {
) {
return -1;
}
const [_fullMatch, page, pipePart, label] = match;
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(
cx.elt("WikiLink", pos, endPos, [
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),
]),
);
@@ -222,6 +236,7 @@ export default function buildMarkdown(mdExtensions: MDExt[]): Language {
styleTags({
WikiLink: ct.WikiLinkTag,
WikiLinkPage: ct.WikiLinkPageTag,
WikiLinkAlias: ct.WikiLinkPageTag,
// CommandLink: ct.CommandLinkTag,
// CommandLinkName: ct.CommandLinkNameTag,
Task: ct.TaskTag,