-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove duplicated code, unify includes.
- Loading branch information
Showing
4 changed files
with
73 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import path from 'node:path'; | ||
import { handleTemplateFile } from './transforms/template-data.js'; | ||
|
||
/** | ||
* Language-agnostic include method to handle includes in sissi | ||
* @param {string} inputContent the content | ||
* @param {string} inputPath the full path to the file processed | ||
* @param {SissiConfig} config the configuration | ||
* @param {any} data the data object | ||
* @param {string} includePath the include path to be used. If nullish, it's relative to the inputPath | ||
* @param {Regexp} regex the regular expression to match include directives | ||
* @param {(context, ...args: string[]) => string} replaceFunction the callback function of the string.replace | ||
* @returns {string} the contents with the includes resolved | ||
*/ | ||
export async function handleIncludes( | ||
content, inputPath, config, data, includePath, regex, replaceFunction) { | ||
const includes = new Map(); | ||
let parsed = path.parse(inputPath); | ||
const matches = Array.from(content.matchAll(regex)); | ||
for (const [, file] of matches) { | ||
if (includes.has(file)) { | ||
continue; | ||
} | ||
const _dependants = data?._dependants instanceof Array ? data._dependants.slice(0) : [inputPath]; | ||
const includeFile = path.join(includePath ?? parsed.dir, file); | ||
if (_dependants.includes(includeFile)) { | ||
console.warn(`Skipping Circular include detected in ${inputPath}: ${includeFile}`); | ||
continue; | ||
} | ||
_dependants.push(file); | ||
const include = await handleTemplateFile(config, {_dependants, ...(data ?? {})}, includeFile); | ||
includes.set(file, include); | ||
} | ||
|
||
const defaultReplaceFunction = (context, _, file) => { | ||
return context.includes.get(file)?.content ?? `<!-- missing include: ${file} -->`; | ||
}; | ||
|
||
const replaceContext = { | ||
inputPath, | ||
includes, | ||
}; | ||
|
||
return content.replace(regex, | ||
(...args) => (replaceFunction ?? defaultReplaceFunction)(replaceContext, ...args) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters