diff --git a/src/transform/plugins/includes/collect.ts b/src/transform/plugins/includes/collect.ts index 17b06eb3..d5522443 100644 --- a/src/transform/plugins/includes/collect.ts +++ b/src/transform/plugins/includes/collect.ts @@ -12,7 +12,6 @@ function processRecursive( includePath: string, targetDestPath: string, options: IncludeCollectOpts, - appendix: Map, ) { const {path, log, copyFile, includedParentPath: includedParentPathNullable, included} = options; const includedParentPath = includedParentPathNullable || path; @@ -34,20 +33,16 @@ function processRecursive( const includedRelativePath = getRelativePath(includedParentPath, includePath); // The appendix is the map that protects from multiple include files - if (!appendix.has(includedRelativePath)) { + if (!options.appendix?.has(includedRelativePath)) { // Recursive function to include the depth structure - const includeContent = collectRecursive( - content, - { - ...options, - path: includePath, - includedParentPath, - }, - appendix, - ); + const includeContent = collectRecursive(content, { + ...options, + path: includePath, + includedParentPath, + }); // Add to appendix set structure - appendix.set( + options.appendix?.set( includedRelativePath, `{% included (${includedRelativePath}) %}\n${includeContent}\n{% endincluded %}`, ); @@ -59,11 +54,7 @@ function processRecursive( } } -function collectRecursive( - result: string, - options: IncludeCollectOpts, - appendix: Map, -) { +function collectRecursive(result: string, options: IncludeCollectOpts) { const {root, path, destPath = '', log, singlePage} = options; const INCLUDE_REGEXP = /{%\s*include\s*(notitle)?\s*\[(.+?)]\((.+?)\)\s*%}/g; @@ -100,7 +91,7 @@ function collectRecursive( includesPaths.push(includePath); - processRecursive(includePath, targetDestPath, options, appendix); + processRecursive(includePath, targetDestPath, options); includesPaths.pop(); } @@ -109,14 +100,16 @@ function collectRecursive( } function collect(input: string, options: IncludeCollectOpts) { - const appendix: Map = new Map(); + const shouldWriteAppendix = !options.appendix; + + options.appendix = options.appendix ?? new Map(); - input = collectRecursive(input, options, appendix); + input = collectRecursive(input, options); - if (!options.path.includes('_includes')) { + if (shouldWriteAppendix) { // Appendix should be appended to the end of the file (it supports depth structure, so the included files will have included as well) - if (appendix.size > 0) { - input += '\n' + [...appendix.values()].join('\n'); + if (options.appendix.size > 0) { + input += '\n' + [...options.appendix.values()].join('\n'); } } diff --git a/src/transform/plugins/includes/types.ts b/src/transform/plugins/includes/types.ts index 66eff785..341af7d8 100644 --- a/src/transform/plugins/includes/types.ts +++ b/src/transform/plugins/includes/types.ts @@ -14,4 +14,5 @@ export type IncludeCollectOpts = MarkdownItPluginOpts & { included: Boolean; includedParentPath?: string; additionalIncludedList?: string[]; + appendix?: Map; }; diff --git a/src/transform/preprocessors/included/index.ts b/src/transform/preprocessors/included/index.ts index 1fef568d..b068f538 100644 --- a/src/transform/preprocessors/included/index.ts +++ b/src/transform/preprocessors/included/index.ts @@ -74,7 +74,7 @@ const index: MarkdownItPreprocessorCb<{ // To reduce file reading we can include the file content into the generated content if (included) { - const lines = input.split('\n') || []; + const lines = input?.split('\n') || []; // The finction reads the files from bottom to top(!). It stops the loop if it does not have anything to swap. // If the function finds something to process then it restarts the loop because the position of the last element has been moved.