LLWiki正在建设中,欢迎加入我们!
MediaWiki:Gadget-code-prettify.js:修订间差异
跳转到导航
跳转到搜索
删除的内容 添加的内容
小无编辑摘要 |
小无编辑摘要 |
||
| 第22行: | 第22行: | ||
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(function() { |
.then(function() { |
||
$block.each( hljs.highlightBlock ).addClass( 'highlighted' ).filter( '.linenums' ) |
$block.each(function() { hljs.highlightBlock( this ); }).addClass( 'highlighted' ).filter( '.linenums' ) |
||
.html(function() { |
|||
var lines = $(this).html().replace(/\n$/, '').split('\n'); |
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); } |
||
2020年12月11日 (五) 11:31的版本
//<nowiki>
// 由ResourceLoader直接调用,不可使用ES6语法
// 引自[[moegirl:mediawiki:gadget-code-prettify.js]]和[[moegirl:user:机智的小鱼君/gadget/Highlight.js]]
"use strict";
/*global mw, $, hljs */
const acceptLangs = {js:"javascript", javascript:"javascript", json:"json", css:"css", scribunto:"lua", lua:"lua"},
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) {
console.log('Hook: wikipage.content, 开始执行语法高亮');
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( '.linenums' )
.html(function() {
var lines = $(this).html().replace(/\n$/, '').split('\n');
if (mw.config.get( 'wgNamespaceNumber' ) == 274) { lines = lines.slice(1, -1); }
return $('<ol>', {html: lines.map(function(ele, i) {
return $('<li>', {class:'L' + (i % 2), html:ele, id:'line' + i});
})});
});
mw.hook( 'code.prettify' ).fire($block);
}, function(reason) { console.error('无法获取highlight.js,错误原因:' + reason); });
}
});
//</nowiki>
// [[category:作为模块的小工具]] [[category:阅读工具]] [[category:默认开启的小工具]] [[category:桌面版小工具]] [[category:手机版小工具]]
// {{DEFAULTSORT:code-prettify.js}}