mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-04-12 22:17:06 +08:00
fix: resolve issues from previous commit about feat(score tag)
This commit is contained in:
57
layout/includes/third-party/abcjs/abcjs.pug
vendored
57
layout/includes/third-party/abcjs/abcjs.pug
vendored
@@ -1,51 +1,46 @@
|
|||||||
script.
|
script.
|
||||||
(function() {
|
(function() {
|
||||||
var abcjsInit = function() {
|
const abcjsInit = function() {
|
||||||
var abcjsFn = function() {
|
const abcjsFn = function() {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
var sheets = document.querySelectorAll(".abc-music-sheet");
|
const sheets = document.querySelectorAll(".abc-music-sheet")
|
||||||
for (var i = 0; i < sheets.length; i++) {
|
for (let i = 0; i < sheets.length; i++) {
|
||||||
var ele = sheets[i];
|
const ele = sheets[i]
|
||||||
if (ele.children.length > 0) continue;
|
if (ele.children.length > 0) continue
|
||||||
|
|
||||||
// 从 data-params 属性中解析参数
|
// Parse parameters from data-params attribute
|
||||||
var params = {};
|
let params = {}
|
||||||
var dp = ele.getAttribute("data-params");
|
const dp = ele.getAttribute("data-params")
|
||||||
if (dp) {
|
if (dp) {
|
||||||
try {
|
try {
|
||||||
params = JSON.parse(dp);
|
params = JSON.parse(dp)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("解析 data-params 失败:", e);
|
console.error("Failed to parse data-params:", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将解析出的参数与 responsive 参数合并,
|
// Merge parsed parameters with the responsive option
|
||||||
// 保证 params 中的内容在 responsive 之前
|
// Ensures params content appears before responsive
|
||||||
var options = {};
|
const options = { ...params, responsive: "resize" }
|
||||||
for (var key in params) {
|
|
||||||
if (params.hasOwnProperty(key)) {
|
|
||||||
options[key] = params[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
options.responsive = "resize";
|
|
||||||
|
|
||||||
// 使用 ABCJS.renderAbc 渲染乐谱
|
// Render the music score using ABCJS.renderAbc
|
||||||
ABCJS.renderAbc(ele, ele.innerHTML, options);
|
ABCJS.renderAbc(ele, ele.innerHTML, options)
|
||||||
}
|
}
|
||||||
}, 100);
|
}, 100)
|
||||||
};
|
}
|
||||||
|
|
||||||
if (typeof ABCJS === "object") {
|
if (typeof ABCJS === "object") {
|
||||||
abcjsFn();
|
abcjsFn()
|
||||||
} else {
|
} else {
|
||||||
btf.getScript("!{url_for(theme.asset.abcjs_basic_js)}").then(abcjsFn);
|
btf.getScript("!{url_for(theme.asset.abcjs_basic_js)}").then(abcjsFn)
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
if (window.pjax) {
|
if (window.pjax) {
|
||||||
abcjsInit();
|
abcjsInit()
|
||||||
} else {
|
} else {
|
||||||
window.addEventListener("load", abcjsInit);
|
window.addEventListener("load", abcjsInit)
|
||||||
}
|
}
|
||||||
btf.addGlobalFn("encrypt", abcjsInit, "abcjs");
|
|
||||||
})();
|
btf.addGlobalFn("encrypt", abcjsInit, "abcjs")
|
||||||
|
})()
|
||||||
|
|||||||
@@ -6,12 +6,12 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const score = (args, content) => {
|
const score = (args, content) => {
|
||||||
// 转义 HTML 标签和部分特殊字符,包括花括号
|
// Escape HTML tags and some special characters, including curly braces
|
||||||
const escapeHtmlTags = s => {
|
const escapeHtmlTags = s => {
|
||||||
const lookup = {
|
const lookup = {
|
||||||
'&': '&',
|
'&': '&',
|
||||||
'"': '"',
|
'"': '"',
|
||||||
'\'': ''',
|
"'": ''',
|
||||||
'<': '<',
|
'<': '<',
|
||||||
'>': '>',
|
'>': '>',
|
||||||
'{': '{',
|
'{': '{',
|
||||||
@@ -21,15 +21,15 @@ const score = (args, content) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const trimmed = content.trim()
|
const trimmed = content.trim()
|
||||||
// 使用六个横线作为分隔符拆分内容
|
// Split content using six dashes as a delimiter
|
||||||
const parts = trimmed.split('------')
|
const parts = trimmed.split('------')
|
||||||
|
|
||||||
if (parts.length < 2) {
|
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>`
|
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 paramPart = parts[0].trim()
|
||||||
const scorePart = parts.slice(1).join('------').trim()
|
const scorePart = parts.slice(1).join('------').trim()
|
||||||
|
|
||||||
@@ -37,14 +37,14 @@ const score = (args, content) => {
|
|||||||
try {
|
try {
|
||||||
paramsObj = JSON.parse(paramPart)
|
paramsObj = JSON.parse(paramPart)
|
||||||
} catch (e) {
|
} 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))}">
|
return `<div class="abc-music-sheet" data-params="${escapeHtmlTags(JSON.stringify(paramsObj))}">
|
||||||
${escapeHtmlTags(scorePart)}
|
${escapeHtmlTags(scorePart)}
|
||||||
</div>`
|
</div>`
|
||||||
}
|
}
|
||||||
|
|
||||||
hexo.extend.tag.register('score', score, { ends: true })
|
hexo.extend.tag.register("score", score, { ends: true })
|
||||||
|
|
||||||
Reference in New Issue
Block a user