import { isTagNode } from "@bbob/plugin-helper"; import type { BBobCoreTagNodeTree, BBobPluginOptions, PresetExtendCallback, PresetFactory, PresetOptions, PresetTagsDefinition, ProcessorFunction } from "@bbob/types"; export function process( tags: Tags, tree: BBobCoreTagNodeTree, core: BBobPluginOptions, options: Options ) { return tree.walk((node) => { if (isTagNode(node)) { const tag = node.tag; const tagCallback = tags[tag]; if (typeof tagCallback === "function") { return tagCallback(node, core, options); } } return node; }); } /** * Create a preset plugin for @bbob/core */ function createPreset( defTags: Tags, processor: ProcessorFunction = process ) { const presetFactory: PresetFactory = (opts?: Options) => { presetFactory.options = Object.assign(presetFactory.options || {}, opts); function presetExecutor( tree: BBobCoreTagNodeTree, core: BBobPluginOptions ) { return processor(defTags, tree, core, presetFactory.options || {}); } presetExecutor.options = presetFactory.options as Options; return presetExecutor; }; presetFactory.extend = function presetExtend( callback: PresetExtendCallback ) { const newTags = callback(defTags, presetFactory.options) return createPreset(newTags, processor as unknown as ProcessorFunction); }; return presetFactory; } export { createPreset }; export default createPreset;