Skip to content

Commit 1495b47

Browse files
authored
feat: add included ignore includes (#542)
1 parent f564c24 commit 1495b47

File tree

3 files changed

+18
-24
lines changed

3 files changed

+18
-24
lines changed

src/transform/plugins/includes/collect.ts

+16-23
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ function processRecursive(
1212
includePath: string,
1313
targetDestPath: string,
1414
options: IncludeCollectOpts,
15-
appendix: Map<string, string>,
1615
) {
1716
const {path, log, copyFile, includedParentPath: includedParentPathNullable, included} = options;
1817
const includedParentPath = includedParentPathNullable || path;
@@ -34,20 +33,16 @@ function processRecursive(
3433
const includedRelativePath = getRelativePath(includedParentPath, includePath);
3534

3635
// The appendix is the map that protects from multiple include files
37-
if (!appendix.has(includedRelativePath)) {
36+
if (!options.appendix?.has(includedRelativePath)) {
3837
// Recursive function to include the depth structure
39-
const includeContent = collectRecursive(
40-
content,
41-
{
42-
...options,
43-
path: includePath,
44-
includedParentPath,
45-
},
46-
appendix,
47-
);
38+
const includeContent = collectRecursive(content, {
39+
...options,
40+
path: includePath,
41+
includedParentPath,
42+
});
4843

4944
// Add to appendix set structure
50-
appendix.set(
45+
options.appendix?.set(
5146
includedRelativePath,
5247
`{% included (${includedRelativePath}) %}\n${includeContent}\n{% endincluded %}`,
5348
);
@@ -59,11 +54,7 @@ function processRecursive(
5954
}
6055
}
6156

62-
function collectRecursive(
63-
result: string,
64-
options: IncludeCollectOpts,
65-
appendix: Map<string, string>,
66-
) {
57+
function collectRecursive(result: string, options: IncludeCollectOpts) {
6758
const {root, path, destPath = '', log, singlePage} = options;
6859

6960
const INCLUDE_REGEXP = /{%\s*include\s*(notitle)?\s*\[(.+?)]\((.+?)\)\s*%}/g;
@@ -100,7 +91,7 @@ function collectRecursive(
10091

10192
includesPaths.push(includePath);
10293

103-
processRecursive(includePath, targetDestPath, options, appendix);
94+
processRecursive(includePath, targetDestPath, options);
10495

10596
includesPaths.pop();
10697
}
@@ -109,14 +100,16 @@ function collectRecursive(
109100
}
110101

111102
function collect(input: string, options: IncludeCollectOpts) {
112-
const appendix: Map<string, string> = new Map();
103+
const shouldWriteAppendix = !options.appendix;
104+
105+
options.appendix = options.appendix ?? new Map();
113106

114-
input = collectRecursive(input, options, appendix);
107+
input = collectRecursive(input, options);
115108

116-
if (!options.path.includes('_includes')) {
109+
if (shouldWriteAppendix) {
117110
// Appendix should be appended to the end of the file (it supports depth structure, so the included files will have included as well)
118-
if (appendix.size > 0) {
119-
input += '\n' + [...appendix.values()].join('\n');
111+
if (options.appendix.size > 0) {
112+
input += '\n' + [...options.appendix.values()].join('\n');
120113
}
121114
}
122115

src/transform/plugins/includes/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export type IncludeCollectOpts = MarkdownItPluginOpts & {
1414
included: Boolean;
1515
includedParentPath?: string;
1616
additionalIncludedList?: string[];
17+
appendix?: Map<string, string>;
1718
};

src/transform/preprocessors/included/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const index: MarkdownItPreprocessorCb<{
7474

7575
// To reduce file reading we can include the file content into the generated content
7676
if (included) {
77-
const lines = input.split('\n') || [];
77+
const lines = input?.split('\n') || [];
7878

7979
// The finction reads the files from bottom to top(!). It stops the loop if it does not have anything to swap.
8080
// If the function finds something to process then it restarts the loop because the position of the last element has been moved.

0 commit comments

Comments
 (0)