1 import { describe, expect, it } from "vitest"
2 import { getCleanMd } from "../get-clean-md.js"
3 import { globalConfig } from "../global-config.js"
4
5 const clean = (content: string) =>
6 getCleanMd({
7 file: content,
8 type: "content",
9 })
10
11 describe("getCleanMd", () => {
12 describe("MDX expressions", () => {
13 it("resolves expressions referencing the docs config", async () => {
14 const md = await clean(
15 `Medusa's current version is v{config.version.number}.`
16 )
17
18 expect(md.trim()).toBe(
19 `Medusa's current version is v${globalConfig.version!.number}.`
20 )
21 })
22
23 it("resolves expressions referencing constants exported by the page", async () => {
24 const md = await clean(
25 [
26 "export const releaseNoteText = `Read the [Release Notes](${config.version.releaseUrl}).`",
27 "",
28 "{releaseNoteText}",
29 ].join("\n")
30 )
31
32 expect(md.trim()).toBe(
33 `Read the [Release Notes](${globalConfig.version!.releaseUrl}).`
34 )
35 })
36
37 it("resolves constants declared after their usage", async () => {
38 const md = await clean(
39 ["Hello {name}!", "", 'export const name = "World"'].join("\n")
40 )
41
42 expect(md.trim()).toBe("Hello World!")
43 })
44
45 it("resolves values passed through the scope option", async () => {
46 const withScope = await getCleanMd({
47 file: `The answer is {custom.answer}.`,
48 type: "content",
49 scope: { custom: { answer: 42 } },
50 })
51
52 expect(withScope.trim()).toBe("The answer is 42.")
53 })
54
55 it("removes expressions that can't be resolved instead of leaking them", async () => {
56 const md = await clean(`Version {someUnknownVariable} here.`)
57
58 expect(md).not.toContain("someUnknownVariable")
59 expect(md).not.toContain("{")
60 })
61
62 it("removes MDX comments", async () => {
63 const md = await clean(["{/* a comment */}", "", "Content."].join("\n"))
64
65 expect(md.trim()).toBe("Content.")
66 })
67
68 it("resolves the page title in the main heading", async () => {
69 const md = await clean(
70 [
71 "export const metadata = {",
72 " title: `${pageNumber} Updating Medusa`,",
73 "}",
74 "",
75 "# {metadata.title}",
76 ].join("\n")
77 )
78
79 expect(md.trim()).toBe("# Updating Medusa")
80 })
81 })
82
83 describe("code block meta", () => {
84 it("removes UI-only meta from code blocks", async () => {
85 const md = await clean(
86 ["```bash npx2yarn", "npx medusa -v", "```"].join("\n")
87 )
88
89 expect(md.trim()).toBe(["```bash", "npx medusa -v", "```"].join("\n"))
90 })
91
92 it("keeps the title meta", async () => {
93 const md = await clean(
94 [
95 '```ts title="src/jobs/hello-world.ts" highlights={highlights}',
96 "const a = 1",
97 "```",
98 ].join("\n")
99 )
100
101 expect(md.trim()).toBe(
102 ['```ts title="src/jobs/hello-world.ts"', "const a = 1", "```"].join(
103 "\n"
104 )
105 )
106 })
107 })
108 describe("injected Markdown components", () => {
109 it("replaces the component with the passed Markdown", async () => {
110 const content = [
111 "## Cloud Plans",
112 "",
113 "- Deploy from [GitHub](https://docs.medusajs.com/cloud/projects)",
114 "",
115 "| Feature | Develop | Launch |",
116 "| --- | --- | --- |",
117 "| Orders | Unlimited | 1<br />2 |",
118 ].join("\n")
119
120 const md = await getCleanMd({
121 file: ["# Pricing", "", "<PricingContent />"].join("\n"),
122 type: "content",
123 parserOptions: { PricingContent: { content } },
124 })
125
126 expect(md.trim()).toBe(["# Pricing", "", content].join("\n"))
127 })
128
129 it("removes the component when no Markdown is passed", async () => {
130 const md = await clean(["# Pricing", "", "<PricingContent />"].join("\n"))
131
132 expect(md.trim()).toBe("# Pricing")
133 })
134 })
135 })