-
Notifications
You must be signed in to change notification settings - Fork 3
Examples (external link)
Bhsd edited this page Apr 15, 2026
·
5 revisions
展开
你可以将页面中已知域名的 http:// 链接替换为 https:// 链接。此示例演示了 MagicLinkToken 的用法。
// Replacing non-secure URLs with secure ones (main)
// 包含非安全 URL 的页面内容
var content = `http://www.mediawiki.org/wiki/Foo
[http://www.mediawiki.org/wiki/Bar Baz]`,
root = Parser.parse(content);
for (const link of root.querySelectorAll('free-ext-link, ext-link-url')) {
try {
// 如果 URL 无效可能会抛出错误
const url = link.getUrl();
if (url.host === 'www.mediawiki.org' && url.protocol === 'http:') {
link.protocol = 'https://';
}
} catch {}
}
assert.equal(
root,
`https://www.mediawiki.org/wiki/Foo
[https://www.mediawiki.org/wiki/Bar Baz]`,
);展开
你可以将页面中的裸链接替换为引用模板。此示例演示了 MagicLinkToken 和 ExtLinkToken 的用法。
// Replacing bare URLs with citation templates (main)
// 包含裸链接的页面内容
var content = `<ref>https://www.mediawiki.org/wiki/Foo</ref>
<ref>[https://www.mediawiki.org/wiki/Bar Baz]</ref>`,
root = Parser.parse(content),
links = root.querySelectorAll(
'ext-inner#ref > ext-link, ext-inner#ref > free-ext-link',
);
for (const link of links) {
if (link.type === 'ext-link' && link.length === 2) {
link.replaceWith(`{{Cite web|url=${link.link}|title=${link.innerText}}}`);
} else {
link.replaceWith(`{{Cite web|url=${link.link}}}`);
}
}
assert.equal(
root,
`<ref>{{Cite web|url=https://www.mediawiki.org/wiki/Foo}}</ref>
<ref>{{Cite web|url=https://www.mediawiki.org/wiki/Bar|title=Baz}}</ref>`,
);展开
你可以将已弃用的 ISBN 魔术链接替换为模板。此示例演示了 MagicLinkToken 的用法。
// Replacing bare URLs with citation templates (main)
// 包含 ISBN 魔术链接的页面内容
var content = 'ISBN 1234567890',
root = Parser.parse(content);
for (const isbn of root.querySelectorAll('magic-link[protocol=ISBN]')) {
isbn.replaceWith(`{{ISBN|${isbn.link.slice(5)}}}`);
}
assert.equal(
root,
'{{ISBN|1234567890}}',
);对维基文本批量执行语法检查的命令行工具
轻量级的维基模板解析器
维基文本语言服务器协议实现
用于维基文本的 VS Code 扩展
A command-line tool that performs linting on Wikitext in bulk
A lightweight Wikitext template parser
An implementation of the Language Server Protocol for Wikitext
VS Code extension for Wikitext