你现在正在访问的是萌娘百科用于保存和管理图片、音频等媒体文件的子项目萌娘共享,想要回到萌娘百科,请使用浏览器上的返回按钮、或点击这里
MediaWiki:Gadget-prism-line-numbers.js
AnnAngela-dbot(讨论 | 贡献)2024年4月16日 (二) 09:06的版本 (代码变动:2d5d0ae3 - feat(Gadget/prism): 新的代码高亮工具 (#443) by U:AnnAngela, co-authored-by: GH:github-actions[bot],U:Bhsd)
注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件。
/** * ------------------------------------------------------------------------- * !!! DON'T MODIFY THIS PAGE MANUALLY, YOUR CHANGES WILL BE OVERWRITTEN !!! * ------------------------------------------------------------------------- */ var _addText = '{{GHIACode|page=GHIA:MoegirlPediaInterfaceCodes/blob/master/src/gadgets/prism-core/MediaWiki:Gadget-prism-line-numbers.js|user=[[U:AnnAngela]]|co-authors=GH:github-actions[bot]、[[U:Bhsd]]|longId=2d5d0ae30421796cfa323a02a78713c0ed261417|shortId=2d5d0ae3|summary=feat(Gadget/prism): 新的代码高亮工具 (#443)|body=<nowiki>Co-authored-by: github-actions[bot] <41898282+github-actions[bot]📧users.noreply.github.com>↩Co-authored-by: Bhsd <55071315+bhsd-harry📧users.noreply.github.com></nowiki>}}'; /* <pre> */ "use strict"; (function () { if (typeof Prism === 'undefined' || typeof document === 'undefined') { return; } var PLUGIN_NAME = 'line-numbers'; var NEW_LINE_EXP = /\n(?!$)/g; var config = Prism.plugins.lineNumbers = { getLine: function (element, number) { if (element.tagName !== 'PRE' || !element.classList.contains(PLUGIN_NAME)) { return; } var lineNumberRows = element.querySelector('.line-numbers-rows'); if (!lineNumberRows) { return; } var lineNumberStart = parseInt(element.getAttribute('data-start'), 10) || 1; var lineNumberEnd = lineNumberStart + (lineNumberRows.children.length - 1); if (number < lineNumberStart) { number = lineNumberStart; } if (number > lineNumberEnd) { number = lineNumberEnd; } var lineIndex = number - lineNumberStart; return lineNumberRows.children[lineIndex]; }, resize: function (element) { resizeElements([element]); }, assumeViewportIndependence: true }; function resizeElements(elements) { elements = elements.filter(function (e) { var codeStyles = getStyles(e); var whiteSpace = codeStyles['white-space']; return whiteSpace === 'pre-wrap' || whiteSpace === 'pre-line'; }); if (elements.length == 0) { return; } var infos = elements.map(function (element) { var codeElement = element.querySelector('code'); var lineNumbersWrapper = element.querySelector('.line-numbers-rows'); if (!codeElement || !lineNumbersWrapper) { return undefined; } var lineNumberSizer = element.querySelector('.line-numbers-sizer'); var codeLines = codeElement.textContent.split(NEW_LINE_EXP); if (!lineNumberSizer) { lineNumberSizer = document.createElement('span'); lineNumberSizer.className = 'line-numbers-sizer'; codeElement.appendChild(lineNumberSizer); } lineNumberSizer.innerHTML = '0'; lineNumberSizer.style.display = 'block'; var oneLinerHeight = lineNumberSizer.getBoundingClientRect().height; lineNumberSizer.innerHTML = ''; return { element: element, lines: codeLines, lineHeights: [], oneLinerHeight: oneLinerHeight, sizer: lineNumberSizer }; }).filter(Boolean); infos.forEach(function (info) { var lineNumberSizer = info.sizer; var lines = info.lines; var lineHeights = info.lineHeights; var oneLinerHeight = info.oneLinerHeight; lineHeights[lines.length - 1] = undefined; lines.forEach(function (line, index) { if (line && line.length > 1) { var e = lineNumberSizer.appendChild(document.createElement('span')); e.style.display = 'block'; e.textContent = line; } else { lineHeights[index] = oneLinerHeight; } }); }); infos.forEach(function (info) { var lineNumberSizer = info.sizer; var lineHeights = info.lineHeights; var childIndex = 0; for (var i = 0; i < lineHeights.length; i++) { if (lineHeights[i] === undefined) { lineHeights[i] = lineNumberSizer.children[childIndex++].getBoundingClientRect().height; } } }); infos.forEach(function (info) { var lineNumberSizer = info.sizer; var wrapper = info.element.querySelector('.line-numbers-rows'); lineNumberSizer.style.display = 'none'; lineNumberSizer.innerHTML = ''; info.lineHeights.forEach(function (height, lineNumber) { wrapper.children[lineNumber].style.height = height + 'px'; }); }); } function getStyles(element) { if (!element) { return null; } return window.getComputedStyle ? getComputedStyle(element) : (element.currentStyle || null); } var lastWidth = undefined; window.addEventListener('resize', function () { if (config.assumeViewportIndependence && lastWidth === window.innerWidth) { return; } lastWidth = window.innerWidth; resizeElements(Array.prototype.slice.call(document.querySelectorAll('pre.' + PLUGIN_NAME))); }); Prism.hooks.add('complete', function (env) { if (!env.code) { return; } var code = (env.element); var pre = (code.parentNode); if (!pre || !/pre/i.test(pre.nodeName)) { return; } if (code.querySelector('.line-numbers-rows')) { return; } if (!Prism.util.isActive(code, PLUGIN_NAME)) { return; } code.classList.remove(PLUGIN_NAME); pre.classList.add(PLUGIN_NAME); var match = env.code.match(NEW_LINE_EXP); var linesNum = match ? match.length + 1 : 1; var lineNumbersWrapper; var lines = new Array(linesNum + 1).join('<span></span>'); lineNumbersWrapper = document.createElement('span'); lineNumbersWrapper.setAttribute('aria-hidden', 'true'); lineNumbersWrapper.className = 'line-numbers-rows'; lineNumbersWrapper.innerHTML = lines; if (pre.hasAttribute('data-start')) { pre.style.counterReset = 'linenumber ' + (parseInt(pre.getAttribute('data-start'), 10) - 1); } env.element.appendChild(lineNumbersWrapper); resizeElements([pre]); Prism.hooks.run('line-numbers', env); }); Prism.hooks.add('line-numbers', function (env) { env.plugins = env.plugins || {}; env.plugins.lineNumbers = true; }); }()); /* </pre> */