Skip to content

Title (EN)

bhsd edited this page Jul 23, 2024 · 9 revisions
Table of Contents

Other Languages

Introduction

The Title object is generated by the Parser.normalizeTitle method and represents the title of a page.

✅ Available in the Mini and Browser versions.

Properties

valid

✅ 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('&#60;').valid);
assert(!Parser.normalizeTitle('%3C').valid);
assert(!Parser.normalizeTitle('#a').valid);
assert(Parser.normalizeTitle('a#a<').valid);
assert(!Parser.normalizeTitle('&amp;nbsp;').valid);

ns

✅ 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');

fragment

✅ 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&nbsp;';
assert.equal(title, '#a_b');

main

✅ 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');

prefix

✅ Expand

type: string
Standard namespace prefix of the title, read-only.

// prefix
assert.strictEqual(Parser.normalizeTitle('Wikipedia:A').prefix, 'Project:');

title

✅ 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',
);

extension

✅ 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');

interwiki

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');

Methods

getRedirection

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']);

autoConvert

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');

toSubjectPage

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');

toTalkPage

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');

isTalkPage

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());

toBasePage

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');

toRootPage

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');

getUrl

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');
Clone this wiki locally