Skip to content

Commit

Permalink
DOCSTOOLS-842: support including toc inline, not as new section
Browse files Browse the repository at this point in the history
  • Loading branch information
yndx-birman committed Nov 22, 2021
1 parent 872103a commit ed9dbcc
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/services/tocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ function _liquidSubstitutions(input: string, vars: Record<string, string>, path:
*/
function _replaceIncludes(items: YfmToc[], tocDir: string, sourcesDir: string, vars: Record<string, string>) {
return items.reduce((acc, item) => {
let includedInlineItems: YfmToc[] | null = null;

if (item.name) {
const tocPath = join(tocDir, 'toc.yaml');

Expand Down Expand Up @@ -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;
Expand All @@ -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[]);
}

Expand Down
19 changes: 18 additions & 1 deletion tests/include-toc.test.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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);
}
});
});
1 change: 1 addition & 0 deletions tests/mocks/include-toc/test2/expected-output/file1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# File 1
1 change: 1 addition & 0 deletions tests/mocks/include-toc/test2/expected-output/fileX.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# File X
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# File A
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# File B
10 changes: 10 additions & 0 deletions tests/mocks/include-toc/test2/expected-output/toc.yaml
Original file line number Diff line number Diff line change
@@ -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: .
1 change: 1 addition & 0 deletions tests/mocks/include-toc/test2/input/file1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# File 1
1 change: 1 addition & 0 deletions tests/mocks/include-toc/test2/input/fileX.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# File X
1 change: 1 addition & 0 deletions tests/mocks/include-toc/test2/input/folder/fileA.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# File A
1 change: 1 addition & 0 deletions tests/mocks/include-toc/test2/input/folder/fileB.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# File B
5 changes: 5 additions & 0 deletions tests/mocks/include-toc/test2/input/folder/toc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
items:
- name: NameA
href: fileA.md
- name: NameB
href: fileB.md
6 changes: 6 additions & 0 deletions tests/mocks/include-toc/test2/input/toc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
items:
- name: Name1
href: file1.md
- include: { mode: link, path: folder/toc.yaml }
- name: NameX
href: fileX.md

0 comments on commit ed9dbcc

Please sign in to comment.