LLWiki正在建设中,欢迎加入我们!
MediaWiki:Gadget-code-prettify.js:修订间差异
跳转到导航
跳转到搜索
删除的内容 添加的内容
小 // 使用Wikiplus小工具快速编辑 标签:移动版编辑 移动版网页编辑 |
小无编辑摘要 |
||
| 第4行: | 第4行: | ||
"use strict"; |
"use strict"; |
||
/*global mw, $, hljs */ |
/*global mw, $, hljs */ |
||
const acceptLangs = { |
const acceptLangs = {js:"javascript", javascript:"javascript", json:"json", css:"css", scribunto:"lua", lua:"lua"}, |
||
javascript: "javascript", |
|||
json: "json", |
|||
css: "css", |
|||
scribunto: "lua", |
|||
lua: "lua", |
|||
html: "html" |
|||
}, |
|||
contentModel = mw.config.get( "wgPageContentModel" ).toLowerCase(), |
contentModel = mw.config.get( "wgPageContentModel" ).toLowerCase(), |
||
script = mw.config.get( "wgScript" ); |
script = mw.config.get( "wgScript" ); |
||
| 第22行: | 第15行: | ||
const self = $(this), |
const self = $(this), |
||
lang = self.attr( "lang" ).toLowerCase(); |
lang = self.attr( "lang" ).toLowerCase(); |
||
if (lang in acceptLangs) { |
if (lang in acceptLangs) { return "hljs " + acceptLangs[lang] + (self.is('pre') ? " linenums" : ""); } |
||
return "hljs " + acceptLangs[lang] + (self.is('pre') ? " linenums" : ""); |
|||
} |
|||
}); |
}); |
||
const $block = $content.find( '.hljs:not(.highlighted)' ); |
const $block = $content.find( '.hljs:not(.highlighted)' ); |
||
| 第31行: | 第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( |
$block.each( hljs.highlightBlock ).addClass( 'highlighted' ).filter( '.linenums' ).html(function() { |
||
.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); } |
||
return '<ol>' |
return $('<ol>', {html: lines.map(function(ele, i) { |
||
return '<li class |
return $('<li>', {class:'L' + (i % 2), html:ele, id:'line' + i}); |
||
}) |
})}); |
||
}); |
}); |
||
mw.hook( 'code.prettify' ).fire($block); |
mw.hook( 'code.prettify' ).fire($block); |
||
2020年12月11日 (五) 11:30的版本
//<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( hljs.highlightBlock ).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}}