Site Notice
hello, world
Difference between revisions of "MediaWiki:Gadget-Highlight.js"
From Project-EPB Commons
([InPageEdit] 没有编辑摘要) |
m ([InPageEdit] 没有编辑摘要) |
||
Line 40: | Line 40: | ||
$.each(splitHtml, function (lineNum, lineContent) { | $.each(splitHtml, function (lineNum, lineContent) { | ||
lineNum = lineNum + 1 | lineNum = lineNum + 1 | ||
+ | if (lineNum === splitHtml.length) return // 最后一个行不要再添加空的一行了 | ||
$html.find('.line-content').append( | $html.find('.line-content').append( | ||
$('<div>', { class: 'line-row', html: lineContent }) | $('<div>', { class: 'line-row', html: lineContent }) |
Revision as of 20:42, 29 October 2020
/**
* @name highlight.js
* @description 为页面内的代码块提供语法高亮
*/
!(function () {
// 为代码页面添加class
var pageName = mw.config.get('wgPageName')
if (pageName.substr(pageName.length - 3, 3) === '.js') {
$('#mw-content-text pre').addClass('lang-js highlight linenums')
} else if (pageName.substr(pageName.length - 4, 4) === '.css') {
$('#mw-content-text pre').addClass('lang-css highlight linenums')
} else if (mw.config.get('wgNamespaceNumber') === 828) {
$('#mw-content-text pre').addClass('lang-lua highlight linenums')
}
// 加载主题
mw.loader.load('https://cdn.jsdelivr.net/npm/[email protected]/styles/solarized-light.css', 'text/css')
// 加载脚本
$.ajax({
url: 'https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js',
dataType: 'script',
cache: true
}).then(function () {
// 初始化高亮
$('#mw-content-text pre.highlight').each(function (_, block) {
hljs.highlightBlock(block)
// 添加行号
var $block = $(block)
if ($block.hasClass('linenums')) {
$block.html(function () {
var splitHtml = $(this).html().split('\n')
var $html = $('<div>', { class: 'line-container' }).append(
$('<div>', { class: 'line-content' }),
$('<div>', { class: 'line-numbers' })
)
$.each(splitHtml, function (lineNum, lineContent) {
lineNum = lineNum + 1
if (lineNum === splitHtml.length) return // 最后一个行不要再添加空的一行了
$html.find('.line-content').append(
$('<div>', { class: 'line-row', html: lineContent })
)
$html.find('.line-numbers').append(
$('<div>', { class: 'line-number-block', text: lineNum })
)
})
return $html
})
}
})
})
})()