Skip to content

FileToken (EN)

bhsd edited this page Jul 4, 2024 · 13 revisions
Table of Contents

Other Languages

Introduction

File。

✅ Available in the Mini and Browser versions.

Properties

extension

✅ Expand

version added: 1.5.3

type: string
File extension, read-only.

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

name

Expand

type: string
File name,read-only.

// name (main)
var {firstChild} = Parser.parse('[[file:a]]');
assert.strictEqual(firstChild.name, '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'));
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]]');
assert.deepStrictEqual(firstChild.lint(), [
	{
		rule: 'no-duplicate',
		severity: 'error',
		message: 'duplicated image caption parameter',
		startLine: 0,
		startCol: 44,
		startIndex: 44,
		endLine: 0,
		endCol: 45,
		endIndex: 45,
	},
	{
		rule: 'no-duplicate',
		severity: 'error',
		message: 'duplicated image caption parameter',
		startLine: 0,
		startCol: 46,
		startIndex: 46,
		endLine: 0,
		endCol: 47,
		endIndex: 47,
	},
	{
		rule: 'no-duplicate',
		severity: 'error',
		message: 'conflicting image frame parameter',
		startLine: 0,
		startCol: 9,
		startIndex: 9,
		endLine: 0,
		endCol: 18,
		endIndex: 18,
	},
	{
		rule: 'no-duplicate',
		severity: 'error',
		message: 'conflicting image frame parameter',
		startLine: 0,
		startCol: 19,
		startIndex: 19,
		endLine: 0,
		endCol: 25,
		endIndex: 25,
	},
	{
		rule: 'no-duplicate',
		severity: 'error',
		message: 'conflicting image horizontal-alignment parameter',
		startLine: 0,
		startCol: 26,
		startIndex: 26,
		endLine: 0,
		endCol: 30,
		endIndex: 30,
	},
	{
		rule: 'no-duplicate',
		severity: 'error',
		message: 'conflicting image horizontal-alignment parameter',
		startLine: 0,
		startCol: 31,
		startIndex: 31,
		endLine: 0,
		endCol: 35,
		endIndex: 35,
	},
	{
		rule: 'no-duplicate',
		severity: 'error',
		message: 'conflicting image vertical-alignment parameter',
		startLine: 0,
		startCol: 36,
		startIndex: 36,
		endLine: 0,
		endCol: 39,
		endIndex: 39,
	},
	{
		rule: 'no-duplicate',
		severity: 'error',
		message: 'conflicting image vertical-alignment parameter',
		startLine: 0,
		startCol: 40,
		startIndex: 40,
		endLine: 0,
		endCol: 43,
		endIndex: 43,
	},
]);

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

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

cloneNode

Expand

returns: this
Deep clone the node.

// cloneNode (main)
var {firstChild} = Parser.parse('[[file:a]]');
assert.deepStrictEqual(firstChild.cloneNode(), firstChild);

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