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

Table of Contents

Other Languages

Introduction

File. This class inherits all the properties and methods of the LinkBaseToken class.

✅ Available in the Mini and Browser versions.
🌐 Available in the Browser version.

Properties

extension

✅ Expand

version added: 1.5.3

type: string
File extension, read-only.

// extension (print)
var {firstChild} = Parser.parse("[[file:a.jpg]]");
assert.strictEqual(firstChild.extension, "jpg");

name

✅ Expand

type: string
File name,read-only.

// name
var {firstChild} = Parser.parse("[[file:a]]");
assert.strictEqual(firstChild.name, "File:A");
// name (main)
var {firstChild} = Parser.parse("[[file:a]]");
firstChild.firstChild.setText("file:b", 0);
assert.strictEqual(firstChild.name, "File:B");

link

Expand

type: string | Title
File link.

// link (main)
var {firstChild, lastChild} = Parser.parse("[[file:a]][[file:b|link=c]]");
assert.equal(firstChild, "[[file:a]]");
assert.equal(lastChild, "[[file:b|link=c]]");
assert.deepStrictEqual(firstChild.link, firstChild.normalizeTitle("file:a"));
assert.deepStrictEqual(
	lastChild.link,
	lastChild.normalizeTitle("c", 0, {page: ""}),
);
firstChild.link = "//c";
assert.equal(firstChild, "[[file:a|link=//c]]");

size

Expand

type: {width: string, height: string}
Image size.

// size (main)
var {firstChild} = Parser.parse("[[file:a|x1px]]");
assert.deepStrictEqual(firstChild.size, {width: "", height: "1"});
firstChild.size = {width: "1", height: "1"};
assert.equal(firstChild, "[[file:a|1x1px]]");

width

Expand

type: number
Image width.

// width (main)
var {firstChild} = Parser.parse("[[file:a|1x1px]]");
assert.strictEqual(firstChild.width, "1");
firstChild.width = undefined;
assert.equal(firstChild, "[[file:a|x1px]]");

height

Expand

type: number
Image height.

// height (main)
var {firstChild} = Parser.parse("[[file:a|1x1px]]");
assert.strictEqual(firstChild.height, "1");
firstChild.height = undefined;
assert.equal(firstChild, "[[file:a|1px]]");

Methods

lint

✅ Expand

returns: LintError[]
Report potential grammar errors.

// lint
var {firstChild} = Parser
	.parse("[[file:a|frameless|framed|left|none|sub|top|b|c|alt=]]");
assert.deepStrictEqual(firstChild.lint(), [
	{
		rule: "invalid-gallery",
		severity: "error",
		message: "missing file extension",
		startLine: 0,
		startCol: 0,
		startIndex: 0,
		endLine: 0,
		endCol: 54,
		endIndex: 54,
	},
	{
		rule: "blank-alt",
		severity: "warning",
		message: "blank alt attribute",
		startLine: 0,
		startCol: 48,
		startIndex: 48,
		endLine: 0,
		endCol: 52,
		endIndex: 52,
		fix: {
			desc: "remove",
			range: [47, 52],
			text: "",
		},
	},
	{
		rule: "no-duplicate",
		severity: "error",
		message: "duplicate image caption parameter",
		startLine: 0,
		startCol: 44,
		startIndex: 44,
		endLine: 0,
		endCol: 45,
		endIndex: 45,
		suggestions: [
			{
				desc: "remove",
				range: [43, 45],
				text: "",
			},
		],
	},
	{
		rule: "no-duplicate",
		severity: "error",
		message: "duplicate image caption parameter",
		startLine: 0,
		startCol: 46,
		startIndex: 46,
		endLine: 0,
		endCol: 47,
		endIndex: 47,
		suggestions: [
			{
				desc: "remove",
				range: [45, 47],
				text: "",
			},
		],
	},
	{
		rule: "no-duplicate",
		severity: "error",
		message: "conflicting image frame parameter",
		startLine: 0,
		startCol: 9,
		startIndex: 9,
		endLine: 0,
		endCol: 18,
		endIndex: 18,
		suggestions: [
			{
				desc: "remove",
				range: [8, 18],
				text: "",
			},
		],
	},
	{
		rule: "no-duplicate",
		severity: "error",
		message: "conflicting image frame parameter",
		startLine: 0,
		startCol: 19,
		startIndex: 19,
		endLine: 0,
		endCol: 25,
		endIndex: 25,
		suggestions: [
			{
				desc: "remove",
				range: [18, 25],
				text: "",
			},
		],
	},
	{
		rule: "no-duplicate",
		severity: "error",
		message: "conflicting image horizontal-alignment parameter",
		startLine: 0,
		startCol: 26,
		startIndex: 26,
		endLine: 0,
		endCol: 30,
		endIndex: 30,
		suggestions: [
			{
				desc: "remove",
				range: [25, 30],
				text: "",
			},
		],
	},
	{
		rule: "no-duplicate",
		severity: "error",
		message: "conflicting image horizontal-alignment parameter",
		startLine: 0,
		startCol: 31,
		startIndex: 31,
		endLine: 0,
		endCol: 35,
		endIndex: 35,
		suggestions: [
			{
				desc: "remove",
				range: [30, 35],
				text: "",
			},
		],
	},
	{
		rule: "no-duplicate",
		severity: "error",
		message: "conflicting image vertical-alignment parameter",
		startLine: 0,
		startCol: 36,
		startIndex: 36,
		endLine: 0,
		endCol: 39,
		endIndex: 39,
		suggestions: [
			{
				desc: "remove",
				range: [35, 39],
				text: "",
			},
		],
	},
	{
		rule: "no-duplicate",
		severity: "error",
		message: "conflicting image vertical-alignment parameter",
		startLine: 0,
		startCol: 40,
		startIndex: 40,
		endLine: 0,
		endCol: 43,
		endIndex: 43,
		suggestions: [
			{
				desc: "remove",
				range: [39, 43],
				text: "",
			},
		],
	},
]);

getAllArgs

✅ Expand

returns: ImageParameterToken[]
Get all image parameter nodes.

// getAllArgs
var {firstChild} = Parser.parse("[[file:a|thumb|1px|link=b|alt=c|d]]");
assert.deepStrictEqual(firstChild.getAllArgs(), firstChild.childNodes.slice(1));

getArgs

✅ Expand

param: string parameter name
returns: ImageParameterToken[]
Get specified image parameter nodes.

// getArgs
var {firstChild} = Parser.parse("[[file:a|link=b|链接=c]]");
assert.deepStrictEqual(
	firstChild.getArgs("link"),
	firstChild.childNodes.slice(1),
);

getArg

✅ Expand

param: string parameter name
returns: ImageParameterToken
Get effective node of the specified image parameter.

// getArg
var {firstChild} = Parser.parse("[[file:a|link=b|链接=c]]");
assert.strictEqual(firstChild.getArg("link"), firstChild.lastChild);

getValue

✅ Expand

param: string parameter name
returns: string | true
Get effective value of the specified image parameter.

// getValue
var {firstChild} = Parser.parse("[[file:a|b|c]]");
assert.strictEqual(firstChild.getValue("caption"), "c");

json

🌐 Expand

Save the syntax tree as JSON.

// json (print)
var {lastChild} = Parser.parse(" [[file:a.jpg|1px]]");
assert.deepStrictEqual(lastChild.json(), {
	range: [1, 19],
	type: "file",
	name: "File:A.jpg",
	extension: "jpg",
	childNodes: [
		{
			range: [3, 13],
			type: "link-target",
			childNodes: [
				{
					range: [3, 13],
					data: "file:a.jpg",
				},
			],
		},
		{
			range: [14, 17],
			type: "image-parameter",
			name: "width",
			childNodes: [
				{
					range: [14, 15],
					data: "1",
				},
			],
		},
	],
});

getFrameArgs

Expand

returns: ImageParameterToken[]
Get image frame parameter nodes.

// getFrameArgs (main)
var {firstChild} = Parser.parse("[[file:a|thumb]]");
assert.deepStrictEqual(firstChild.getFrameArgs(), [firstChild.lastChild]);

getHorizAlignArgs

Expand

returns: ImageParameterToken[]
Get image horizontal alignment parameter nodes.

// getHorizAlignArgs (main)
var {firstChild} = Parser.parse("[[file:a|none]]");
assert.deepStrictEqual(firstChild.getHorizAlignArgs(), [firstChild.lastChild]);

getVertAlignArgs

Expand

returns: ImageParameterToken[]
Get image vertical alignment parameter nodes.

// getVertAlignArgs (main)
var {firstChild} = Parser.parse("[[file:a|top]]");
assert.deepStrictEqual(firstChild.getVertAlignArgs(), [firstChild.lastChild]);

setTarget

Expand

param: string target file name
Set target file.

// setTarget (main)
var {firstChild} = Parser.parse("[[file:a]]");
firstChild.setTarget("file:b");
assert.equal(firstChild, "[[file:b]]");

hasArg

Expand

param: string parameter name
returns: boolean
Whether the image has the specified parameter.

// hasArg (main)
var {firstChild} = Parser.parse("[[file:a|b]]");
assert.ok(firstChild.hasArg("caption"));

removeArg

Expand

param: string parameter name
Remove the specified image parameter.

// removeArg (main)
var {firstChild} = Parser.parse("[[file:a|link=b]]");
firstChild.removeArg("link");
assert.equal(firstChild, "[[file:a]]");

getKeys

Expand

returns(): Set<string>
Get image parameter names.

// getKeys (main)
var {firstChild} = Parser.parse("[[file:a|thumb|1px|link=b|alt=c|d]]");
assert.deepStrictEqual(
	firstChild.getKeys(),
	new Set(["thumbnail", "width", "link", "alt", "caption"]),
);

getValues

Expand

param: string parameter name
returns: (string | true)[]
Get specified image parameter values.

// getValues (main)
var {firstChild} = Parser.parse("[[file:a|b|c]]");
assert.deepStrictEqual(firstChild.getValues("caption"), ["b", "c"]);

setValue

Expand

param: string parameter name
param: string | boolean parameter value
Set the specified image parameter.

// setValue (main)
var {firstChild} = Parser.parse("[[file:a|thumb]]");
firstChild.setValue("thumbnail", false);
firstChild.setValue("width", "100");
firstChild.setValue("framed", true);
assert.equal(firstChild, "[[file:a|100px|framed]]");
⚠️ **GitHub.com Fallback** ⚠️