Fixes #529 by removing directives (#613)

* Fixes #529 by removing directives
* Load builtin tags on space reindex
This commit is contained in:
Zef Hemel
2024-01-02 14:47:02 +01:00
committed by GitHub
parent 5f4e584e46
commit 8a2e081672
46 changed files with 159 additions and 1445 deletions
+6 -7
View File
@@ -1,17 +1,16 @@
import { parse } from "../../common/markdown_parser/parse_tree.ts";
import buildMarkdown from "../../common/markdown_parser/parser.ts";
import { AST, findNodeOfType, parseTreeToAST } from "$sb/lib/tree.ts";
import { AST, parseTreeToAST } from "$sb/lib/tree.ts";
import { assertEquals } from "../../test_deps.ts";
import { astToKvQuery } from "$sb/lib/parse-query.ts";
const lang = buildMarkdown([]);
import { languageFor } from "../../common/languages.ts";
function wrapQueryParse(query: string): AST | null {
const tree = parse(lang, `<!-- #query ${query} -->\n$\n<!-- /query -->`);
return parseTreeToAST(findNodeOfType(tree, "Query")!);
const tree = parse(languageFor("query")!, query);
// console.log("tree", tree);
return parseTreeToAST(tree.children![0]);
}
Deno.test("Test directive parser", () => {
Deno.test("Test query parser", () => {
// const query = ;
// console.log("query", query);
assertEquals(
-23
View File
@@ -1,23 +0,0 @@
import { renderToText } from "./tree.ts";
import { assert, assertEquals } from "../../test_deps.ts";
import { removeQueries } from "./query.ts";
import { parseMarkdown } from "$sb/lib/test_utils.ts";
const queryRemovalTest = `
# Heading
Before
<!-- #query page -->
Bla bla remove me
<!-- /query -->
End
`;
Deno.test("White out queries", () => {
const mdTree = parseMarkdown(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);
});
-20
View File
@@ -1,12 +1,6 @@
import { ParseTree, renderToText, replaceNodesMatching } from "$sb/lib/tree.ts";
import { FunctionMap, KV, Query, QueryExpression } from "$sb/types.ts";
export const queryRegex =
/(<!--\s*#query\s+(.+?)-->)(.+?)(<!--\s*\/query\s*-->)/gs;
export const directiveStartRegex = /<!--\s*#([\w\-]+)\s+(.+?)-->/s;
export const directiveEndRegex = /<!--\s*\/([\w\-]+)\s*-->/s;
export function evalQueryExpression(
val: QueryExpression,
obj: any,
@@ -246,17 +240,3 @@ export function applyQueryNoFilterKV(
}
return allItems;
}
export function removeQueries(pt: ParseTree) {
replaceNodesMatching(pt, (t) => {
if (t.type !== "Directive") {
return;
}
const renderedText = renderToText(t);
return {
from: t.from,
to: t.to,
text: new Array(renderedText.length + 1).join(" "),
};
});
}
+12 -18
View File
@@ -49,16 +49,13 @@ Deno.test("Test rewritePageRefs", () => {
let tree = parseMarkdown(`
This is a [[local link]] and [[local link|with alias]].
<!-- #query page render [[template/page]] -->
<!-- /query -->
\`\`\`query
page render [[template/page]]
\`\`\`
<!-- #use [[template/use-template]] {} -->
<!-- /use -->
<!-- #include [[template/include-template]] {} -->
<!-- /include -->
\`\`\`template
page: "[[template/use-template]]"
\`\`\`
`);
rewritePageRefs(tree, "!silverbullet.md");
let rewrittenText = renderToText(tree);
@@ -68,16 +65,13 @@ This is a [[local link]] and [[local link|with alias]].
`
This is a [[!silverbullet.md/local link]] and [[!silverbullet.md/local link|with alias]].
<!-- #query page render [[!silverbullet.md/template/page]] -->
<!-- /query -->
\`\`\`query
page render [[!silverbullet.md/template/page]]
\`\`\`
<!-- #use [[!silverbullet.md/template/use-template]] {} -->
<!-- /use -->
<!-- #include [[!silverbullet.md/template/include-template]] {} -->
<!-- /include -->
\`\`\`template
page: "[[!silverbullet.md/template/use-template]]"
\`\`\`
`,
);
-23
View File
@@ -49,29 +49,6 @@ export function isFederationPath(path: string) {
export function rewritePageRefs(tree: ParseTree, containerPageName: string) {
traverseTree(tree, (n): boolean => {
if (n.type === "DirectiveStart") {
const pageRef = findNodeOfType(n, "PageRef")!;
if (pageRef) {
const pageRefName = pageRef.children![0].text!.slice(2, -2);
pageRef.children![0].text = `[[${
resolvePath(containerPageName, pageRefName)
}]]`;
}
const directiveText = n.children![0].text;
// #use or #import
if (directiveText) {
const match = /\[\[(.+)\]\]/.exec(directiveText);
if (match) {
const pageRefName = match[1];
n.children![0].text = directiveText.replace(
match[0],
`[[${resolvePath(containerPageName, pageRefName)}]]`,
);
}
}
return true;
}
if (n.type === "FencedCode") {
const codeInfo = findNodeOfType(n, "CodeInfo");
if (!codeInfo) {