Title (EN) - bhsd-harry/wikiparser-node GitHub Wiki
Table of Contents
The Title object is generated by the Parser.normalizeTitle method and represents the title of a page.
✅ Available in the Mini and Browser versions.
✅ Expand
version added: 1.1.0
type: string | undefined
Extension in lowercase.
// extension
assert.strictEqual(Parser.normalizeTitle('A.CSS').extension, 'css');
// extension (main)
var title = Parser.normalizeTitle('A.CSS');
title.extension = 'JS';
assert.equal(title, 'A.JS');
title.extension = undefined;
assert.equal(title, 'A');
title.extension = 'json';
assert.equal(title, 'A.json');
✅ Expand
type: string | undefined
URI fragment.
// fragment
assert.strictEqual(Parser.normalizeTitle('Wikitext').fragment, undefined);
assert.strictEqual(Parser.normalizeTitle('# %3C ').fragment, '_<');
// fragment (main)
var title;
title = Parser.normalizeTitle('a#%3C');
title.fragment = 'a%20 b ';
assert.equal(title, 'A#a_b');
title.fragment = undefined;
assert.equal(title, 'A');
Expand
type: string
Interwiki prefix of the title. Note that it does not contain :
and will be empty when using the default parsing configurations.
// interwiki (main)
var title;
assert.strictEqual(Parser.normalizeTitle('mw:Main Page', 0).interwiki, '');
Parser.getConfig();
Parser.config.interwiki = ['mw', 'metawiki'];
title = Parser.normalizeTitle('mw:Main Page');
assert.strictEqual(title.interwiki, 'mw');
title.interwiki = 'metawiki';
assert.equal(title, 'metawiki:Main_Page');
✅ Expand
type: string
Title main part without namespace. Note that the first letter will be capitalized and underscores will be replaced with spaces.
// main
var title = Parser.normalizeTitle('template:birth_ date:mm');
assert.strictEqual(title.main, 'Birth date:mm');
title.main = 'birth_month';
assert.strictEqual(title.main, 'Birth month');
✅ Expand
type: number
Namespace number.
// ns
assert.strictEqual(Parser.normalizeTitle(':File:a', 10).ns, 6);
assert.strictEqual(Parser.normalizeTitle('File:a', 10).ns, 6);
assert.strictEqual(Parser.normalizeTitle('A', 10).ns, 10);
assert.strictEqual(Parser.normalizeTitle('../a', 10).ns, 0);
// ns (main)
var title;
Parser.getConfig();
Parser.config.interwiki = ['mw'];
title = Parser.normalizeTitle('mw:File:a');
assert.strictEqual(title.ns, 6);
title.ns = 0;
assert.equal(title, 'mw:A');
✅ Expand
type: string
Standard namespace prefix of the title, read-only.
// prefix
assert.strictEqual(Parser.normalizeTitle('A').prefix, '');
assert.strictEqual(Parser.normalizeTitle('Wikipedia:A').prefix, 'Project:');
// prefix (main)
Parser.getConfig();
Parser.config.interwiki = ['mw'];
assert.strictEqual(Parser.normalizeTitle('mw:user:a').prefix, 'User:');
✅ Expand
type: string
Normalized title with namespace prefix, read-only. Note that the first letter will be capitalized and spaces will be replaced with underscores.
// title
assert.strictEqual(
Parser.normalizeTitle('user talk:a b').title,
'User_talk:A_b',
);
// title (main)
Parser.conversionTable.set('漢', '汉');
assert.strictEqual(Parser.normalizeTitle('漢').title, '汉');
Parser.redirects.set('汉', 'Chinese#Chinese');
assert.strictEqual(Parser.normalizeTitle('漢').title, 'Chinese');
Parser.config.interwiki = ['mw'];
assert.strictEqual(Parser.normalizeTitle('mw:a').title, 'mw:A');
✅ Expand
type: boolean
Whether the title is valid according to the MediaWiki rules.
// valid
assert.ok(!Parser.normalizeTitle('').valid);
assert.ok(!Parser.normalizeTitle('#a').valid);
assert.ok(!Parser.normalizeTitle('<').valid);
assert.ok(!Parser.normalizeTitle('<').valid);
assert.ok(!Parser.normalizeTitle('%3c').valid);
assert.ok(Parser.normalizeTitle('a#a<').valid);
assert.ok(!Parser.normalizeTitle('&nbsp;').valid);
assert.ok(!Parser.normalizeTitle('%25').valid);
assert.ok(!Parser.normalizeTitle('::a').valid);
assert.ok(Parser.normalizeTitle(':a').valid);
assert.ok(!Parser.normalizeTitle('./a').valid);
assert.ok(!Parser.normalizeTitle('a/..').valid);
assert.ok(Parser.normalizeTitle('../a').valid);
assert.ok(!Parser.normalizeTitle('<nowiki/>a').valid);
assert.ok(Parser.normalizeTitle('{{a}}').valid);
assert.ok(!Parser.normalizeTitle('{{!}}').valid);
Expand
Perform unidirectional conversion. This will modify the main
property.
// autoConvert (main)
var title;
Parser.conversionTable.set('頁', '页');
Parser.conversionTable.set('首頁', 'Main page');
title = Parser.normalizeTitle('首頁');
title.autoConvert();
assert.strictEqual(title.main, 'Main page');
Expand
version added: 1.12.2
returns: [boolean, string]
Check if the title is a redirection and get the target title.
// getRedirection (main)
var title;
Parser.redirects.set('File:A', 'File:B');
title = Parser.normalizeTitle('media:a');
assert.deepStrictEqual(title.getRedirection(), [true, 'Media:B']);
Parser.conversionTable.set('漢', '汉');
Parser.redirects.set('汉', 'Chinese#Chinese');
title = Parser.normalizeTitle('漢');
assert.deepStrictEqual(title.getRedirection(), [true, 'Chinese']);
title = Parser.normalizeTitle('c');
assert.deepStrictEqual(title.getRedirection(), [false, 'C']);
✅ Expand
version added: 1.10.0
returns: string
Get the URL of the title.
// getUrl
var title = Parser.normalizeTitle('?/a#b');
assert.strictEqual(title.getUrl(), '/wiki/%3F%2Fa#b');
title = Parser.normalizeTitle('#<');
assert.strictEqual(title.getUrl(), '#%3C');
title = Parser.normalizeTitle('');
assert.strictEqual(title.getUrl(), '');
// getUrl (main)
var title;
Parser.redirects.set('C', 'D#C');
title = Parser.normalizeTitle('C');
assert.strictEqual(title.getUrl(), '/wiki/D#C');
Expand
version added: 1.1.0
Whether it is a talk page.
// isTalkPage (main)
var title = Parser.normalizeTitle('file talk:a.jpg');
assert.ok(title.isTalkPage());
title = Parser.normalizeTitle('file:a.jpg');
assert.ok(!title.isTalkPage());
Expand
version added: 1.1.0
Switch to the base page.
// toBasePage (main)
var title = Parser.normalizeTitle('A/AA/AAA');
title.toBasePage();
assert.equal(title, 'A/AA');
title.toBasePage();
assert.equal(title, 'A');
Expand
version added: 1.1.0
Switch to the root page.
// toRootPage (main)
var title = Parser.normalizeTitle('A/AA/AAA');
title.toRootPage();
assert.equal(title, 'A');
Expand
version added: 1.1.0
Switch to the subject page.
// toSubjectPage (main)
var title = Parser.normalizeTitle('file talk:a.jpg');
title.toSubjectPage();
assert.equal(title, 'File:A.jpg');
Expand
version added: 1.1.0
Switch to the talk page.
// toTalkPage (main)
var title = Parser.normalizeTitle('file:a.jpg');
title.toTalkPage();
assert.equal(title, 'File_talk:A.jpg');