FileToken - bhsd-harry/wikiparser-node GitHub Wiki

目录

Other Languages

简介

文件。这个类继承了 LinkBaseToken 类的全部属性和方法。

✅ 在 MiniBrowser 版本中可用。
🌐 在 Browser 版本中可用。

Properties

extension

✅ 展开

加入的版本:1.5.3

type: string
文件扩展名,只读。

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

name

✅ 展开

type: string
文件页面名,只读。

// 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

展开

type: string | Title
图片链接。

// 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

展开

type: {width: string, height: string}
图片大小。

// 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

展开

type: number
图片宽度。

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

height

展开

type: number
图片高度。

// 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

✅ 展开

returns: LintError[]
报告潜在语法错误。

// 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

✅ 展开

returns: ImageParameterToken[]
获取所有图片参数节点。

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

getArgs

✅ 展开

param: string 参数名
returns: ImageParameterToken[]
获取指定图片参数。

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

getArg

✅ 展开

param: string 参数名
returns: ImageParameterToken
获取生效的指定图片参数。

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

getValue

✅ 展开

param: string 参数名
returns: string | true
获取生效的指定图片参数值。

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

json

🌐 展开

将语法树保存为 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

展开

returns: ImageParameterToken[]
获取图片框架属性参数节点。

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

getHorizAlignArgs

展开

returns: ImageParameterToken[]
获取图片水平对齐参数节点。

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

getVertAlignArgs

展开

returns: ImageParameterToken[]
获取图片垂直对齐参数节点。

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

setTarget

展开

param: string 文件目标
设置文件目标。

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

hasArg

展开

param: string 参数名
returns: boolean
是否具有指定图片参数。

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

removeArg

展开

param: string 参数名
移除指定图片参数。

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

getKeys

展开

returns(): Set<string>
获取图片参数名。

// 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

展开

param: string 参数名
returns: (string | true)[]
获取指定的图片参数值。

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

setValue

展开

param: string 参数名
param: string | boolean 参数值
设置图片参数。

// 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** ⚠️