Skip to content

Commit

Permalink
feat: add included ignore includes (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
makamekm authored Oct 22, 2024
1 parent f564c24 commit 1495b47
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
39 changes: 16 additions & 23 deletions src/transform/plugins/includes/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ function processRecursive(
includePath: string,
targetDestPath: string,
options: IncludeCollectOpts,
appendix: Map<string, string>,
) {
const {path, log, copyFile, includedParentPath: includedParentPathNullable, included} = options;
const includedParentPath = includedParentPathNullable || path;
Expand All @@ -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 %}`,
);
Expand All @@ -59,11 +54,7 @@ function processRecursive(
}
}

function collectRecursive(
result: string,
options: IncludeCollectOpts,
appendix: Map<string, string>,
) {
function collectRecursive(result: string, options: IncludeCollectOpts) {
const {root, path, destPath = '', log, singlePage} = options;

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

includesPaths.push(includePath);

processRecursive(includePath, targetDestPath, options, appendix);
processRecursive(includePath, targetDestPath, options);

includesPaths.pop();
}
Expand All @@ -109,14 +100,16 @@ function collectRecursive(
}

function collect(input: string, options: IncludeCollectOpts) {
const appendix: Map<string, string> = 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');
}
}

Expand Down
1 change: 1 addition & 0 deletions src/transform/plugins/includes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export type IncludeCollectOpts = MarkdownItPluginOpts & {
included: Boolean;
includedParentPath?: string;
additionalIncludedList?: string[];
appendix?: Map<string, string>;
};
2 changes: 1 addition & 1 deletion src/transform/preprocessors/included/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 1495b47

Please sign in to comment.