Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add included ignore includes #542

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading