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

44 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

import "./syscall_mock.ts";
import { parse } from "$common/markdown_parser/parse_tree.ts";
2024-08-22 02:13:40 +08:00
import {
cleanAttributes,
extractAttributes,
} from "@silverbulletmd/silverbullet/lib/attribute";
2024-07-30 23:24:17 +08:00
import { assertEquals } from "@std/assert";
2024-02-29 22:23:05 +08:00
import { renderToText } from "./tree.ts";
import { extendedMarkdownLanguage } from "$common/markdown_parser/parser.ts";
2023-07-25 01:54:31 +08:00
const inlineAttributeSample = `
# My document
2023-07-26 23:12:56 +08:00
Top level attributes: [name:: sup] [age:: 42] [children: [pete, "john", mary]]
2023-07-25 01:54:31 +08:00
* [ ] Attribute in a task [tag:: foo]
* Regular item [tag:: bar]
1. Itemized list [tag:: baz]
`;
const cleanedInlineAttributeSample = `
# My document
2023-07-26 23:12:56 +08:00
Top level attributes:
2023-07-25 01:54:31 +08:00
2024-08-22 02:13:40 +08:00
* [ ] Attribute in a task
* Regular item
2023-07-25 01:54:31 +08:00
2024-08-22 02:13:40 +08:00
1. Itemized list
2023-07-25 01:54:31 +08:00
`;
2023-07-26 23:12:56 +08:00
Deno.test("Test attribute extraction", async () => {
2024-01-24 20:34:12 +08:00
const tree = parse(extendedMarkdownLanguage, inlineAttributeSample);
2024-08-22 02:13:40 +08:00
const toplevelAttributes = await extractAttributes(["test"], tree);
2023-07-26 23:12:56 +08:00
// console.log("All attributes", toplevelAttributes);
2023-07-25 01:54:31 +08:00
assertEquals(toplevelAttributes.name, "sup");
assertEquals(toplevelAttributes.age, 42);
2023-07-26 23:12:56 +08:00
assertEquals(toplevelAttributes.children, ["pete", "john", "mary"]);
2023-07-25 01:54:31 +08:00
// Check if the attributes are still there
assertEquals(renderToText(tree), inlineAttributeSample);
2024-08-22 02:13:40 +08:00
// And now clean
cleanAttributes(tree);
2023-07-25 01:54:31 +08:00
assertEquals(renderToText(tree), cleanedInlineAttributeSample);
});