### AstRange startContainer Example (JavaScript)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/AstRange-(EN)
Demonstrates how to use the `setStart` method to define the start point of an AstRange and how the `startContainer` property reflects the node containing the start of the range. This property is read-only and returns an AstNode.
```javascript
var root = Parser.parse('[[a]]bc'),
{lastChild} = root,
range = root.createRange();
assert.equal(lastChild, 'bc');
range.setStart(root, 1);
assert.strictEqual(range.startContainer, root);
range.setStart(lastChild, 0);
assert.strictEqual(range.startContainer, lastChild);
```
--------------------------------
### Example CLI command to get parser config
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/README.md
An example demonstrating how to use the `getParserConfig` CLI command to fetch the parser configuration for the Japanese Wikipedia.
```sh
npx getParserConfig jawiki https://ja.wikipedia.org/w user@example.net
```
--------------------------------
### AstRange startPos Example (JavaScript)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/AstRange-(EN)
Demonstrates the `startPos` property, which returns the row and column coordinates of the start point. The example uses `setStart` to position the range and then asserts the expected `{top, left}` object returned by `startPos`. This property is read-only.
```javascript
var root = Parser.parse('[[a]]\nb'),
{lastChild} = root,
range = root.createRange();
assert.equal(lastChild, '\nb');
range.setStart(root, 1);
assert.deepStrictEqual(range.startPos, {top: 0, left: 5});
range.setStart(lastChild, 1);
assert.deepStrictEqual(range.startPos, {top: 1, left: 0});
```
--------------------------------
### AstRange startIndex Example (JavaScript)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/AstRange-(EN)
Shows the usage of the `startIndex` property, representing the absolute position of the start point within the entire AST. The example demonstrates how `setStart` affects the `startIndex` value, which is a read-only number.
```javascript
var root = Parser.parse('[[a]]bc'),
{lastChild} = root,
range = root.createRange();
assert.equal(lastChild, 'bc');
range.setStart(root, 1);
assert.strictEqual(range.startIndex, 5);
range.setStart(lastChild, 1);
assert.strictEqual(range.startIndex, 6);
```
--------------------------------
### Wikitext to HTML Conversion (5-Quote Opening Sequence)
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/quotes.txt
Presents the HTML output for wikitext starting with five quotes, demonstrating the combined effect of italics and bold formatting. This example shows how the parser interprets sequences of five quotes.
```wikitext
'''''foo''
```
```html
foo
```
--------------------------------
### AstRange startOffset Example (JavaScript)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/AstRange-(EN)
Illustrates the `startOffset` property, which indicates the position within the `startContainer`. The example shows how `setStart` updates both the container and the offset, and how `startOffset` reflects this change. It is a read-only numeric property.
```javascript
var root = Parser.parse('[[a]]bc'),
{lastChild} = root,
range = root.createRange();
assert.equal(lastChild, 'bc');
range.setStart(root, 1);
assert.strictEqual(range.startOffset, 1);
range.setStart(lastChild, 1);
assert.strictEqual(range.startOffset, 1);
```
--------------------------------
### Wikitext Examples for Syntax-Like Rule
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/syntax-like
Demonstrates correct Wikitext syntax that the rule might flag by default. This includes examples of headings, redirects, and lists.
```wikitext
Foo
==Bar
```
```wikitext
Foo
==Bar[//example.com ==]
```
```wikitext
Foo
#redirect
:[[Bar]]
```
--------------------------------
### Install WikiLint using npm
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/README.md
Installs the WikiLint package, a lighter version of WikiParser-Node focused on CLI and linting functionalities. It does not allow AST modification.
```sh
npm i wikilint
```
--------------------------------
### Wikitext Reference Parsing Example
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/subReferencingTests.txt
Demonstrates the wikitext syntax for defining references with details and names, and how they are structured before parsing. This serves as the input for the parsing logic.
```wikitext
[The book]
```
--------------------------------
### Get First Child Node (Node.js)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/nodeLike
Demonstrates how to retrieve the first child node of an element using the nodeLike mixin in Node.js. It requires the 'nodeLike' mixin and asserts the first child's value.
```javascript
var {nodeLike} = require('../mixin/nodeLike');
var NodeLike, token;
class S {
childNodes = ['a', 'b'];
}
NodeLike = nodeLike(S);
token = new NodeLike();
assert.strictEqual(token.firstChild, 'a');
```
--------------------------------
### Get Element Width (Node.js)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/nodeLike
Demonstrates obtaining the offset width of an element in Node.js via the nodeLike mixin. It requires the 'nodeLike' mixin and the getDimension method, asserting the width value.
```javascript
var {nodeLike} = require('../mixin/nodeLike');
var NodeLike, token;
class S {
getDimension() {
return {width: 1};
}
}
NodeLike = nodeLike(S);
token = new NodeLike();
assert.strictEqual(token.offsetWidth, 1);
```
--------------------------------
### Install WikiParser-Node or WikiLint (Node.js)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/Home-(EN)
Installs the WikiParser-Node or WikiLint package using npm. These packages are used for parsing Wikitext in a Node.js environment.
```sh
npm i wikiparser-node
```
```sh
npm i wikilint
```
--------------------------------
### Render Wiki Text with Script Elements to HTML (PHP)
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/headings.txt
This example demonstrates the rendering of wiki text containing script elements (Script[edit]
```
--------------------------------
### Wikitext to HTML Conversion (Italics and Possessives)
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/quotes.txt
Demonstrates the conversion of wikitext containing italics and possessives into HTML. This example highlights how specific wikitext markup is translated into corresponding HTML tags, such as for italics.
```wikitext
The first monolingual dictionary written in a Romance language was ''Sebastián Covarrubias''' ''Tesoro de la lengua castellana o española'', published in 1611 in Madrid. In 1612 the first edition of the ''Vocabolario dell'Accademia della Crusca'', for Italian, was published. In 1690 in Rotterdam was published, posthumously, the ''Dictionnaire Universel''.
```
```html
The first monolingual dictionary written in a Romance language was Sebastián Covarrubias' Tesoro de la lengua castellana o española, published in 1611 in Madrid. In 1612 the first edition of the Vocabolario dell'Accademia della Crusca, for Italian, was published. In 1690 in Rotterdam was published, posthumously, the Dictionnaire Universel.
```
```html/parsoid
The first monolingual dictionary written in a Romance language was Sebastián Covarrubias' Tesoro de la lengua castellana o española, published in 1611 in Madrid. In 1612 the first edition of the Vocabolario dell'Accademia della Crusca, for Italian, was published. In 1690 in Rotterdam was published, posthumously, the Dictionnaire Universel.
```
--------------------------------
### Get sortkey of CategoryLink
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/CategoryToken-(EN)
Illustrates how to get the sort key of a category link. The sort key is read-only in the Browser version. This example shows retrieving the sort key for different category link formats.
```javascript
// sortkey (print)
var {
firstChild,
lastChild,
} = Parser.parse('[[category:a|<|>]][[category:b]]');
assert.strictEqual(firstChild.sortkey, '<|]');
assert.strictEqual(lastChild.sortkey, undefined);
```
--------------------------------
### Initialize and Use RedirectMap in JavaScript
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/RedirectMap
Demonstrates the initialization and usage of the RedirectMap class in JavaScript. It shows how to create instances with different input types (object, Map) and case sensitivity settings, and how to add and retrieve key-value pairs. Asserts are used to verify the expected behavior.
```javascript
var {RedirectMap} = require('../lib/redirectMap');
var a = new RedirectMap({a: 'b'}),
b = new RedirectMap(new Map([['a', 'b']])),
c = new RedirectMap({a: 'b'}, false),
d = new RedirectMap(new Map([['a', 'b']]), false);
assert.deepStrictEqual([...a], [['A', 'B']]);
assert.deepStrictEqual([...b], [['A', 'B']]);
assert.deepStrictEqual([...c], [['A', 'b']]);
assert.deepStrictEqual([...d], [['A', 'b']]);
a.set('c', 'd');
assert.deepStrictEqual([...a], [['A', 'B'], ['C', 'D']]);
c.set('c', 'd');
assert.deepStrictEqual([...c], [['A', 'b'], ['C', 'd']]);
```
--------------------------------
### Get Element Height (Node.js)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/nodeLike
Shows how to get the offset height of an element in Node.js using the nodeLike mixin. This functionality relies on the 'nodeLike' mixin and the getDimension method, asserting the height value.
```javascript
var {nodeLike} = require('../mixin/nodeLike');
var NodeLike, token;
class S {
getDimension() {
return {height: 1};
}
}
NodeLike = nodeLike(S);
token = new NodeLike();
assert.strictEqual(token.offsetHeight, 1);
```
--------------------------------
### Get and Set Header Text - JavaScript
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/HeadingToken-(EN)
Illustrates how to get and set the inner text content of a header token. The example shows parsing a header, asserting its initial innerText, modifying it, and then asserting the change.
```javascript
// innerText (main)
var {firstChild} = Parser.parse('==a==');
assert.equal(firstChild, '==a==');
assert.strictEqual(firstChild.innerText, 'a');
firstChild.innerText = 'b';
assert.equal(firstChild, '==b==');
```
--------------------------------
### Normalize Section IDs with Legacy Fragment Mode
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/headings.txt
Illustrates section ID normalization when both 'html5' and 'legacy' fragment modes are enabled. This example specifically shows how non-breaking spaces are handled and encoded in section IDs and links.
```html
#Foo bar
```
```html
Foo bar
#Foo bar
```
--------------------------------
### Configure Create InputBox with Edit Intro
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/inputBoxParserTests.txt
This configuration for a 'create' InputBox includes an 'editintro' parameter, specifying a template to be used for the edit page. The generated HTML includes a hidden input field for 'editintro'.
```wikitext
type=create
editintro=MediaWiki:Test
```
```html
```
--------------------------------
### Initialize Legacy Lint Configuration in JavaScript
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/LintConfig
Demonstrates the initialization of a legacy lint configuration for the 'h1' rule. It verifies that the severity is correctly set to 'error' and checks for the presence of essential methods and properties like computeEditInfo, fix, ignoreDisables, and configurationComment.
```javascript
Parser.lintConfig = {h1: 2};
assert.strictEqual(Parser.lintConfig.rules.getSeverity('h1'), 'error');
assert.ok(Parser.lintConfig.computeEditInfo);
assert.ok(Parser.lintConfig.fix);
assert.ok(!Parser.lintConfig.ignoreDisables);
assert.strictEqual(Parser.lintConfig.configurationComment, 'lint');
```
--------------------------------
### Wikitext Example for Sub-Reference Merging
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/subReferencingTests.txt
This snippet shows the wikitext input used to test sub-reference merging. It includes two identical references with the same name and details.
```wikitext
[Book][Book]
```
--------------------------------
### Get and Set Fragment Property in JavaScript
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/LinkBaseToken-(EN)
Demonstrates how to get and set the fragment property of a parsed wiki link. The fragment is the anchor part of a URL. It shows examples for both print and main usage, including how the link target is auto-normalized when the fragment is modified.
```javascript
var { firstChild, lastChild } = Parser.parse('[[a#%7B%7D]][[b]]');
assert.strictEqual(firstChild.fragment, '{}');
assert.strictEqual(lastChild.fragment, undefined);
```
```javascript
var {firstChild} = Parser.parse('[[a#]]');
firstChild.fragment = undefined;
assert.equal(firstChild, '[[A]]'); // auto normalize the target page name
firstChild.fragment = 'b';
assert.equal(firstChild, '[[A#b]]');
```
--------------------------------
### Configure Create Page InputBox
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/inputBoxParserTests.txt
This snippet shows the configuration for a 'create' InputBox, used for creating new wiki pages. It sets the 'type' to 'create' and generates the HTML form for page creation.
```wikitext
type=create
```
```html
```
--------------------------------
### Get Type of a LinkToken
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/LinkToken-(EN)
Demonstrates how to retrieve the 'type' property of a link token. The example shows that the type is 'link' for a category link.
```javascript
var {firstChild} = Parser.parse('[[:category:a]]');
assert.strictEqual(firstChild.type, 'link');
```
--------------------------------
### Get WMF Site Nickname and Hostname (Node.js)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/Parser-(EN)
Retrieves the nickname and hostname of a WMF site given its script path. This function is useful for identifying specific MediaWiki installations.
```javascript
// getWMFSite (Node.js)
assert.deepStrictEqual(
Parser.getWMFSite('https://en.wikipedia.org/w/'),
['enwiki', 'https://en.wikipedia.org'],
);
assert.deepStrictEqual(
Parser.getWMFSite('https://zh.wiktionary.org/w/'),
['zhwiktionary', 'https://zh.wiktionary.org'],
);
```
--------------------------------
### Configure Create InputBox with Preload Parameters
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/inputBoxParserTests.txt
This configuration for a 'create' InputBox includes multiple 'preloadparams'. Each parameter is translated into a separate hidden input field in the generated HTML, allowing for pre-filled values.
```wikitext
type=create
preloadparams[]=param1
preloadparams[]=param2
```
```html
```
--------------------------------
### Parse Wikitext with __NOTOC__ Flag for HTML Output
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/headings.txt
This example demonstrates how the parser handles the `__NOTOC__` directive in wikitext. The generated HTML output will not contain any table of contents placeholders, as indicated by the 'no-toc' flag in the metadata.
```html
```
--------------------------------
### Introduction
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/ConverterToken-(EN)
Provides an overview of the wikiparser-node library, explaining its language conversion capabilities and inheritance from other classes. It also notes limitations for Mini and Browser versions.
```APIDOC
## Introduction
Language conversion. This class mixes the properties and methods of [ConverterFlagsToken](./ConverterFlagsToken-(EN)), and inherits all the properties and methods of the [Token](./Token-(EN)) class which are not repeated here.
All of the following properties and methods are not available in the [Mini](./Home-(EN)#mini) and [Browser](./Home-(EN)#browser-compatible) versions.
```
--------------------------------
### Get ParamLine Type Property
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/ParamLineToken-(EN)
Retrieves the 'type' property of a param-line element parsed from an inputbox tag. The 'type' property is fixed to 'param-line'. This example uses assert for verification.
```javascript
var line = Parser.parse('type=create')
.querySelector('param-line');
assert.equal(line, 'type=create');
assert.strictEqual(line.type, 'param-line');
```
--------------------------------
### Initialize and Use BoundingRect (Node.js)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/BoundingRect
This snippet demonstrates initializing a BoundingRect object with a token and an offset, then asserting its properties. It requires the BoundingRect class from '../lib/rect' and the Parser module.
```javascript
var {BoundingRect} = require('../lib/rect');
var token = Parser.parse('{{a\n|b=1\n}}').querySelector('parameter'),
rect = new BoundingRect(token, 5);
assert.equal(token, 'b=1\n');
assert.strictEqual(rect.start, 5);
assert.strictEqual(rect.top, 1);
assert.strictEqual(rect.left, 1);
```
--------------------------------
### Fetch MediaWiki Site Configuration (JavaScript)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/Parser-(EN)
Demonstrates fetching parsing configurations for a specific MediaWiki site. This requires the site nickname, script path, and optionally a user identifier. It returns a Promise that resolves to a Config object.
```javascript
Parser.fetchConfig('frwiki', 'https://fr.wikipedia.org/w/', 'user@example.net');
```
--------------------------------
### Get Lowercase Tag Name in JavaScript
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/AttributesToken-(EN)
Provides examples of accessing the read-only 'name' property, which returns the lowercase name of the tag. This is demonstrated for both extension and table cell attributes.
```javascript
var extAttrs = Parser.parse('').querySelector('ext-attrs'),
tableAttrs = Parser.parse('{|
!').querySelector('td')
.querySelector('table-attrs');
assert.strictEqual(extAttrs.name, 'ref');
assert.strictEqual(tableAttrs.name, 'th');
```
--------------------------------
### Get Last Child Node (Node.js)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/nodeLike
Illustrates retrieving the last child node of an element in Node.js using the nodeLike mixin. It depends on the 'nodeLike' mixin and verifies the last child's value.
```javascript
var {nodeLike} = require('../mixin/nodeLike');
var NodeLike, token;
class S {
childNodes = ['a', 'b'];
}
NodeLike = nodeLike(S);
token = new NodeLike();
assert.strictEqual(token.lastChild, 'b');
```
--------------------------------
### Wikitext to HTML Conversion (4-Quote Opening Sequence)
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/quotes.txt
Demonstrates the HTML rendering of wikitext with four-quote opening sequences. This section covers combinations that result in italics, bold, and combinations thereof, including handling of nowiki tags.
```wikitext
''''foo''
```
```html
''foo
```
```wikitext
''''foo''
```
```html
''foo
```
```wikitext
''''foo'''
```
```html
'foo
```
```wikitext
''''foo''''
```
```html
'foo'
```
```wikitext
''''foo''''
```
```html
'foo'
```
```wikitext
''''foo'''''
```
```html
'foo
```
```html/parsoid
'foo
```
```wikitext
''''foo'''
```
--------------------------------
### Wikitext to HTML Conversion: Mixed Lists with Options (PHP and Parsoid)
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/definitionLists.txt
Shows the conversion of wikitext containing mixed list types and options, demonstrating the resulting HTML structure. The example includes output from both PHP and Parsoid parsers, indicating potential differences in handling options.
```wikitext
*;d1 :d2
*;d3 :d4
```
```html
```
--------------------------------
### Apply Conversion Rule with A Flag (zh)
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/langParserTests2.txt
Demonstrates using the 'A' flag to insert a conversion rule that also produces output in the current language ('zh'). The rule specifies different outputs for zh-cn, zh-hk, and zh-tw, and the HTML includes both the rule's result and the default 'zh' output.
```wikitext
-{A|zh-cn:气气气;zh-hk:馬馬馬;zh-tw:電電電;}-
气气气, 馬馬馬, 電電電
```
```html
气气气
气气气, 馬馬馬, 電電電
```
--------------------------------
### Get ParamLine Name Property
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/ParamLineToken-(EN)
Retrieves the 'name' property of a param-line element parsed from an inputbox tag. The 'name' property represents the extension tag in lowercase and is read-only. This example uses assert for verification.
```javascript
var line = Parser.parse('type=create')
.querySelector('param-line');
assert.equal(line, 'type=create');
assert.strictEqual(line.name, 'inputbox');
```
--------------------------------
### Access and modify link URL
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/MagicLinkToken-(EN)
Shows how to get the external link URL using the `link` property. In Mini and Browser versions, this property is read-only. The example also demonstrates modifying the link, which is allowed in the main version.
```javascript
var {firstChild} = Parser.parse('ftp://a');
assert.strictEqual(firstChild.link, 'ftp://a');
```
```javascript
var {firstChild} = Parser.parse('ftp://a');
firstChild.link = 'https://b';
assert.equal(firstChild, 'https://b');
```
--------------------------------
### Pattern Definition and Instantiation (JavaScript)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/syntax
This snippet demonstrates how to define a pattern using a mixin and instantiate a class with that pattern. It imports the 'syntax' mixin and applies it to a base class 'S', then creates a token and asserts its pattern property.
```javascript
var {syntax} = require('../mixin/syntax');
var SyntaxBase, token;
class S {
seal() {
//
}
}
SyntaxBase = syntax('test')(S);
token = new SyntaxBase();
assert.strictEqual(token.pattern, 'test');
```
--------------------------------
### Get end position of text in Node.js
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/lint
The `getEndPos` function calculates the ending position (line and character) within a text based on provided start and end coordinates. It is part of the `lint` utility module. The function returns an object with `line` and `character` properties.
```javascript
var {getEndPos} = require('../util/lint');
assert.deepStrictEqual(
getEndPos(1, 1, 1, 1),
{line: 1, character: 2},
);
assert.deepStrictEqual(
getEndPos(1, 1, 2, 1),
{line: 2, character: 1},
);
```
--------------------------------
### Get Parameter Value - JavaScript
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/ParameterToken-(EN)
Illustrates how to retrieve the value of a parameter using the 'getValue' method. The example parses a template with both anonymous and named parameters, including surrounding whitespace. It shows that 'getValue' preserves whitespace for anonymous parameters but trims it for named parameters.
```javascript
var [anonymous, named] = Parser.parse('{{a| b | c = 1 }}')
.querySelectorAll('parameter');
assert.equal(anonymous, ' b ');
assert.equal(named, ' c = 1 ');
assert.strictEqual(anonymous.getValue(), ' b '); // 模板的匿名参数保留首尾的空白字符
assert.strictEqual(named.getValue(), '1'); // 模板的命名参数不保留首尾的空白字符
```
--------------------------------
### Wikitext to HTML Conversion (2-Quote Opening Sequence)
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/quotes.txt
Illustrates how wikitext with two-quote opening sequences is parsed into HTML. This covers cases for italics and bold formatting, showing variations in output based on the exact number of quotes and parser behavior.
```wikitext
''foo''
```
```html
foo
```
```wikitext
''foo'''
```
```html
foo'
```
```wikitext
''foo''''
```
```html
foo''
```
```wikitext
''foo''''
```
```html
foo''
```
```wikitext
''foo'''''
```
```html
foo
```
```html/parsoid
foo
```
```wikitext
''foo''
```
--------------------------------
### Wikitext to HTML Conversion: Mixed Lists with Options (PHP and Parsoid)
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/definitionLists.txt
Illustrates the conversion of wikitext with mixed list types and specific options applied. This example shows the resulting HTML from both PHP and Parsoid parsers, emphasizing Parsoid's preferred output for shared nesting.
```wikitext
*;foo :bar
```
```html
```
--------------------------------
### Get HTML Tag Range with JavaScript
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/HtmlToken-(EN)
Retrieves the start and end range of an HTML tag pair within the parsed AST. This function is useful for understanding the precise location of an element in the document structure. It returns an AstRange object or undefined if the tag has no range.
```javascript
// getRange (main)
var root = Parser.parse('x'),
{lastChild} = root,
range = lastChild.getRange();
assert.equal(lastChild, '');
assert.strictEqual(range.startContainer, root);
assert.strictEqual(range.startOffset, 1);
assert.strictEqual(range.endContainer, root);
assert.strictEqual(range.endOffset, 2);
```
--------------------------------
### Get and Set Interwiki Property in JavaScript
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/LinkBaseToken-(EN)
Illustrates how to retrieve and modify the interwiki prefix for a wiki link. This property specifies the wiki language or project. The example shows setting custom interwiki configurations and then updating the interwiki property of a parsed link.
```javascript
var firstChild;
Parser.getConfig();
Parser.config.interwiki = ['zhwp', 'enwp'];
({firstChild} = Parser.parse('[[zhwp:a#b]]'));
assert.strictEqual(firstChild.interwiki, 'zhwp');
firstChild.interwiki = 'enwp';
assert.equal(firstChild, '[[enwp:A#b]]');
```
--------------------------------
### Get ID Utility Function (Node.js)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/html
The getId function, also likely from an '../util/html' module, extracts an identifier from a parsed HTML structure. The example uses Node.js and the Parser module to create a root node, then asserts the output of getId on the root and its child nodes.
```javascript
var {getId} = require('../util/html');
var root = Parser.parse('_ -{}-
<__');
assert.strictEqual(getId(root), '-{}-_<');
assert.strictEqual(getId(root.childNodes), '-{}-_<');
```
--------------------------------
### Wikitext to HTML Conversion: Nested Lists with Options (PHP and Parsoid)
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/definitionLists.txt
Shows the conversion of nested wikitext lists with options applied, demonstrating the resulting HTML structure. This example highlights the differences in output between PHP and Parsoid parsers, particularly regarding nesting.
```wikitext
*#;foo :bar
```
```html
```
--------------------------------
### Configure Create InputBox with Empty Preload Parameters
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/inputBoxParserTests.txt
This snippet shows a 'create' InputBox configured with empty 'preloadparams'. The generated HTML includes hidden input fields for 'preloadparams[]' without any values, which can be useful for specific template structures.
```wikitext
type=create
preloadparams[]=
preloadparams[]=
```
```html
```
--------------------------------
### Get and Set Link Property in JavaScript
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/LinkBaseToken-(EN)
Shows how to access and modify the full link target, which includes the page name and any interwiki prefixes or fragments. This property is read-only in certain versions of the library. Examples cover basic link setting and handling of special prefixes like 'file:' and 'category:'.
```javascript
var {firstChild: {link}} = Parser.parse('[[a]]');
assert.deepStrictEqual(
link,
Parser.normalizeTitle('A'),
);
```
```javascript
var {firstChild} = Parser.parse('[[a]]');
firstChild.link = 'file:b';
assert.equal(firstChild, '[[:file:b]]'); // auto add ':' at the beginning
({firstChild} = Parser.parse('[[category:c]]'));
firstChild.link = 'category:d';
assert.equal(firstChild, '[[category:d]]');
```
--------------------------------
### Example of Correct Code for No Duplicate Rule
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/no-duplicate
This snippet shows examples of correct wikitext that would not trigger the 'no-duplicate' rule by default. It includes examples with duplicate spans and template parameters.
```wikitext
1
2
```
```wikitext
{{Foo|bar=[1]|bar=[2]}}
```
--------------------------------
### Collapse a Range to its start or end point in JavaScript
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/AstRange-(EN)
Reduces a Range to a single point, either its start or end. The optional boolean parameter determines which point to collapse to (true for start, false or omitted for end). This is useful for simplifying selections.
```javascript
var root = Parser.parse('a'),
range = root.createRange();
range.setEnd(root, 1);
range.collapse();
assert.strictEqual(range.startContainer, root);
assert.strictEqual(range.startOffset, 1);
range.setStart(root, 0);
range.collapse(true);
assert.strictEqual(range.endContainer, root);
assert.strictEqual(range.endOffset, 0);
```
--------------------------------
### Provide Signature Help for Wikitext (Node.js)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/LanguageService-(EN)
Demonstrates how to use the `provideSignatureHelp` function to get signature information for magic words within wikitext. It covers different magic words like '#invoke', 'PAGENAME', and 'PAGESIZE', and various cursor positions.
```javascript
// provideSignatureHelp (Node.js)
(async () => {
const lsp = Parser.createLanguageService();
assert.deepStrictEqual(
await lsp.provideSignatureHelp(
'{{ #invoke: a | b | c | d }}',
{line: 0, character: 12},
),
{
signatures: [
{
label: '{{#invoke:module name|function name|args}}',
parameters: [
{label: 'module name'},
{label: 'function name'},
{label: 'args'},
],
activeParameter: 0,
},
],
activeParameter: 0,
},
);
assert.deepStrictEqual(
await lsp.provideSignatureHelp(
'{{ #invoke: a | b | c | d }}',
{line: 0, character: 16},
),
{
signatures: [
{
label: '{{#invoke:module name|function name|args}}',
parameters: [
{label: 'module name'},
{label: 'function name'},
{label: 'args'},
],
activeParameter: 1,
},
],
activeParameter: 1,
},
);
assert.deepStrictEqual(
await lsp.provideSignatureHelp(
'{{ #invoke: a | b | c | d }}',
{line: 0, character: 25},
),
{
signatures: [
{
label: '{{#invoke:module name|function name|args}}',
parameters: [
{label: 'module name'},
{label: 'function name'},
{label: 'args'},
],
activeParameter: 2,
},
],
activeParameter: 3,
},
);
assert.deepStrictEqual(
await lsp.provideSignatureHelp(
'{{ PAGENAME: }}',
{line: 0, character: 2},
),
{
signatures: [
{
label: '{{PAGENAME:page name}}',
parameters: [{label: 'page name'}],
}
],
activeParameter: -1,
},
);
assert.deepStrictEqual(
await lsp.provideSignatureHelp(
'{{ PAGESIZE: a | R }}',
{line: 0, character: 13},
),
{
signatures: [
{
label: '{{PAGESIZE:page name|R}}',
parameters: [
{label: 'page name'},
{label: 'R', documentation: 'Predefined parameter'},
],
}
],
activeParameter: 0,
},
);
assert.deepStrictEqual(
await lsp.provideSignatureHelp(
'{{PAGESIZE: a | R }}',
{line: 0, character: 17},
),
{
signatures: [
{
label: '{{PAGESIZE:page name|R}}',
parameters: [
{label: 'page name'},
{label: 'R', documentation: 'Predefined parameter'},
],
}
],
activeParameter: 1,
},
);
})();
```
--------------------------------
### Wikitext to HTML Conversion (3-Quote Opening Sequence)
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/quotes.txt
Shows the HTML output for wikitext using three-quote opening sequences, primarily for bold formatting. It details how different numbers of trailing quotes affect the resulting HTML tags and structure.
```wikitext
'''foo''
```
```html
'foo
```
```wikitext
'''foo'''
```
```html
foo
```
```wikitext
'''foo''''
```
```html
foo'
```
```wikitext
'''foo'''''
```
```html
foo
```
```html/parsoid
foo
```
```wikitext
'''foo'''
```
--------------------------------
### Handle Valid URLs in Wikitext - WikiText
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/invalid-url
Shows correct wikitext examples for URLs that are accepted by the WikiParser Node's invalid-url rule. These examples often use template parameters or properly formatted internal links.
```wikitext
[{{server}}/Foo]
```
```wikitext
[[File:example.jpg|link={{fullurl:Example}}]]
```
--------------------------------
### Get client width - JavaScript
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/AstElement-(EN)
Gets the inner width of an AstElement, available only when innerText is defined. This read-only property returns a number.
```javascript
var root = Parser.parse('ab\nc
'),
{firstChild} = root;
assert.equal(firstChild, 'ab\nc
');
assert.strictEqual(firstChild.innerText, 'ab\nc');
assert.strictEqual(root.innerText, undefined);
assert.strictEqual(firstChild.clientWidth, 1);
assert.strictEqual(root.clientWidth, undefined);
```
--------------------------------
### Add Custom Paths for Parsing Configurations
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/Parser-(EN)
Illustrates how to extend the search paths for parsing configurations by pushing a directory to the 'configPaths' array. This allows the parser to find configurations in custom locations.
```js
// configPaths (Node.js)
Parser.configPaths.push('config');
Parser.config = './enwiki';
assert.strictEqual(Parser.getConfig().articlePath, '/wiki/$1');
```
--------------------------------
### Get client height - JavaScript
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/AstElement-(EN)
Gets the inner height of an AstElement, available only when innerText is defined. This read-only property returns a number.
```javascript
var root = Parser.parse('a\nb
'),
{firstChild} = root;
assert.equal(firstChild, 'a\nb
');
assert.strictEqual(firstChild.innerText, 'a\nb');
assert.strictEqual(root.innerText, undefined);
assert.strictEqual(firstChild.clientHeight, 2);
assert.strictEqual(root.clientHeight, undefined);
```
--------------------------------
### Run Shadow Debugging Utility (Node.js)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/debug
Demonstrates the usage of Shadow.run for debugging purposes within a Node.js environment. It initializes a Token object and asserts its attributes and the state of Shadow.running.
```javascript
var {Shadow} = require('../util/debug'),
{Token} = require('../src/index');
var token = Shadow.run(() => new Token());
assert.ok(token.getAttribute('built'));
assert.ok(!Shadow.running);
```
--------------------------------
### Install WikiParser-Node using npm
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/README.md
Installs the WikiParser-Node package for use in a Node.js environment. This is the primary method for integrating the parser into your project.
```sh
npm i wikiparser-node
```
--------------------------------
### Provide Inlay Hints for Wikitext (Node.js)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/LanguageService-(EN)
Shows how to compute inlay hints for wikitext content using the `provideInlayHints` function. This is useful for displaying additional information directly within the editor.
```javascript
// provideInlayHints
(async () => {
const lsp = Parser.createLanguageService();
assert.deepStrictEqual(
await lsp.provideInlayHints('{{a|b=|c}}'),
[
{
position: {line: 0, character: 7},
kind: 2,
label: '1=',
},
]
);
assert.deepStrictEqual(
await lsp.provideInlayHints('{{#invoke:a|b|c}}'),
[
{
position: {line: 0, character: 14},
kind: 2,
label: '1=',
},
].reverse()
);
})();
```
--------------------------------
### Fetch Wiki Configuration (Node.js)
Source: https://github.com/bhsd-harry/wikiparser-node/wiki/fetchConfig
Demonstrates fetching configuration for different wiki instances using the fetchConfig function. It requires the '../bin/config' module and takes parameters for wiki name, API endpoint, revision type, and boolean flags. The function is asynchronous and can be called multiple times to configure various wikis.
```javascript
var fetchConfig = require('../bin/config').default;
(async () => {
await fetchConfig(
'enwiki',
'https://en.wikipedia.org/w',
'git',
false,
true,
);
await fetchConfig(
'llwiki',
'https://llwiki.org/mediawiki',
'git',
false,
true,
);
})();
```
--------------------------------
### Create Page Input Box with Summary HTML
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/inputBoxParserTests.txt
Generates HTML for a 'create' input box that includes a 'summary' field. This allows users to provide a summary for the new page during creation.
```html
```
--------------------------------
### Wikitext to HTML Conversion: Mixed Definition and Unordered Lists (PHP and Parsoid)
Source: https://github.com/bhsd-harry/wikiparser-node/blob/main/test/core/definitionLists.txt
Demonstrates the conversion of wikitext containing a mix of definition lists and nested unordered lists. The example provides the HTML output from both PHP and Parsoid parsers, highlighting their handling of such mixed structures.
```wikitext
:*d1
::*d2
```
```html
-
```