MediaWiki:Gadget-CodeLinks.js
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:按 Ctrl-F5。
/**
* SPDX-License-Identifier: CC-BY-SA-4.0
* _addText: '{{Gadget Header|license=CC-BY-SA-4.0}}'
*
* @base {@link https://en.wiktionary.org/wiki/MediaWiki:Gadget-CodeLinks.js}
* @base {@link https://zh.wiktionary.org/wiki/MediaWiki:Gadget-CodeLinks.js}
* @source {@link https://git.qiuwen.net.cn/InterfaceAdmin/QiuwenGadgets/src/branch/master/src/CodeLinks}
* @license CC-BY-SA-4.0 {@link https://www.qiuwenbaike.cn/wiki/H:CC-BY-SA-4.0}
*/
/**
* +------------------------------------------------------------+
* | === WARNING: GLOBAL GADGET FILE === |
* +------------------------------------------------------------+
* | All changes should be made in the repository, |
* | otherwise they will be lost. |
* +------------------------------------------------------------+
* | Changes to this page may affect many users. |
* | Please discuss changes by opening an issue before editing. |
* +------------------------------------------------------------+
*/
/* <nowiki> */
(() => {
"use strict";
// dist/CodeLinks/CodeLinks.js
function _createForOfIteratorHelper(r, e) {
var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
if (!t) {
if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
t && (r = t);
var n = 0, F = function() {
};
return { s: F, n: function() {
return n >= r.length ? { done: true } : { done: false, value: r[n++] };
}, e: function(r2) {
throw r2;
}, f: F };
}
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var o, a = true, u = false;
return { s: function() {
t = t.call(r);
}, n: function() {
var r2 = t.next();
return a = r2.done, r2;
}, e: function(r2) {
u = true, o = r2;
}, f: function() {
try {
a || null == t.return || t.return();
} finally {
if (u) throw o;
}
} };
}
function _unsupportedIterableToArray(r, a) {
if (r) {
if ("string" == typeof r) return _arrayLikeToArray(r, a);
var t = {}.toString.call(r).slice(8, -1);
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
}
}
function _arrayLikeToArray(r, a) {
(null == a || a > r.length) && (a = r.length);
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
return n;
}
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
var _iterator = _createForOfIteratorHelper(__getOwnPropNames(from)), _step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done; ) {
let key = _step.value;
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target,
mod
));
//! src/CodeLinks/modules/addLink.tsx
var import_ext_gadget = __toESM(require("ext.gadget.React"), 1);
var addLink = (element, targetTitle) => {
if (!(element instanceof Element)) {
throw new TypeError("Expected Element interface");
}
const {
firstChild
} = element;
if (!(firstChild instanceof Text)) {
throw new TypeError("Expected Text interface");
}
element.append(/* @__PURE__ */ import_ext_gadget.default.createElement("a", {
href: mw.util.getUrl(targetTitle)
}, firstChild));
};
//! src/CodeLinks/CodeLinks.ts
var import_ext_gadget3 = require("ext.gadget.Util");
//! src/CodeLinks/modules/constant.ts
/**
* @preserve
* @author John Gruber
* @copyright 2002–2023 The Daring Fireball Company LLC.
* @see {@link https://daringfireball.net/2010/07/improved_regex_for_matching_urls}
*/
var REGEX_URL = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()[\]{};:'".,<>?«»“”‘’]))/i;
//! src/CodeLinks/modules/processComment.tsx
var import_ext_gadget2 = __toESM(require("ext.gadget.React"), 1);
var processComment = (element) => {
var _index, _ref;
const {
firstChild
} = element;
if (!(firstChild instanceof Text)) {
throw new TypeError("Expected Text interface");
}
const {
textContent
} = firstChild;
if (!textContent) {
return;
}
const templateMatch = /(\{\{(?:#invoke:)?)([^|{}[\]\n#]+)(?=\||}})/i.exec(textContent);
const wikilinkMatch = /\[\[([^|{}[\]\n]+)?(?:\|.*?)?]]/.exec(textContent);
const urlMatch = REGEX_URL.exec(textContent);
if (!(templateMatch || wikilinkMatch || urlMatch)) {
return;
}
const link = /* @__PURE__ */ import_ext_gadget2.default.createElement("a", {
className: "code-link"
});
let linkText = "";
let start = (_index = (_ref = wikilinkMatch || templateMatch || urlMatch) === null || _ref === void 0 ? void 0 : _ref.index) !== null && _index !== void 0 ? _index : 0;
if (urlMatch) {
const [url] = urlMatch;
linkText = url;
link.href = url;
} else {
let fullPageName = "";
if (wikilinkMatch) {
[linkText] = wikilinkMatch;
fullPageName = wikilinkMatch[1];
} else if (templateMatch) {
const prefix = templateMatch[1];
const pageName = templateMatch[2];
fullPageName = "".concat(prefix === "{{#invoke:" ? "Module:" : "Template:").concat(pageName);
linkText = pageName;
link.title = fullPageName;
start += prefix.length;
}
link.href = mw.util.getUrl(fullPageName);
}
link.append(document.createTextNode(linkText));
firstChild.before(link);
const afterLink = textContent.slice(Math.max(0, start + linkText.length));
firstChild.textContent = afterLink;
const beforeLink = textContent.slice(0, Math.max(0, start));
element.insertBefore(document.createTextNode(beforeLink), link);
};
//! src/CodeLinks/CodeLinks.ts
void (0, import_ext_gadget3.getBody)().then(function codeLinks($body) {
var _iterator2 = _createForOfIteratorHelper($body.find(".mw-highlight")), _step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done; ) {
const codeBlock = _step2.value;
for (var _i2 = 0, _arr2 = ["c", "c1", "cm"]; _i2 < _arr2.length; _i2++) {
const commentClass = _arr2[_i2];
var _iterator5 = _createForOfIteratorHelper(codeBlock.querySelectorAll(".".concat(commentClass))), _step5;
try {
for (_iterator5.s(); !(_step5 = _iterator5.n()).done; ) {
const comment = _step5.value;
processComment(comment);
}
} catch (err) {
_iterator5.e(err);
} finally {
_iterator5.f();
}
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
const classes = {
identifier: "n",
functionName: "nb",
singleQuoteString: "s1",
doubleQuoteString: "s2"
};
const moduleNameElementArray = [];
var _iterator3 = _createForOfIteratorHelper($body.find(".".concat(classes.functionName))), _step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done; ) {
var _functionNameElement$, _nextElement$firstChi, _nextElement$firstChi3, _nextElementFirstValu;
const functionNameElement = _step3.value;
const text = (_functionNameElement$ = functionNameElement.firstChild) === null || _functionNameElement$ === void 0 ? void 0 : _functionNameElement$.nodeValue;
if (text !== "require") {
continue;
}
let nextElement = functionNameElement.nextElementSibling;
if (!nextElement) {
continue;
}
let nextElementFirstValue = (_nextElement$firstChi = nextElement.firstChild) === null || _nextElement$firstChi === void 0 ? void 0 : _nextElement$firstChi.nodeValue;
const hasParenthesis = nextElementFirstValue === "(";
if (hasParenthesis) {
var _nextElement$firstChi2;
nextElement = nextElement.nextElementSibling;
if (!nextElement) {
continue;
}
nextElementFirstValue = (_nextElement$firstChi2 = nextElement.firstChild) === null || _nextElement$firstChi2 === void 0 ? void 0 : _nextElement$firstChi2.nodeValue;
}
const {
classList
} = nextElement;
if (!(classList.contains(classes.singleQuoteString) || classList.contains(classes.doubleQuoteString))) {
continue;
}
const element = nextElement;
const elementValue = nextElementFirstValue;
if (!elementValue) {
continue;
}
nextElement = nextElement.nextElementSibling;
if (!nextElement) {
continue;
}
nextElementFirstValue = (_nextElement$firstChi3 = nextElement.firstChild) === null || _nextElement$firstChi3 === void 0 ? void 0 : _nextElement$firstChi3.nodeValue;
if (hasParenthesis && ((_nextElementFirstValu = nextElementFirstValue) === null || _nextElementFirstValu === void 0 ? void 0 : _nextElementFirstValu[0]) !== ")") {
continue;
}
moduleNameElementArray[moduleNameElementArray.length] = element;
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
const dataModuleNameElementArray = [];
var _iterator4 = _createForOfIteratorHelper($body.find(".".concat(classes.singleQuoteString, ",.").concat(classes.doubleQuoteString))), _step4;
try {
for (_iterator4.s(); !(_step4 = _iterator4.n()).done; ) {
var _element$firstChild2, _prevElement$firstChi, _prevElement$firstChi3, _prevElement$firstChi4;
const element = _step4.value;
if (moduleNameElementArray.includes(element)) {
continue;
}
const elementFirstValue = (_element$firstChild2 = element.firstChild) === null || _element$firstChild2 === void 0 ? void 0 : _element$firstChild2.nodeValue;
if (!elementFirstValue || !/^["'](?:module|模[组組块]):/i.test(elementFirstValue)) {
continue;
}
let prevElement = element.previousElementSibling;
if (!prevElement) {
continue;
}
let prevElementFirstValue = (_prevElement$firstChi = prevElement.firstChild) === null || _prevElement$firstChi === void 0 ? void 0 : _prevElement$firstChi.nodeValue;
if (prevElementFirstValue === "(") {
var _nextElement$firstChi4, _prevElement$firstChi2;
const nextElement = element.nextElementSibling;
if (!nextElement) {
continue;
}
const nextElementFirstValue = (_nextElement$firstChi4 = nextElement.firstChild) === null || _nextElement$firstChi4 === void 0 ? void 0 : _nextElement$firstChi4.nodeValue;
if ((nextElementFirstValue === null || nextElementFirstValue === void 0 ? void 0 : nextElementFirstValue[0]) !== ")") {
continue;
}
prevElement = prevElement.previousElementSibling;
if (!prevElement) {
continue;
}
prevElementFirstValue = (_prevElement$firstChi2 = prevElement.firstChild) === null || _prevElement$firstChi2 === void 0 ? void 0 : _prevElement$firstChi2.nodeValue;
}
if (prevElementFirstValue !== "loadData") {
continue;
}
prevElement = prevElement.previousElementSibling;
if (!prevElement) {
continue;
}
prevElementFirstValue = (_prevElement$firstChi3 = prevElement.firstChild) === null || _prevElement$firstChi3 === void 0 ? void 0 : _prevElement$firstChi3.nodeValue;
if (prevElementFirstValue !== ".") {
continue;
}
prevElement = prevElement.previousElementSibling;
if (!prevElement) {
continue;
}
prevElementFirstValue = (_prevElement$firstChi4 = prevElement.firstChild) === null || _prevElement$firstChi4 === void 0 ? void 0 : _prevElement$firstChi4.nodeValue;
if (prevElementFirstValue !== "mw") {
continue;
}
dataModuleNameElementArray[dataModuleNameElementArray.length] = element;
}
} catch (err) {
_iterator4.e(err);
} finally {
_iterator4.f();
}
for (var _i = 0, _arr = [...moduleNameElementArray, ...dataModuleNameElementArray]; _i < _arr.length; _i++) {
var _element$firstChild;
const element = _arr[_i];
const elementFirstValue = (_element$firstChild = element.firstChild) === null || _element$firstChild === void 0 ? void 0 : _element$firstChild.nodeValue;
if (!elementFirstValue) {
continue;
}
const moduleName = elementFirstValue.slice(1, -1);
const targetTitle = /^(?:module|模[组組块])?:/i.test(moduleName) ? moduleName : "Help:Lua#".concat(moduleName);
addLink(element, targetTitle);
}
});
})();