-
Notifications
You must be signed in to change notification settings - Fork 1
Title (EN)
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
type: boolean
Whether the title is valid according to the MediaWiki rules.
// valid
assert(!Parser.normalizeTitle('').valid);
assert(!Parser.normalizeTitle('<').valid);
assert(!Parser.normalizeTitle('<').valid);
assert(!Parser.normalizeTitle('%3C').valid);
assert(!Parser.normalizeTitle('#a').valid);
assert(Parser.normalizeTitle('a#a<').valid);
assert(!Parser.normalizeTitle('&nbsp;').valid);
✅ Expand
type: number
Namespace number.
// ns
var title;
assert.strictEqual(Parser.normalizeTitle(':File:a').ns, 6);
assert.strictEqual(Parser.normalizeTitle('File:a', 10).ns, 6);
title = Parser.normalizeTitle('A', 10);
assert.strictEqual(title.ns, 10);
// ns (main)
var title;
Parser.config = 'zhwiki';
title = Parser.normalizeTitle('mw:File:a');
assert.strictEqual(title.ns, 6);
title.ns = 0;
assert.equal(title, 'mw:A');
✅ Expand
type: string | undefined
URI fragment.
// fragment
var title;
assert.strictEqual(Parser.normalizeTitle('Wikitext').fragment, undefined);
title = Parser.normalizeTitle('#%3C');
assert.strictEqual(title.fragment, '<');
// fragment (main)
var title;
title = Parser.normalizeTitle('#%3C');
title.fragment = 'a%20 b ';
assert.equal(title, '#a_b');
✅ 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');
assert.strictEqual(title.main, 'Birth date');
title.main = 'birth_month';
assert.strictEqual(title.main, 'Birth month');
✅ Expand
type: string
Standard namespace prefix of the title, read-only.
// prefix
assert.strictEqual(Parser.normalizeTitle('Wikipedia:A').prefix, 'Project:');
✅ 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('template:birth _date').title,
'Template:Birth_date',
);
✅ Expand
version added: 1.1.0
type: string | undefined
Extension in lowercase.
// extension
var title = Parser.normalizeTitle('A.CSS');
assert.strictEqual(title.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
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.config = 'zhwiki';
title = Parser.normalizeTitle('mw:Main Page');
assert.strictEqual(title.interwiki, 'mw');
title.interwiki = 'metawiki';
assert.equal(title, 'metawiki:Main_Page');
Expand
version added: 1.12.0
Check if the title is a redirection and get the target title.
returns: [boolean, string]
// getRedirection (main)
var title;
Parser.redirects.set('A', 'B');
title = Parser.normalizeTitle('a');
assert.deepStrictEqual(title.getRedirection(), [true, 'B']);
title = Parser.normalizeTitle('c');
assert.deepStrictEqual(title.getRedirection(), [false, 'C']);
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.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');
Expand
version added: 1.1.0
Whether it is a talk page.
// isTalkPage (main)
var title = Parser.normalizeTitle('file talk:a.jpg');
assert(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.10.0
returns: string
Get the URL of the title.
// getUrl (main)
var title = Parser.normalizeTitle('?/a#b');
assert.strictEqual(title.getUrl(), '/wiki/%3F%2Fa#b');
title = Parser.normalizeTitle('#<');
assert.strictEqual(title.getUrl(), '#%3C');
对维基文本批量执行语法检查的命令行工具
用于维基文本的 ESLint 插件
A command-line tool that performs linting on Wikitext in bulk
ESLint plugin for Wikitext