MediaWiki:Gadget-prism-inline-color.js

来自萌娘共享
AnnAngela-dbot讨论 | 贡献2024年4月8日 (一) 18:35的版本 (代码变动:2d5d0ae - feat(Gadget/prism): 新的代码高亮工具 (#443) by U:AnnAngela, co-authored-by: GH:github-actions[bot],U:Bhsd
跳转至: 导航搜索

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-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-inline-color.js|user=[[U:AnnAngela]]|co-authors=GH:github-actions[bot]、[[U:Bhsd]]|longId=2d5d0ae30421796cfa323a02a78713c0ed261417|shortId=2d5d0ae|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 HTML_TAG = /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/g;
    var HEX_COLOR = /^#?((?:[\da-f]){3,4}|(?:[\da-f]{2}){3,4})$/i;
    function parseHexColor(hex) {
        var match = HEX_COLOR.exec(hex);
        if (!match) {
            return undefined;
        }
        hex = match[1];
        var channelWidth = hex.length >= 6 ? 2 : 1;
        var channelCount = hex.length / channelWidth;
        var scale = channelWidth == 1 ? 1 / 15 : 1 / 255;
        var channels = [];
        for (var i = 0; i < channelCount; i++) {
            var int = parseInt(hex.substr(i * channelWidth, channelWidth), 16);
            channels.push(int * scale);
        }
        if (channelCount == 3) {
            channels.push(1);
        }
        var rgb = channels.slice(0, 3).map(function (x) {
            return String(Math.round(x * 255));
        }).join(',');
        var alpha = String(Number(channels[3].toFixed(3)));
        return 'rgba(' + rgb + ',' + alpha + ')';
    }
    function validateColor(color) {
        var s = new Option().style;
        s.color = color;
        return s.color ? color : undefined;
    }
    var parsers = [
        parseHexColor,
        validateColor
    ];
    Prism.hooks.add('wrap', function (env) {
        if (env.type === 'color' || env.classes.indexOf('color') >= 0) {
            var content = env.content;
            var rawText = content.split(HTML_TAG).join('');
            var color;
            for (var i = 0, l = parsers.length; i < l && !color; i++) {
                color = parsers[i](rawText);
            }
            if (!color) {
                return;
            }
            var previewElement = '<span class="inline-color-wrapper"><span class="inline-color" style="background-color:' + color + ';"></span></span>';
            env.content = previewElement + content;
        }
    });
}()); 

/* </pre> */