diff --git a/src/services/tocs.ts b/src/services/tocs.ts index ee609a20..80772da8 100644 --- a/src/services/tocs.ts +++ b/src/services/tocs.ts @@ -272,6 +272,8 @@ function _liquidSubstitutions(input: string, vars: Record, path: */ function _replaceIncludes(items: YfmToc[], tocDir: string, sourcesDir: string, vars: Record) { return items.reduce((acc, item) => { + let includedInlineItems: YfmToc[] | null = null; + if (item.name) { const tocPath = join(tocDir, 'toc.yaml'); @@ -301,7 +303,11 @@ function _replaceIncludes(items: YfmToc[], tocDir: string, sourcesDir: string, v /* Save the path to exclude toc from the output directory in the next step */ includedTocPaths.add(includeTocPath); - item.items = (item.items || []).concat(includeToc.items); + if (item.name) { + item.items = (item.items || []).concat(includeToc.items); + } else { + includedInlineItems = includeToc.items; + } } catch (err) { log.error(`Error while including toc: ${bold(includeTocPath)} to ${bold(join(tocDir, 'toc.yaml'))}`); return acc; @@ -314,7 +320,11 @@ function _replaceIncludes(items: YfmToc[], tocDir: string, sourcesDir: string, v item.items = _replaceIncludes(item.items, tocDir, sourcesDir, vars); } - return acc.concat(item); + if (includedInlineItems) { + return acc.concat(includedInlineItems); + } else { + return acc.concat(item); + } }, [] as YfmToc[]); } diff --git a/tests/include-toc.test.ts b/tests/include-toc.test.ts index efdad2e0..1c371a6b 100644 --- a/tests/include-toc.test.ts +++ b/tests/include-toc.test.ts @@ -1,7 +1,7 @@ import {compareDirectories, runYfmDocs, getTestPaths} from './utils'; describe('Include toc', () => { - test('Test1', () => { + test('Toc is included in link mode', () => { const testRootPath = './tests/mocks/include-toc/test1'; const {inputPath, outputPath, expectedOutputPath} = getTestPaths(testRootPath); @@ -17,4 +17,21 @@ describe('Include toc', () => { expect(expectedContent).toEqual(outputContent); } }); + + test('Toc is included inline, not as a new section', () => { + const testRootPath = './tests/mocks/include-toc/test2'; + const {inputPath, outputPath, expectedOutputPath} = getTestPaths(testRootPath); + + runYfmDocs(inputPath, outputPath); + + 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/test2/expected-output/file1.md b/tests/mocks/include-toc/test2/expected-output/file1.md new file mode 100644 index 00000000..a7c58a36 --- /dev/null +++ b/tests/mocks/include-toc/test2/expected-output/file1.md @@ -0,0 +1 @@ +# File 1 diff --git a/tests/mocks/include-toc/test2/expected-output/fileX.md b/tests/mocks/include-toc/test2/expected-output/fileX.md new file mode 100644 index 00000000..2da6cbb7 --- /dev/null +++ b/tests/mocks/include-toc/test2/expected-output/fileX.md @@ -0,0 +1 @@ +# File X diff --git a/tests/mocks/include-toc/test2/expected-output/folder/fileA.md b/tests/mocks/include-toc/test2/expected-output/folder/fileA.md new file mode 100644 index 00000000..eb88b4f4 --- /dev/null +++ b/tests/mocks/include-toc/test2/expected-output/folder/fileA.md @@ -0,0 +1 @@ +# File A diff --git a/tests/mocks/include-toc/test2/expected-output/folder/fileB.md b/tests/mocks/include-toc/test2/expected-output/folder/fileB.md new file mode 100644 index 00000000..16e71803 --- /dev/null +++ b/tests/mocks/include-toc/test2/expected-output/folder/fileB.md @@ -0,0 +1 @@ +# File B diff --git a/tests/mocks/include-toc/test2/expected-output/toc.yaml b/tests/mocks/include-toc/test2/expected-output/toc.yaml new file mode 100644 index 00000000..59cf2325 --- /dev/null +++ b/tests/mocks/include-toc/test2/expected-output/toc.yaml @@ -0,0 +1,10 @@ +items: + - name: Name1 + href: file1.md + - name: NameA + href: folder/fileA.md + - name: NameB + href: folder/fileB.md + - name: NameX + href: fileX.md +base: . diff --git a/tests/mocks/include-toc/test2/input/file1.md b/tests/mocks/include-toc/test2/input/file1.md new file mode 100644 index 00000000..a7c58a36 --- /dev/null +++ b/tests/mocks/include-toc/test2/input/file1.md @@ -0,0 +1 @@ +# File 1 diff --git a/tests/mocks/include-toc/test2/input/fileX.md b/tests/mocks/include-toc/test2/input/fileX.md new file mode 100644 index 00000000..2da6cbb7 --- /dev/null +++ b/tests/mocks/include-toc/test2/input/fileX.md @@ -0,0 +1 @@ +# File X diff --git a/tests/mocks/include-toc/test2/input/folder/fileA.md b/tests/mocks/include-toc/test2/input/folder/fileA.md new file mode 100644 index 00000000..eb88b4f4 --- /dev/null +++ b/tests/mocks/include-toc/test2/input/folder/fileA.md @@ -0,0 +1 @@ +# File A diff --git a/tests/mocks/include-toc/test2/input/folder/fileB.md b/tests/mocks/include-toc/test2/input/folder/fileB.md new file mode 100644 index 00000000..16e71803 --- /dev/null +++ b/tests/mocks/include-toc/test2/input/folder/fileB.md @@ -0,0 +1 @@ +# File B diff --git a/tests/mocks/include-toc/test2/input/folder/toc.yaml b/tests/mocks/include-toc/test2/input/folder/toc.yaml new file mode 100644 index 00000000..386f0690 --- /dev/null +++ b/tests/mocks/include-toc/test2/input/folder/toc.yaml @@ -0,0 +1,5 @@ +items: + - name: NameA + href: fileA.md + - name: NameB + href: fileB.md diff --git a/tests/mocks/include-toc/test2/input/toc.yaml b/tests/mocks/include-toc/test2/input/toc.yaml new file mode 100644 index 00000000..7d74bd84 --- /dev/null +++ b/tests/mocks/include-toc/test2/input/toc.yaml @@ -0,0 +1,6 @@ +items: + - name: Name1 + href: file1.md + - include: { mode: link, path: folder/toc.yaml } + - name: NameX + href: fileX.md