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

    Interface Wikitext

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

    Wikitext instances primarily provide parsing and modification methods:

    The modification methods are centralized in modify, which applies a transformation callback to the output of a parser method.

    interface Wikitext {
        get byteLength(): number;
        get content(): string;
        get length(): number;
        identifySection(
            obj: { endIndex: number; startIndex: number } & Record<string, any>,
        ): null | Section;
        identifySection(startIndex: number, endIndex: number): null | Section;
        modify<K extends keyof ModificationMap>(
            type: K,
            modificationPredicate: ModificationPredicate<ModificationMap[K]>,
        ): string;
        modifyParameters(
            modificationPredicate: ModificationPredicate<Parameter>,
        ): string;
        modifySections(
            modificationPredicate: ModificationPredicate<Section>,
        ): string;
        modifyTags(modificationPredicate: ModificationPredicate<Tag>): string;
        modifyTemplates(
            modificationPredicate: ModificationPredicate<DoubleBracedClasses>,
        ): string;
        modifyWikilinks(
            modificationPredicate: ModificationPredicate<DoubleBracketedClasses>,
        ): string;
        parseParameters(config?: ParseParametersConfig): Parameter[];
        parseSections(config?: ParseSectionsConfig): Section[];
        parseTags(config?: ParseTagsConfig): Tag[];
        parseTemplates(config?: ParseTemplatesConfig): DoubleBracedClasses[];
        parseWikilinks(config?: ParseWikilinksConfig): DoubleBracketedClasses[];
    }
    Index

    Accessors

    • get byteLength(): number

      Returns the byte length of the wikitext.

      The same result can be obtained by using:

      Mwbot.String.byteLength(wikitext.content);
      

      Returns number

    • get content(): string

      Returns the wikitext content of the instance.

      Returns string

    • get length(): number

      Returns the length of the wikitext.

      The same result can be obtained by using:

      wikitext.content.length;
      

      Returns number

    Methods

    • Identifies the section containing an expression based on its start and end indices.

      Example:

      const text =
      `== Foo ==
      === Bar ===
      [[Main page]]
      == Baz ==
      [[Another page]]`;

      const wikitext = new mwbot.Wikitext(text);
      const [main] = wikitext.parseWikilinks();
      console.log(wikitext.identifySection(main));
      // Output:
      // {
      // heading: '=== Bar ===',
      // title: 'Bar',
      // level: 3,
      // index: 2,
      // startIndex: 10,
      // endIndex: 36,
      // text: '=== Bar ===\n[[Main page]]\n'
      // }

      Parameters

      • obj: { endIndex: number; startIndex: number } & Record<string, any>

        Any (markup) object containing startIndex and endIndex properties.

      Returns null | Section

      The deepest Section containing the expression, or null if none is found.

    • Identifies the section containing an expression based on its start and end indices.

      This method is intended to be used with expressions obtained from parser methods.

      Parameters

      • startIndex: number

        The start index of the expression.

      • endIndex: number

        The exclusive end index of the expression.

      Returns null | Section

      The deepest Section containing the expression, or null if none is found.

    • Modifies a specific type of expression in the wikitext content.

      This method extracts expressions of the given type, applies the modificationPredicate to transform them, and updates the wikitext accordingly.

      const wikitext = new mwbot.Wikitext('<span>a<div><del>b</span><span>c');
      const oldContent = wikitext.content;
      const newContent = wikitext.modify('tags', (tag) => {
      if (tag.unclosed && !tag.skip) {
      // If this tag is unclosed, append its expected end tag to the tag text.
      // `Tag` objects with the `unclosed` property set to `true` have their
      // expected end tag stored in the `end` property.
      // In most cases, the `skip` property should be guaranteed to be `false`
      // to ensure we're not modifying special cases such as
      // "<nowiki><noinclude></nowiki>".
      return tag.text + tag.end; // Returning a string applies the modification.
      } else {
      return null; // Returning `null` means no modification is made.
      }
      });

      if (oldContent !== newContent) {
      console.log(newContent);
      // Output: <span>a<div><del>b</del></div></span><span>c</span>
      }
      • This method (and its shorthand variants) modifies and updates content and its associated expressions.
      • Any copies of content or parsed expressions made before calling this method should not be reused, as properties such as startIndex will change after modification.

      Type Parameters

      Parameters

      Returns string

      The modified wikitext content.

      • If type is invalid.
      • If modificationPredicate is not a function.
      • If the array created from modificationPredicate contains values other than strings or null.