LLWiki正在建设中,欢迎加入我们!
MediaWiki:Gadget-code-prettify.js:修订间差异
跳转到导航
跳转到搜索
删除的内容 添加的内容
无编辑摘要 |
小无编辑摘要 |
||
| 第29行: | 第29行: | ||
if ($block.length) { |
if ($block.length) { |
||
mw.loader.getScript( 'https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js' ) |
mw.loader.getScript( 'https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js' ) |
||
.then(() |
.then(function() { |
||
$block.each(function() { hljs.highlightBlock(this); }).addClass( 'highlighted' ) |
$block.each(function() { hljs.highlightBlock(this); }).addClass( 'highlighted' ) |
||
.filter(function() { return $(this).hasClass( 'linenums' ); }) |
.filter(function() { return $(this).hasClass( 'linenums' ); }) |
||
.html(function() { |
.html(function() { |
||
var lines = $(this).html().replace(/\n$/, '').split('\n'); |
|||
if (mw.config.get( 'wgNamespaceNumber' ) == 274) { lines = lines.slice(1, -1); } |
if (mw.config.get( 'wgNamespaceNumber' ) == 274) { lines = lines.slice(1, -1); } |
||
return '<ol>' + lines.map((ele, i) |
return '<ol>' + lines.map(function(ele, i) { |
||
return '<li class="L' + (i % 2) + '">' + ele + '</li>'; |
|||
}).join('') + '</ol>'; |
|||
}); |
}); |
||
mw.hook( 'code.prettify' ).fire($block); |
mw.hook( 'code.prettify' ).fire($block); |
||
2020年11月23日 (一) 04:07的版本
//<nowiki>
// 由ResourceLoader直接调用,不可使用ES6语法
/* 引自[[moegirl:mediawiki:gadget-code-prettify.js]] */
"use strict";
const acceptLangs = {
js: "javascript",
javascript: "javascript",
json: "json",
css: "css",
scribunto: "lua",
lua: "lua",
html: "html"
},
contentModel = mw.config.get( "wgPageContentModel" ).toLowerCase(),
script = mw.config.get( "wgScript" );
mw.hook( 'wikipage.content' ).add(function($content) {
if (contentModel in acceptLangs) {
$content.find( '.mw-code' ).addClass( 'hljs linenums ' + acceptLangs[contentModel] );
}
$content.find('pre[lang], code[lang]').addClass(function() {
const self = $(this),
lang = self.attr( "lang" ).toLowerCase();
if (lang in acceptLangs) {
return "hljs " + acceptLangs[lang] + (self.is('pre') ? " linenums" : "");
}
});
const $block = $content.find( '.hljs:not(.highlighted)' );
if ($block.length) {
mw.loader.getScript( 'https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js' )
.then(function() {
$block.each(function() { hljs.highlightBlock(this); }).addClass( 'highlighted' )
.filter(function() { return $(this).hasClass( 'linenums' ); })
.html(function() {
var lines = $(this).html().replace(/\n$/, '').split('\n');
if (mw.config.get( 'wgNamespaceNumber' ) == 274) { lines = lines.slice(1, -1); }
return '<ol>' + lines.map(function(ele, i) {
return '<li class="L' + (i % 2) + '">' + ele + '</li>';
}).join('') + '</ol>';
});
mw.hook( 'code.prettify' ).fire($block);
});
}
});
//</nowiki>
// [[category:作为模块的小工具]] [[category:阅读工具]] [[category:默认开启的小工具]] [[category:桌面版小工具]] [[category:手机版小工具]]
// {{DEFAULTSORT:code-prettify.js}}