Returns the byte length of the wikitext.
The same result can be obtained by using:
Mwbot.String.byteLength(wikitext.content);
Returns the wikitext content of the instance.
Returns the length of the wikitext.
The same result can be obtained by using:
wikitext.content.length;
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'
// }
Any (markup) object containing startIndex
and endIndex
properties.
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.
The start index of the expression.
The exclusive end index of the expression.
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>
}
content
or parsed expressions made before calling this method should not
be reused, as properties such as startIndex
will change after modification.The type of expressions to modify.
Type | First argument of modificationPredicate |
---|---|
tags | Tag |
parameters | Parameter |
sections | Section |
templates | ParsedTemplate, ParsedParserFunction, or RawTemplate |
wikilinks | ParsedWikilink, ParsedFileWikilink, or ParsedRawWikilink |
A function that processes expression objects and returns a string or null
.
null
means no modification is applied to that expression.The modified wikitext content.
Modifies {{{parameter}}}
markups in the wikitext content.
This is a shorthand method of modify with its first argument set as parameters
.
Modifies sections in the wikitext content.
This is a shorthand method of modify with its first argument set as sections
.
Modifies tags in the wikitext content.
This is a shorthand method of modify with its first argument set as tags
.
Modifies {{template}}
markups in the wikitext content.
This is a shorthand method of modify with its first argument set as templates
.
Modifies [[wikilink]]
markups in the wikitext content.
This is a shorthand method of modify with its first argument set as wikilinks
.
Parses the wikitext content for {{{parameter}}}
markups.
Optional
config: ParseParametersConfigConfig to filter the output.
An array of parsed parameters.
Parses the wikitext content for sections.
Optional
config: ParseSectionsConfigConfig to filter the output.
An array of parsed sections.
Parses the wikitext content for HTML tags.
Optional
config: ParseTagsConfigConfig to filter the output.
An array of Tag objects.
Parses the wikitext content for {{template}}
markups.
This method parses any double-braced markups, including magic words and parser functions.
Optional
config: ParseTemplatesConfigConfig to filter the output.
An array of parsed templates.
Parses the wikitext content for [[wikilink]]
markups in the wikitext.
Optional
config: ParseWikilinksConfigConfig to filter the output.
An array of parsed wikilinks.
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:<tag></tag>
: parseTags, modifyTags== section ==
: parseSections, modifySections{{{parameter}}}
: parseParameters, modifyParameters{{template}}
: parseTemplates, modifyTemplates[[wikilink]]
: parseWikilinks, modifyWikilinksThe modification methods are centralized in modify, which applies a transformation callback to the output of a parser method.