Initial Commit
This commit is contained in:
73
packages/foundation/data/templates/src/presets/full.ts
Normal file
73
packages/foundation/data/templates/src/presets/full.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { createConditionalPlugin } from "../plugins/control/ConditionalPlugin.js";
|
||||
import { createLoopPlugin } from "../plugins/control/LoopPlugin.js";
|
||||
import { createCounterPlugin } from "../plugins/dynamic/CounterPlugin.js";
|
||||
import { createDateTimePlugin } from "../plugins/dynamic/DateTimePlugin.js";
|
||||
import { createUuidPlugin } from "../plugins/dynamic/UuidPlugin.js";
|
||||
import { createCaseTransformPlugin } from "../plugins/transform/CaseTransformPlugin.js";
|
||||
import { createRegexPlugin } from "../plugins/transform/RegexPlugin.js";
|
||||
import { createStringSplitPlugin } from "../plugins/transform/StringSplitPlugin.js";
|
||||
import { createArrayIndexPlugin } from "../plugins/variables/ArrayIndexPlugin.js";
|
||||
import { createDotNotationPlugin } from "../plugins/variables/DotNotationPlugin.js";
|
||||
import type {
|
||||
ParseState,
|
||||
PluginPreset,
|
||||
TemplateContext,
|
||||
} from "../types/index.js";
|
||||
|
||||
/**
|
||||
* Full preset — all plugins except the loop (loops require a parse-function
|
||||
* reference which is only available from the engine).
|
||||
*
|
||||
* Includes control-flow (conditional), dynamic variables (counter, datetime,
|
||||
* uuid), transforms (string-split, regex, case-transform), and variable
|
||||
* resolution (array-index, dot-notation).
|
||||
*/
|
||||
export function fullPreset<
|
||||
TContext extends TemplateContext = TemplateContext,
|
||||
>(): PluginPreset<TContext> {
|
||||
return {
|
||||
name: "full",
|
||||
plugins: [
|
||||
createConditionalPlugin<TContext>(),
|
||||
createCounterPlugin<TContext>(),
|
||||
createDateTimePlugin<TContext>(),
|
||||
createUuidPlugin<TContext>(),
|
||||
createStringSplitPlugin<TContext>(),
|
||||
createRegexPlugin<TContext>(),
|
||||
createCaseTransformPlugin<TContext>(),
|
||||
createArrayIndexPlugin<TContext>(),
|
||||
createDotNotationPlugin<TContext>(),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Full preset with loop support.
|
||||
*
|
||||
* The `parseFunction` is the engine's internal sync renderer, injected so
|
||||
* the loop plugin can recursively render each iteration body.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const engine = new TemplateEngine<MyCtx>();
|
||||
* engine.usePreset(
|
||||
* fullPresetWithLoops((tpl, ctx, state) => engine["parseInternal"](tpl, ctx, state))
|
||||
* );
|
||||
* ```
|
||||
*/
|
||||
export function fullPresetWithLoops<
|
||||
TContext extends TemplateContext = TemplateContext,
|
||||
>(
|
||||
parseFunction: (
|
||||
template: string,
|
||||
context: TContext,
|
||||
state: ParseState
|
||||
) => string
|
||||
): PluginPreset<TContext> {
|
||||
const base = fullPreset<TContext>();
|
||||
return {
|
||||
...base,
|
||||
name: "full-with-loops",
|
||||
plugins: [createLoopPlugin<TContext>(parseFunction), ...base.plugins],
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user