diff --git a/src/services/tocs.ts b/src/services/tocs.ts index ef188559..b414f534 100644 --- a/src/services/tocs.ts +++ b/src/services/tocs.ts @@ -50,7 +50,7 @@ async function add(path: string) { ...vars, }; - if (typeof parsedToc.title === 'string') { + if (parsedToc.title) { parsedToc.title = firstFilterTextItems( parsedToc.title, combinedVars, diff --git a/tests/e2e/include-toc.test.ts b/tests/e2e/include-toc.test.ts index da276966..f669a8ed 100644 --- a/tests/e2e/include-toc.test.ts +++ b/tests/e2e/include-toc.test.ts @@ -50,4 +50,29 @@ describe('Include toc', () => { expect(expectedContent).toEqual(outputContent); } }); + + test('Toc with expressions', () => { + const testRootPath = 'mocks/include-toc/test4'; + const {inputPath, outputPath, expectedOutputPath} = getTestPaths(testRootPath); + + const vars = { + type: 'a', + a: 'A', + b: 'B', + }; + + runYfmDocs(inputPath, outputPath, { + args: `--vars="${JSON.stringify(vars).replace(/(")/g, '\\$1')}"`, + }); + + const compareResult = compareDirectories(outputPath, expectedOutputPath); + + if (typeof compareResult === 'boolean') { + expect(true).toEqual(compareResult); + } else { + const {expectedContent, outputContent} = compareResult; + + expect(expectedContent).toEqual(outputContent); + } + }); }); diff --git a/tests/mocks/include-toc/test4/expected-output/a1.md b/tests/mocks/include-toc/test4/expected-output/a1.md new file mode 100644 index 00000000..ec2a6cff --- /dev/null +++ b/tests/mocks/include-toc/test4/expected-output/a1.md @@ -0,0 +1 @@ +This is the /a1.md file content. diff --git a/tests/mocks/include-toc/test4/expected-output/index.yaml b/tests/mocks/include-toc/test4/expected-output/index.yaml new file mode 100644 index 00000000..d9ac6a5f --- /dev/null +++ b/tests/mocks/include-toc/test4/expected-output/index.yaml @@ -0,0 +1,9 @@ +title: Title A +description: + - Desc A +meta: + title: Meta A +links: + - title: A1 + description: A1 desc + href: a1 diff --git a/tests/mocks/include-toc/test4/expected-output/toc.yaml b/tests/mocks/include-toc/test4/expected-output/toc.yaml new file mode 100644 index 00000000..fd3fa268 --- /dev/null +++ b/tests/mocks/include-toc/test4/expected-output/toc.yaml @@ -0,0 +1,6 @@ +title: Title A +href: index.yaml +items: + - name: A1 + href: a1.md +base: . diff --git a/tests/mocks/include-toc/test4/input/a1.md b/tests/mocks/include-toc/test4/input/a1.md new file mode 100644 index 00000000..ec2a6cff --- /dev/null +++ b/tests/mocks/include-toc/test4/input/a1.md @@ -0,0 +1 @@ +This is the /a1.md file content. diff --git a/tests/mocks/include-toc/test4/input/index.yaml b/tests/mocks/include-toc/test4/input/index.yaml new file mode 100644 index 00000000..8b20bb3c --- /dev/null +++ b/tests/mocks/include-toc/test4/input/index.yaml @@ -0,0 +1,20 @@ +title: + - text: "Title {% if type == 'a' %}{{ a }}{% else %}{{ b }}{% endif %}" + when: type == "a" + - text: "Title {% if type == 'a' %}{{ a }}{% else %}{{ b }}{% endif %}" + when: type == "b" +description: + - text: "Desc {{ a }}" + when: type == "a" + - text: "Desc {{ b }}" + when: type == "b" +meta: + title: + - text: "Meta {{ a }}" + when: type == "a" + - text: "Meta {{ b }}" + when: type == "b" +links: + - title: A1 + description: A1 desc + href: a1 diff --git a/tests/mocks/include-toc/test4/input/toc.yaml b/tests/mocks/include-toc/test4/input/toc.yaml new file mode 100644 index 00000000..f74cb826 --- /dev/null +++ b/tests/mocks/include-toc/test4/input/toc.yaml @@ -0,0 +1,9 @@ +title: + - text: "Title {% if type == 'a' %}{{ a }}{% else %}{{ b }}{% endif %}" + when: type == "a" + - text: "Title {% if type == 'a' %}{{ a }}{% else %}{{ b }}{% endif %}" + when: type == "b" +href: index.yaml +items: + - name: A1 + href: a1.md