2022-10-17 21:48:21 +08:00
|
|
|
import { parse } from "./parse_tree.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { collectNodesOfType, findNodeOfType, renderToText } from "$lib/tree.ts";
|
|
|
|
import { assertEquals, assertNotEquals } from "$lib/test_deps.ts";
|
2024-01-24 20:34:12 +08:00
|
|
|
import { extendedMarkdownLanguage } from "./parser.ts";
|
2022-10-17 21:48:21 +08:00
|
|
|
|
|
|
|
const sample1 = `---
|
|
|
|
type: page
|
|
|
|
tags:
|
|
|
|
- hello
|
|
|
|
- world
|
|
|
|
---
|
|
|
|
# This is a doc
|
|
|
|
|
2022-11-27 15:48:01 +08:00
|
|
|
Here is a [[wiki link]] and a [[wiki link|alias]].
|
|
|
|
|
2022-10-17 21:48:21 +08:00
|
|
|
Supper`;
|
|
|
|
|
|
|
|
const sampleInvalid1 = `---
|
|
|
|
name: Zef
|
|
|
|
# This is a doc
|
|
|
|
|
|
|
|
Supper`;
|
|
|
|
|
|
|
|
Deno.test("Test parser", () => {
|
2024-01-24 20:34:12 +08:00
|
|
|
let tree = parse(extendedMarkdownLanguage, sample1);
|
2022-11-27 15:48:01 +08:00
|
|
|
// console.log("tree", JSON.stringify(tree, null, 2));
|
2022-10-17 21:48:21 +08:00
|
|
|
// Check if rendering back to text works
|
|
|
|
assertEquals(renderToText(tree), sample1);
|
2022-11-27 15:48:01 +08:00
|
|
|
|
|
|
|
// 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
|
2022-10-17 21:48:21 +08:00
|
|
|
let node = findNodeOfType(tree, "FrontMatter");
|
|
|
|
assertNotEquals(node, undefined);
|
2024-01-24 20:34:12 +08:00
|
|
|
tree = parse(extendedMarkdownLanguage, sampleInvalid1);
|
2022-10-17 21:48:21 +08:00
|
|
|
node = findNodeOfType(tree, "FrontMatter");
|
|
|
|
// console.log("Invalid node", node);
|
|
|
|
assertEquals(node, undefined);
|
|
|
|
});
|
2022-12-15 03:04:20 +08:00
|
|
|
|
2023-07-25 01:54:31 +08:00
|
|
|
const inlineAttributeSample = `
|
2023-07-26 23:12:56 +08:00
|
|
|
Hello there [a link](http://zef.plus)
|
|
|
|
[age: 100]
|
|
|
|
[age:: 200]
|
|
|
|
|
|
|
|
Here's a more [ambiguous: case](http://zef.plus)
|
|
|
|
|
|
|
|
And one with nested brackets: [array: [1, 2, 3]]
|
2023-07-25 01:54:31 +08:00
|
|
|
`;
|
|
|
|
|
|
|
|
Deno.test("Test inline attribute syntax", () => {
|
2024-01-24 20:34:12 +08:00
|
|
|
const tree = parse(extendedMarkdownLanguage, inlineAttributeSample);
|
2023-09-01 22:57:29 +08:00
|
|
|
// console.log("Attribute parsed", JSON.stringify(tree, null, 2));
|
2023-07-26 23:12:56 +08:00
|
|
|
const attributes = collectNodesOfType(tree, "Attribute");
|
|
|
|
let nameNode = findNodeOfType(attributes[0], "AttributeName");
|
2023-07-25 01:54:31 +08:00
|
|
|
assertEquals(nameNode?.children![0].text, "age");
|
2023-07-26 23:12:56 +08:00
|
|
|
let valueNode = findNodeOfType(attributes[0], "AttributeValue");
|
2023-07-25 01:54:31 +08:00
|
|
|
assertEquals(valueNode?.children![0].text, "100");
|
2023-07-26 23:12:56 +08:00
|
|
|
|
|
|
|
nameNode = findNodeOfType(attributes[1], "AttributeName");
|
|
|
|
assertEquals(nameNode?.children![0].text, "age");
|
|
|
|
valueNode = findNodeOfType(attributes[1], "AttributeValue");
|
|
|
|
assertEquals(valueNode?.children![0].text, "200");
|
|
|
|
|
|
|
|
nameNode = findNodeOfType(attributes[2], "AttributeName");
|
|
|
|
assertEquals(nameNode?.children![0].text, "array");
|
|
|
|
valueNode = findNodeOfType(attributes[2], "AttributeValue");
|
|
|
|
assertEquals(valueNode?.children![0].text, "[1, 2, 3]");
|
2023-07-25 01:54:31 +08:00
|
|
|
});
|
2023-09-01 22:57:29 +08:00
|
|
|
|
2024-02-03 02:19:07 +08:00
|
|
|
Deno.test("Test template directive parsing", () => {
|
|
|
|
const tree = parse(
|
|
|
|
extendedMarkdownLanguage,
|
|
|
|
"Simple {{name}} and {{count({ page })}}",
|
|
|
|
);
|
|
|
|
console.log("Template directive", JSON.stringify(tree, null, 2));
|
|
|
|
});
|
|
|
|
|
2023-09-01 22:57:29 +08:00
|
|
|
const multiStatusTaskExample = `
|
|
|
|
* [ ] Task 1
|
|
|
|
- [x] Task 2
|
|
|
|
* [TODO] Task 3
|
|
|
|
`;
|
|
|
|
|
|
|
|
Deno.test("Test multi-status tasks", () => {
|
2024-01-24 20:34:12 +08:00
|
|
|
const tree = parse(extendedMarkdownLanguage, multiStatusTaskExample);
|
2023-09-01 22:57:29 +08:00
|
|
|
// console.log("Tasks parsed", JSON.stringify(tree, null, 2));
|
|
|
|
const tasks = collectNodesOfType(tree, "Task");
|
|
|
|
assertEquals(tasks.length, 3);
|
|
|
|
// Check " " checkbox state parsing
|
|
|
|
assertEquals(tasks[0].children![0].children![1].text, " ");
|
|
|
|
assertEquals(tasks[1].children![0].children![1].text, "x");
|
|
|
|
assertEquals(tasks[2].children![0].children![1].text, "TODO");
|
|
|
|
});
|
2023-11-26 01:57:00 +08:00
|
|
|
|
|
|
|
const commandLinkSample = `
|
|
|
|
{[Some: Command]}
|
|
|
|
{[Other: Command|Alias]}
|
|
|
|
{[Command: Space | Spaces ]}
|
|
|
|
`;
|
|
|
|
|
|
|
|
Deno.test("Test command links", () => {
|
2024-01-24 20:34:12 +08:00
|
|
|
const tree = parse(extendedMarkdownLanguage, commandLinkSample);
|
2023-11-26 01:57:00 +08:00
|
|
|
const commands = collectNodesOfType(tree, "CommandLink");
|
2024-02-03 02:19:07 +08:00
|
|
|
// console.log("Command links parsed", JSON.stringify(commands, null, 2));
|
2023-11-26 01:57:00 +08:00
|
|
|
assertEquals(commands.length, 3);
|
|
|
|
assertEquals(commands[0].children![1].children![0].text, "Some: Command");
|
|
|
|
assertEquals(commands[1].children![1].children![0].text, "Other: Command");
|
|
|
|
assertEquals(commands[1].children![3].children![0].text, "Alias");
|
|
|
|
assertEquals(commands[2].children![1].children![0].text, "Command: Space ");
|
|
|
|
assertEquals(commands[2].children![3].children![0].text, " Spaces ");
|
|
|
|
});
|
|
|
|
|
|
|
|
const commandLinkArgsSample = `
|
|
|
|
{[Args: Command]("with", "args")}
|
|
|
|
{[Othargs: Command|Args alias]("other", "args", 123)}
|
|
|
|
`;
|
|
|
|
|
|
|
|
Deno.test("Test command link arguments", () => {
|
2024-01-24 20:34:12 +08:00
|
|
|
const tree = parse(extendedMarkdownLanguage, commandLinkArgsSample);
|
2023-11-26 01:57:00 +08:00
|
|
|
const commands = collectNodesOfType(tree, "CommandLink");
|
|
|
|
assertEquals(commands.length, 2);
|
|
|
|
|
2024-01-02 21:47:02 +08:00
|
|
|
const args1 = findNodeOfType(commands[0], "CommandLinkArgs");
|
2023-11-26 01:57:00 +08:00
|
|
|
assertEquals(args1!.children![0].text, '"with", "args"');
|
|
|
|
|
2024-01-02 21:47:02 +08:00
|
|
|
const args2 = findNodeOfType(commands[1], "CommandLinkArgs");
|
2023-11-26 01:57:00 +08:00
|
|
|
assertEquals(args2!.children![0].text, '"other", "args", 123');
|
2024-01-02 21:47:02 +08:00
|
|
|
});
|
2024-01-05 03:08:12 +08:00
|
|
|
|
2024-02-03 02:19:07 +08:00
|
|
|
Deno.test("Test directive parser", () => {
|
|
|
|
const simpleExample = `Simple {{.}}`;
|
|
|
|
let tree = parse(extendedMarkdownLanguage, simpleExample);
|
|
|
|
assertEquals(renderToText(tree), simpleExample);
|
|
|
|
|
|
|
|
const eachExample = `{{#each .}}Sup{{/each}}`;
|
|
|
|
tree = parse(extendedMarkdownLanguage, eachExample);
|
|
|
|
|
|
|
|
const ifExample = `{{#if true}}Sup{{/if}}`;
|
|
|
|
tree = parse(extendedMarkdownLanguage, ifExample);
|
|
|
|
assertEquals(renderToText(tree), ifExample);
|
|
|
|
|
|
|
|
const ifElseExample = `{{#if true}}Sup{{else}}Sup2{{/if}}`;
|
|
|
|
tree = parse(extendedMarkdownLanguage, ifElseExample);
|
|
|
|
assertEquals(renderToText(tree), ifElseExample);
|
|
|
|
console.log("Final tree", JSON.stringify(tree, null, 2));
|
|
|
|
|
|
|
|
const letExample = `{{#let @p = true}}{{/let}}`;
|
|
|
|
tree = parse(extendedMarkdownLanguage, letExample);
|
|
|
|
assertEquals(renderToText(tree), letExample);
|
2024-01-05 03:08:12 +08:00
|
|
|
});
|