Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tests/unit/pandoc-partition.test.ts
6448 views
1
/*
2
* pandoc-partition.test.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*
6
*/
7
import { assert } from "testing/asserts";
8
import { Metadata } from "../../src/config/types.ts";
9
import { languagesWithClasses, partitionMarkdown } from "../../src/core/pandoc/pandoc-partition.ts";
10
import { unitTest } from "../test.ts";
11
12
// deno-lint-ignore require-await
13
unitTest("partitionYaml", async () => {
14
const frontMatter = "---\ntitle: foo\n---";
15
const headingText = "## Hello World {#cool .foobar foo=bar}";
16
const markdown = "\n\nThis is a paragraph\n\n:::{#refs}\n:::\n";
17
18
const markdownstr = `${frontMatter}\n${headingText}${markdown}`;
19
const partmd = partitionMarkdown(markdownstr);
20
21
const metadataMatches = (yaml?: Metadata) => {
22
if (yaml) {
23
return yaml.title === "foo" && Object.keys(yaml).length === 1;
24
} else {
25
return false;
26
}
27
};
28
29
// Tests of the result
30
assert(partmd.containsRefs, "Refs Div not found");
31
assert(partmd.markdown === markdown, "Partitioned markdown doesn't match");
32
assert(
33
metadataMatches(partmd.yaml),
34
"Partitioned front matter doesn't match",
35
);
36
assert(
37
partmd.headingText === "Hello World",
38
"Heading text not parsed properly",
39
);
40
assert(
41
partmd.headingAttr?.id === "cool",
42
"Heading missing id",
43
);
44
assert(
45
partmd.headingAttr?.classes.includes("foobar"),
46
"Heading missing class",
47
);
48
assert(
49
partmd.headingAttr?.keyvalue[0][0] === "foo",
50
"Heading missing attribute key",
51
);
52
assert(
53
partmd.headingAttr?.keyvalue[0][1] === "bar",
54
"Heading missing attribute value",
55
);
56
});
57
58
// deno-lint-ignore require-await
59
unitTest("languagesWithClasses - dot-joined syntax", async () => {
60
const md = `\`\`\`{python.marimo}
61
x = 1
62
\`\`\`
63
64
\`\`\`{python .foo}
65
y = 2
66
\`\`\`
67
`;
68
const result = languagesWithClasses(md);
69
// {python.marimo} → language "python.marimo", no class
70
assert(result.has("python.marimo"), "Should have language 'python.marimo'");
71
assert(result.get("python.marimo") === undefined, "python.marimo should have no class");
72
// {python .foo} → language "python", class "foo"
73
assert(result.has("python"), "Should have language 'python'");
74
assert(result.get("python") === "foo", "python should have class 'foo'");
75
});
76
77