Title (EN) - bhsd-harry/wikiparser-node GitHub Wiki

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

displayTitle

Expand

version added: 1.22.0

type: string
Normalized title with namespace prefix, read-only. Note that the first letter will be capitalized and underscores will be replaced with spaces.

// displayTitle (main)
assert.strictEqual(
	Parser.normalizeTitle("user talk:a b").displayTitle,
	"User talk:A b",
);

extension

✅ Expand

version added: 1.1.0

type: string | undefined
Extension in lowercase. Read-only in the Mini and Browser versions.

// extension
assert.strictEqual(Parser.normalizeTitle("A.CSS").extension, "css");
assert.strictEqual(Parser.normalizeTitle("A").extension, undefined);
// 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");

fragment

✅ Expand

type: string | undefined
URI fragment. Read-only in the Mini and Browser versions.

// fragment
assert.strictEqual(Parser.normalizeTitle("Wikitext").fragment, undefined);
assert.strictEqual(Parser.normalizeTitle("#&nbsp;  %3C ").fragment, "_<");
// fragment (main)
var title;
title = Parser.normalizeTitle("a#%3C");
title.fragment = "a%20 b&nbsp;";
assert.equal(title, "A#a_b");
title.fragment = undefined;
assert.equal(title, "A");

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.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");

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:mm");
assert.strictEqual(title.main, "Birth date:mm");
title.main = "birth_month";
assert.strictEqual(title.main, "Birth month");

ns

✅ Expand

type: number
Namespace number. Read-only in the Mini and Browser versions.

// 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");

prefix

✅ 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:");

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

valid

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

Methods

autoConvert

Expand

Perform unidirectional conversion. This will modify the main property.

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

getFileUrl

Expand

version added: 1.32.0

param: number | false width
param: number | false height
returns: string
Get the URL of the file.

// getFileUrl (main)
var title = Parser.normalizeTitle("File:A.jpg");
assert.strictEqual(
	title.getFileUrl(),
	"/wiki/Special%3ARedirect%2Ffile%2FA.jpg",
);
assert.strictEqual(
	title.getFileUrl(100),
	"/wiki/Special%3ARedirect%2Ffile%2FA.jpg?width=100",
);
assert.strictEqual(
	title.getFileUrl(false, 100),
	"/wiki/Special%3ARedirect%2Ffile%2FA.jpg?width=10000&height=100",
);
assert.strictEqual(
	title.getFileUrl(100, 100),
	"/wiki/Special%3ARedirect%2Ffile%2FA.jpg?width=100&height=100",
);

getRedirection

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

getUrl

✅ Expand

version added: 1.10.0

returns: string
Get the URL of the title.

// getUrl
var title;
Parser.config.articlePath = "/wiki/$1";
title = Parser.normalizeTitle("?/a#b");
assert.strictEqual(title.getUrl(), "/wiki/%3F%2Fa#b");
Parser.config.articlePath = "/wiki/";
title = Parser.normalizeTitle("?/a#b");
assert.strictEqual(title.getUrl(), "/wiki/%3F%2Fa#b");
Parser.config.articlePath = "/wiki";
title = Parser.normalizeTitle("?/a#b");
assert.strictEqual(title.getUrl(), "/wiki/%3F%2Fa#b");
title = Parser.normalizeTitle("x");
assert.strictEqual(title.getUrl(), "/wiki/X");
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");

isRedirect

Expand

version added: 1.38.2

returns: boolean
Check if the title is a redirection.

// isRedirect (main)
var title;
Parser.redirects.set("File:A", "File:B");
title = Parser.normalizeTitle("media:a");
assert.strictEqual(title.isRedirect(), true);
Parser.conversionTable.set("漢", "汉");
Parser.redirects.set("汉", "Chinese#Chinese");
title = Parser.normalizeTitle("漢");
assert.strictEqual(title.isRedirect(), true);
title = Parser.normalizeTitle("c");
assert.strictEqual(title.isRedirect(), false);

isTalkPage

Expand

version added: 1.1.0

returns: boolean
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());

toBasePage

Expand

version added: 1.1.0

returns: this
Switch to the base page.

// toBasePage (main)
var title = Parser.normalizeTitle("A/AA/AAA");
assert.equal(title.toBasePage(), "A/AA");
assert.equal(title, "A/AA");
assert.equal(title.toBasePage(), "A");
assert.equal(title, "A");

toRootPage

Expand

version added: 1.1.0

returns: this
Switch to the root page.

// toRootPage (main)
var title = Parser.normalizeTitle("A/AA/AAA");
assert.equal(title.toRootPage(), "A");
assert.equal(title, "A");

toSubjectPage

Expand

version added: 1.1.0

returns: this
Switch to the subject page.

// toSubjectPage (main)
var title = Parser.normalizeTitle("file talk:a.jpg");
assert.equal(title.toSubjectPage(), "File:A.jpg");
assert.equal(title, "File:A.jpg");

toTalkPage

Expand

version added: 1.1.0

returns: this
Switch to the talk page.

// toTalkPage (main)
var title = Parser.normalizeTitle("file:a.jpg");
assert.equal(title.toTalkPage(), "File_talk:A.jpg");
assert.equal(title, "File_talk:A.jpg");
⚠️ **GitHub.com Fallback** ⚠️