Skip to content

Title (EN)

bhsd edited this page Dec 11, 2023 · 11 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
Parser.config = 'zhwiki';
assert(!Parser.normalizeTitle('<').valid);
assert(!Parser.normalizeTitle('#a').valid);
assert(Parser.normalizeTitle('mw:').valid);

ns

✅ Expand

type: number
Namespace number.

// ns
var title = Parser.normalizeTitle('A', 10);
assert.equal(title, 'Template:A');
assert.strictEqual(title.ns, 10);
title.ns = 0;
assert.equal(title, 'A');

fragment

✅ Expand

type: string | undefined
URI fragment.

// fragment
var title = Parser.normalizeTitle('Wikitext');
assert.strictEqual(title.fragment, undefined);
title.fragment = 'Notes';
assert.equal(title, 'Wikitext#Notes');

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
Parser.config = 'zhwiki';
var title = Parser.normalizeTitle('mw:Main Page');
assert.strictEqual(title.interwiki, 'mw');
title.interwiki = 'metawiki';
assert.equal(title, 'metawiki:Main_Page');

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');
title.extension = 'js';
assert.equal(title, 'A.js');

Methods

autoConvert

Expand

Perform unidirectional conversion. This will modify the main property.

// autoConvert
Parser.conversionTable.set('頁', '页');
var title = Parser.normalizeTitle('首頁');
assert.strictEqual(title.main, '首頁');
title.autoConvert();
assert.strictEqual(title.main, '首页');

toSubjectPage

Expand

version added: 1.1.0

Switch to the subject page.

// toSubjectPage
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
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
var title = Parser.normalizeTitle('file talk:a.jpg');
assert(title.isTalkPage());

toBasePage

Expand

version added: 1.1.0

Switch to the base page.

// toBasePage
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
var title = Parser.normalizeTitle('A/AA/AAA');
title.toRootPage();
assert.equal(title, 'A');
Clone this wiki locally