mwbot-ts - v1.2.7
    Preparing search index...

    Interface ParsedTemplate

    The instance members of the ParsedTemplate class. For static members, see ParsedTemplateStatic (defined separately due to TypeScript limitations).

    interface ParsedTemplate {
        children: Set<number>;
        endIndex: number;
        index: number;
        nestLevel: number;
        params: Record<string, TemplateParameter>;
        parent: null | number;
        rawTitle: string;
        skip: boolean;
        startIndex: number;
        text: string;
        title: Title;
        deleteParam(key: string, resolveHierarchy?: boolean): boolean;
        getParam(key: string, resolveHierarchy?: boolean): null | TemplateParameter;
        hasParam(key: string | RegExp, value?: string | RegExp): boolean;
        hasParam(predicate: (param: TemplateParameter) => boolean): boolean;
        insertParam(
            key: string,
            value: string,
            overwrite?: boolean,
            position?: { before: string } | { after: string } | "start" | "end",
        ): this;
        setTitle(title: string | Title, verbose?: boolean): boolean;
        stringify(options?: ParsedTemplateOutputConfig): string;
        toParserFunction(
            title: string | Title,
            verbose?: boolean,
        ): null | ParsedParserFunction;
        toString(): string;
        updateParam(key: string, value: string): this;
    }

    Hierarchy (View Summary)

    Index

    Properties

    children: Set<number>

    The indices of the child objects within the parseTemplates result array.

    endIndex: number

    The ending index of this double-braced markup in the wikitext (exclusive).

    index: number

    The index of this object within the result array returned by parseTemplates.

    nestLevel: number

    The nesting level of this double-braced markup. 0 if not nested within another double-braced expression.

    params: Record<string, TemplateParameter>

    The template's parameters.

    This property is read-only. To update it, use insertParam, updateParam, or deleteParam as per your needs.

    parent: null | number

    The index of the parent object within the parseTemplates result array, or null if there is no parent.

    rawTitle: string

    The raw template title, as directly parsed from the first operand of a {{template|...}} expression.

    skip: boolean

    Whether the double-braced markup appears inside an HTML tag specified in SkipTags.

    startIndex: number

    The starting index of this double-braced markup in the wikitext.

    text: string

    The original text of the double-braced markup parsed from the wikitext. The value of this property is static.

    title: Title

    The template's title.

    This property is read-only. To update it, use setTitle.

    Methods

    • Deletes a parameter from the template.

      Parameters

      • key: string

        The parameter key to delete.

      • OptionalresolveHierarchy: boolean

        Whether to consider hierarchies when resolving the key. If true, this method checks the params object for any parameter whose key is an alias of key and deletes it if found. (Default: false)

      Returns boolean

      true if a matching parameter was deleted; otherwise, false.

    • Gets a parameter object by key.

      Caution: The returned object is mutable.

      Parameters

      • key: string

        The parameter key.

      • OptionalresolveHierarchy: boolean

        Whether to consider hierarchies when searching for a matching parameter. If true, this method first checks whether key belongs to a hierarchy array. If params contains a parameter with a higher-priority key in that hierarchy, that parameter is returned instead. (Default: false).

        Example:

        • Given key = '1', hierarchies = [['1', 'user', 'User']], and params containing a parameter keyed 'user', this method returns the 'user' parameter.

        If key does not belong to any hierarchy array, the method behaves the same as when resolveHierarchy is false. This also applies if no template parameter hierarchies have been provided via the constructor or ParseTemplatesConfig.hierarchies.

      Returns null | TemplateParameter

      The parameter object if found; otherwise, null.

    • Checks if a template parameter with the specified key exists, optionally matching its value.

      Parameters

      • key: string | RegExp

        The parameter key to match, either as an exact string or a regular expression.

      • Optionalvalue: string | RegExp

        The optional value matcher.

        • If a string, checks for an exact value match.
        • If a regular expression, tests the parameter value against the pattern.
        • If omitted, only the parameter key is checked.

      Returns boolean

      true if a matching parameter exists; otherwise, false.

      hasParam("user"); // Checks if a parameter named "user" exists.
      hasParam(/^data-/); // Checks for parameters starting with "data-".
      hasParam("id", "123"); // Checks if "id" exists and equals "123".
    • Checks if a template parameter exists based on a custom predicate function.

      Parameters

      • predicate: (param: TemplateParameter) => boolean

        A function that tests each parameter.

      Returns boolean

      true if a matching parameter exists; otherwise, false.

      hasParam((param) => param.key.startsWith("meta") && param.value.length > 10);
      
    • Inserts a template parameter into the list.

      By default, if the parameter is new:

      • It is inserted at the end of the list.
      • If position is specified, the insertion position is controlled accordingly.

      If a parameter with the same key already exists:

      • If overwrite is true (default):
        • If position is not specified, the parameter’s value is updated in place (its position remains unchanged).
        • If position is specified, the parameter’s value is updated and moved to the specified position.
      • If overwrite is false, the existing parameter is left unchanged and no insertion occurs.

      If parameter hierarchies are set, key is automatically resolved to the higher-priority key, if applicable.

      Note that the order of parameters in the final output can also be controlled via stringify, using TemplateOutputConfig.sortPredicate, regardless of the insertion order tracked by this instance.

      Parameters

      • key: string

        The key of the parameter. This can be an empty string for unnamed parameters.

      • value: string

        The value of the parameter.

      • Optionaloverwrite: boolean

        Whether to overwrite an existing parameter if it exists. (Default: true)

      • Optionalposition: { before: string } | { after: string } | "start" | "end"

        Where to insert or move the parameter:

        • 'start': Insert at the beginning.
        • 'end': Insert at the end (default for new parameters).
        • { before: referenceKey }: Insert before the given key.
        • { after: referenceKey }: Insert after the given key.

        If referenceKey does not exist:

        • For new parameters, insertion falls back to 'end'.
        • For existing parameters, the value is updated in place without moving it (i.e., behaves as if position is not specified).

        When specifying a referenceKey, it is best practice to verify its existence beforehand to ensure expected behavior. See hasParam for the relevant utility.

      Returns this

      The current instance for chaining.

    • Sets a new title to the instance.

      Parameters

      • title: string | Title

        The new title to set.

      • Optionalverbose: boolean

        Whether to log errors. (Default: false)

      Returns boolean

      A boolean indicating whether the new title was set.

    • Converts the instance to a ParsedParserFunction.

      This method should only be used for ParsedTemplate instances that represent invalid parser functions (e.g., {{if:1|1|2}}, where "if:" is not a valid function hook and is therefore recognized as part of a template title).

      The conversion is based on the data used to initialize this instance, and any modifications made after initialization will be discarded. Therefore, this method should be called before making any changes to the instance properties.

      Parameters

      • title: string | Title

        The parser function hook to convert this title to, including a trailing colon character (e.g., "#if:"; see also ParserFunctionStatic.verify). If a Title instance is passed, the output of title.getPrefixedDb({ colon: true, fragment: true }) is validated.

        When passing a string, it can (and should) include the function’s first parameter (e.g., "#if:1"). The second and subsequent parameters are initialized based on params.

      • Optionalverbose: boolean

        Whether to log errors (default: false).

      Returns null | ParsedParserFunction

      A new ParsedParserFunction instance on success; otherwise, null.

    • Updates the value of an existing parameter without changing its position.

      • If no parameter with the given key exists, this method does nothing.
      • If parameter hierarchies are set, key is automatically resolved to the higher-priority key, if applicable.

      This is functionally equivalent to calling insertParam with overwrite = true and no position, except that it performs no operation if the parameter does not already exist.

      Use this method when you want to safely update a parameter only if it exists, without affecting the order of parameters or accidentally inserting new ones.

      Parameters

      • key: string

        The key of the parameter. This can be an empty string for unnamed parameters.

      • value: string

        The new value of the parameter.

      Returns this

      The current instance for chaining.