Class ParsedTemplate

Class used by Wikitext.parseTemplates.

Hierarchy

Constructors

Properties

_endIndex: number

CAUTION: Pseudo-private property. Use getEndIndex to get this property's value.

The index up to, but not including, the end of the template in the wikitext out of which the template was parsed.

Note that this property is made private-like because it shouldn't be modified externally, but sometimes Wikitext.parseTemplates needs to modify this property, from outside this class.

_startIndex: number

CAUTION: Pseudo-private property. Use getStartIndex to get this property's value.

The index to the start of the template in the wikitext out of which the template was parsed.

Note that this property is made private-like because it shouldn't be modified externally, but sometimes Wikitext.parseTemplates needs to modify this property, from outside this class.

The arguments of the template parsed as an array of objects.

cleanName: string

name formatted by mw.Title.newFromText.

fullCleanName: string

cleanName with redundancies as in fullName.

fullName: string

Full string that fits into the first slot of the template (i.e. {{fullName}}). May be accompanied by additional characters that are not relevant to the title of the page to be transcluded.

hierarchy: string[][]

Argument hierarchies.

keys: string[]

An array of the names of the template arguments.

name: string

The trimmed name passed to the constructor.

nestLevel: number

The nest level of the template. If not nested by other templates, the value is 0.

originalText: string

The original text of the template.

overriddenArgs: TemplateArgument[]

The overridden arguments of the template stored as an array of objects.

Methods

  • Delete a template argument.

    Parameters

    • name: string

    Returns boolean

    true if the instance has an argument with the specified name and if the argument is successfully removed, false otherwise.

  • Get the name of the template.

    Parameters

    • Optional prop: "full" | "clean" | "fullclean"

      By default, returns the original, unformatted name passed to Template.constructor.

      • If full is passed, returns fullName passed to Template.constructor (same as name if none was passed).
      • If clean is passed, returns name that is formatted.
      • If fullclean is passed, returns name that is formatted and accompanied by redundancies as in fullName.

      In specifying any of the above, the first letter is capitalized.

      Note that if name is prefixed by Template:, the namespace prefix is truncated in prop=clean and prop=fullclean.

      // name: Template:test
      const template = new Template('Template:test');
      console.log(template.getName()): // Template:test
      console.log(template.getName('full')): // Template:test
      console.log(template.getName('clean')): // Test
      console.log(template.getName('fullclean')): // Test

      For the clean names, namespace aliases are formatted to their canonical ones.

      // name: project:test', fullName: '<!--change?-->project:test
      const template = new Template('project:test', {fullName: '<!--change?-->project:test'});
      console.log(template.getName()): // project:test
      console.log(template.getName('full')): // <!--change?-->project:test
      console.log(template.getName('clean')): // Wikipedia:Test
      console.log(template.getName('fullclean')): // <!--change?-->Wikipedia:Test

    Returns string

  • Get the nest level of the template.

    Returns number

  • Check whether the Template instance has an argument with a certain name.

    Parameters

    • name: string | RegExp

      Name of the argument to search for.

    • Optional options: GetArgOptions

      Optional search options.

    Returns boolean

    A boolean value in accordance with whether there is a match.

  • Render the Template instance as wikitext.

    Use render({nameprop: 'full', unformatted: 'both'}) for an output that is closest to the original configurations.

    Parameters

    • Optional options: RenderOptions

      Optional object of rendering specifications

    Returns string

  • Render the original template text.

    Returns string

  • Find the original template in a wikitext and replace it with the (updated) template obtained by render. This method is supposed to be called on a wiktiext same as the one from which the ParsedTemplate instance was parsed and initialized.

    Note that if this method is called recursively against an array of ParsedTemplate, the looped array needs to be reversed so that the replacement takes place from the bottom of the wikitext. This is because the method reads the start and end indexes of the original template before the replacement (unless useIndex is set to false), and if the replacement is done in a top-down fashion, the indexes change and the subsequent replacements are affected.

    Parameters

    • wikitext: string

      Wikitext in which to search for the original template.

    • Optional options: RenderOptions & ReplaceInOptions

      Optional object to specify rendering and replacement options.

    Returns string

    New wikitext with the original template replaced. (Could be the same as the input wikitext if the replacement didn't take place.)

  • Set (or update) arguments in(to) the Template instance. This method does not leave a log when argument override takes place.

    Note: New arguments are simply newly added, just as when addArgs is used.

    Parameters

    • newArgs: NewArg[]

      An array of {name: string; value: string;} objects.

    Returns Template

  • Get class properties in a JSON format.

    Returns TemplateJSON & {
        endIndex: number;
        nestLevel: number;
        originalText: string;
        startIndex: number;
    }

Generated using TypeDoc