MediaWiki:Gadget-code-prettify.js

来自维阿百科
跳转至: 导航搜索

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

  • 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),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
/* eslint-disable prefer-arrow-callback */
/* eslint-disable prefer-template */
/* eslint-disable comma-dangle */
/* eslint-disable no-var */
/* eslint dot-notation: ["error", { "allowPattern": "^(?:catch|default)$" } ] */
/* eslint-disable no-unused-vars */
/* eslint-disable no-redeclare */
/* global mw, $, OO, moment, Cron, prettyPrint */
/* eslint-enable no-unused-vars */
/* eslint-enable no-redeclare */
"use strict";
// <pre>
$(function () {
    if (mw.config.get("wgPageName").match(/\.js$/)) {
        $(".mw-code").addClass("prettyprint lang-js");
    }
    if (mw.config.get("wgPageName").match(/\.css$/)) {
        $(".mw-code").addClass("prettyprint lang-css");
    }
    var acceptsLangs = {
        ts: "ts",
        typescript: "ts",
        js: "js",
        javascript: "js",
        json: "json",
        css: "css",
        htm: "html",
        html: "html",
        xml: "xml",
        scribunto: "lua",
        lua: "lua",
        php: "php",
        regex: "regex",
        latex: "latex",
        tex: "latex",
        wiki: "wiki",
        wikitext: "wiki",
        mediawiki: "wiki",
        mw: "wiki"
    };
    var wgPageContentModel = mw.config.get("wgPageContentModel", "").toLowerCase();
    if (wgPageContentModel in acceptsLangs) {
        $(".mw-code").addClass("prettyprint lang-" + acceptsLangs[wgPageContentModel]);
    }
    $("pre[lang]").each(function () {
        var self = $(this);
        var lang = self.attr("lang").toLowerCase();
        if (lang in acceptsLangs) {
            self.addClass("prettyprint lang-" + acceptsLangs[lang]);
        }
    });
    if ($('.prettyprint[class*=" lang-"]').length > 0) {
        $('pre.prettyprint[class*=" lang-"]').each(function (_, ele) {
            var start = ele.dataset.start;
            if (/^[1-9]\d*$/.test(start)) {
                $(ele).removeClass("linenums").addClass("linenums:" + start);
            } else {
                $(ele).addClass("linenums");
            }
        });
        $.ajax({
            url: mw.config.get("wgServer") + mw.config.get("wgScriptPath") + "/index.php?title=MediaWiki:Gadget-code-prettify-core.js&action=raw&ctype=text/javascript",
            dataType: "script",
            cache: true,
            success: function () {
                prettyPrint();
                if (mw.config.get("wgPageName").match(/\.(js|css)$/)) {
                    $(window).on("hashchange", function () {
                        var frag = new mw.Uri().fragment;
                        if (/^L\d+$/.test(frag)) {
                            var firstCode = $("#" + frag)[0] || $(".prettyprint.prettyprinted > .linenums").first().children().eq(+frag.substring(1) - 1)[0];
                            if (firstCode) {
                                var $firstCode = $(firstCode).addClass("linenums-active");
                                $("html, body").animate({
                                    scrollTop: $firstCode.offset().top - $firstCode.outerHeight(),
                                });
                            }
                        }
                    }).trigger("hashchange");
                }
            }
        });
    }
});
// </pre>