1
0
silverbullet/plug-api/lib/attribute.test.ts

42 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-07-26 15:12:56 +00:00
import "$sb/lib/syscall_mock.ts";
2023-07-24 17:54:31 +00:00
import { parse } from "../../common/markdown_parser/parse_tree.ts";
import buildMarkdown from "../../common/markdown_parser/parser.ts";
import { extractAttributes } from "$sb/lib/attribute.ts";
import { assertEquals } from "../../test_deps.ts";
import { renderToText } from "$sb/lib/tree.ts";
const inlineAttributeSample = `
# My document
2023-07-26 15:12:56 +00:00
Top level attributes: [name:: sup] [age:: 42] [children: [pete, "john", mary]]
2023-07-24 17:54:31 +00:00
* [ ] Attribute in a task [tag:: foo]
* Regular item [tag:: bar]
1. Itemized list [tag:: baz]
`;
const cleanedInlineAttributeSample = `
# My document
2023-07-26 15:12:56 +00:00
Top level attributes:
2023-07-24 17:54:31 +00:00
* [ ] Attribute in a task [tag:: foo]
* Regular item [tag:: bar]
1. Itemized list [tag:: baz]
`;
2023-07-26 15:12:56 +00:00
Deno.test("Test attribute extraction", async () => {
2023-07-24 17:54:31 +00:00
const lang = buildMarkdown([]);
const tree = parse(lang, inlineAttributeSample);
2023-07-26 15:12:56 +00:00
const toplevelAttributes = await extractAttributes(tree, false);
// console.log("All attributes", toplevelAttributes);
2023-07-24 17:54:31 +00:00
assertEquals(toplevelAttributes.name, "sup");
assertEquals(toplevelAttributes.age, 42);
2023-07-26 15:12:56 +00:00
assertEquals(toplevelAttributes.children, ["pete", "john", "mary"]);
2023-07-24 17:54:31 +00:00
// Check if the attributes are still there
assertEquals(renderToText(tree), inlineAttributeSample);
// Now once again with cleaning
2023-07-26 15:12:56 +00:00
await extractAttributes(tree, true);
2023-07-24 17:54:31 +00:00
assertEquals(renderToText(tree), cleanedInlineAttributeSample);
});