fix: resolve issues from previous commit about feat(score tag)

This commit is contained in:
Yuchen Mu
2025-03-02 10:23:40 +08:00
parent 6019cc8b7c
commit 576fa5c80e
2 changed files with 43 additions and 48 deletions

View File

@@ -6,12 +6,12 @@
'use strict'
const score = (args, content) => {
// 转义 HTML 标签和部分特殊字符,包括花括号
// Escape HTML tags and some special characters, including curly braces
const escapeHtmlTags = s => {
const lookup = {
'&': '&',
'"': '"',
'\'': ''',
"'": ''',
'<': '&lt;',
'>': '&gt;',
'{': '&#123;',
@@ -19,32 +19,32 @@ const score = (args, content) => {
}
return s.replace(/[&"'<>{}]/g, c => lookup[c])
}
const trimmed = content.trim()
// 使用六个横线作为分隔符拆分内容
// Split content using six dashes as a delimiter
const parts = trimmed.split('------')
if (parts.length < 2) {
// 如果没有分隔符,直接将整个内容作为乐谱内容处理
// If no delimiter is found, treat the entire content as the score
return `<div class="abc-music-sheet">${escapeHtmlTags(trimmed)}</div>`
}
// 第一部分为参数JSON 字符串),其余部分作为乐谱内容
// First part is parameters (JSON string), the rest is the score content
const paramPart = parts[0].trim()
const scorePart = parts.slice(1).join('------').trim()
let paramsObj = {}
try {
paramsObj = JSON.parse(paramPart)
} catch (e) {
console.error('解析 score 标签中的参数 JSON 失败:', e)
console.error("Failed to parse JSON in score tag:", e)
}
// 使用双引号包裹 data-params 的属性值,确保 JSON 内部的双引号已被转义
// Use double quotes for data-params attribute value,
// ensuring JSON internal double quotes are escaped
return `<div class="abc-music-sheet" data-params="${escapeHtmlTags(JSON.stringify(paramsObj))}">
${escapeHtmlTags(scorePart)}
</div>`
}
hexo.extend.tag.register('score', score, { ends: true })
hexo.extend.tag.register("score", score, { ends: true })