let postContextInitial = false
const postContext = {
/* 初始化代码块 */
initCodeBlock() {
const $code = $('*:not(figure) > pre > code')
if ($code.length === 0) return
$code.each(function (index) {
const $pre = $(this).parent()
let clazz = $(this).attr('class')
// 通过class初始化代码块标题和是否默认关闭
let title = ''
let lines = false
let isClose = false
if (clazz != null) {
let str1 = clazz.match(/[|<](.*)$/)
let str2 = clazz.match(/:select/)
if (str1 || str2) {
let num = 0
if (str2) {
num = str2.index
if (str1) {
if (str1[1].endsWith(str2[0])) {
str1[1] = str1[1].substring(0, str1[1].length - str2[0].length)
}
} else {
title = clazz.substring(9, str2.index)
}
lines = true
}
if (str1) {
num = str1.index < num ? str1.index : num
if (str1[0][0] === '<') {
isClose = true
}
title = str1[1]
}
$(this).attr('class', clazz.substring(0, num))
if (!title) title = clazz.substring(9, num)
} else {
title = clazz.substring(9)
}
}
// 生成行号
let codes = $(this).text().split('\n') || []
let nums = codes.length - 1
let lineDigit = String(nums).length
if (lineDigit === 1) lineDigit = 2
let lis = ''
for (var i = 0; i < nums; i++) {
lis += `
${String(i + 1).padStart(lineDigit, 0)}
`
}
if (lines) {
$(this).text($(this).text().replace(/(^\s*)\|\+\s/gm,'$1'))
}
// 代码块的id,用于代码块复制和折叠
let id = `codeBlock${index}-${new Date().getTime()}`
let close = ''
if (isClose) {
close = ' close'
$(this).parent().hide()
}
// 生成标题栏的按钮
let titleButton = `
`
// 组装代码块
$(this).attr('id', id)
$pre.prepend(`
${lis}
`)
if (nums > DreamConfig.code_fold_line) {
$pre.wrap('').append('