Major directive refactor (#195)

Fixes #188 #144 #76: major refactor of directive parsing, rendering, styling
This commit is contained in:
Zef Hemel
2022-12-14 20:04:20 +01:00
committed by GitHub
parent 79e1151ee6
commit aaebea5e54
44 changed files with 656 additions and 347 deletions
+25
View File
@@ -0,0 +1,25 @@
import { renderToText } from "./tree.ts";
import wikiMarkdownLang from "../../common/markdown_parser/parser.ts";
import { assert, assertEquals } from "../../test_deps.ts";
import { parse } from "../../common/markdown_parser/parse_tree.ts";
import { removeQueries } from "./query.ts";
const queryRemovalTest = `
# Heading
Before
<!-- #query page -->
Bla bla remove me
<!-- /query -->
End
`;
Deno.test("White out queries", () => {
const lang = wikiMarkdownLang([]);
const mdTree = parse(lang, queryRemovalTest);
removeQueries(mdTree);
const text = renderToText(mdTree);
// Same length? We should be good
assertEquals(text.length, queryRemovalTest.length);
assert(text.indexOf("remove me") === -1);
console.log("Whited out text", text);
});
+9 -28
View File
@@ -3,6 +3,7 @@ import {
collectNodesMatching,
ParseTree,
renderToText,
replaceNodesMatching,
} from "$sb/lib/tree.ts";
export const queryRegex =
@@ -134,35 +135,15 @@ export function applyQuery<T>(parsedQuery: ParsedQuery, records: T[]): T[] {
}
export function removeQueries(pt: ParseTree) {
addParentPointers(pt);
collectNodesMatching(pt, (t) => {
if (t.type !== "CommentBlock") {
return false;
replaceNodesMatching(pt, (t) => {
if (t.type !== "Directive") {
return;
}
const text = t.children![0].text!;
const match = directiveStartRegex.exec(text);
if (!match) {
return false;
}
const directiveType = match[1];
const parentChildren = t.parent!.children!;
const index = parentChildren.indexOf(t);
const nodesToReplace: ParseTree[] = [];
for (let i = index + 1; i < parentChildren.length; i++) {
const n = parentChildren[i];
if (n.type === "CommentBlock") {
const text = n.children![0].text!;
const match = directiveEndRegex.exec(text);
if (match && match[1] === directiveType) {
break;
}
}
nodesToReplace.push(n);
}
const renderedText = nodesToReplace.map(renderToText).join("");
parentChildren.splice(index + 1, nodesToReplace.length, {
const renderedText = renderToText(t);
return {
from: t.from,
to: t.to,
text: new Array(renderedText.length + 1).join(" "),
});
return true;
};
});
}
+3 -3
View File
@@ -8,9 +8,9 @@ import {
renderToText,
replaceNodesMatching,
} from "./tree.ts";
import wikiMarkdownLang from "../../common/parser.ts";
import wikiMarkdownLang from "../../common/markdown_parser/parser.ts";
import { assertEquals, assertNotEquals } from "../../test_deps.ts";
import { parse } from "../../common/parse_tree.ts";
import { parse } from "../../common/markdown_parser/parse_tree.ts";
const mdTest1 = `
# Heading
@@ -47,7 +47,7 @@ name: something
\`\`\`
`;
Deno.test("Run a Node sandbox", () => {
Deno.test("Test parsing", () => {
const lang = wikiMarkdownLang([]);
const mdTree = parse(lang, mdTest1);
addParentPointers(mdTree);
+9
View File
@@ -148,3 +148,12 @@ export function renderToText(tree: ParseTree): string {
}
return pieces.join("");
}
export function cloneTree(tree: ParseTree): ParseTree {
const newTree = { ...tree };
if (tree.children) {
newTree.children = tree.children.map(cloneTree);
}
delete newTree.parent;
return newTree;
}