From d3140eb4fab2c378142f55d0cbef3fc3a48b3fdf Mon Sep 17 00:00:00 2001 From: obgnail Date: Fri, 24 Jan 2025 22:25:27 +0800 Subject: [PATCH] update callouts export --- plugin/custom/plugins/callouts.js | 55 +++++++++++++++---------------- plugin/global/core/utils/index.js | 9 +++++ 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/plugin/custom/plugins/callouts.js b/plugin/custom/plugins/callouts.js index 775894b0..873d8ed3 100644 --- a/plugin/custom/plugins/callouts.js +++ b/plugin/custom/plugins/callouts.js @@ -55,42 +55,39 @@ class calloutsPlugin extends BaseCustomPlugin { // icon需要用到font,但是导出时又没有font,因此只能移除 beforeExport = (...args) => { if (this.check(args)) { - return this.utils.styleTemplater.getStyleContent(this.fixedName).replace(/--callout-icon: ".*?";/g, ""); + const css = this.utils.styleTemplater.getStyleContent(this.fixedName) + return css.replace(/--callout-icon: ".*?";/g, "") } } afterExport = (html, ...args) => { - if (!this.check(args)) return; - - const regex = new RegExp("
", "g"); - const count = (html.match(regex) || []).length; - const quotes = Array.from(this.utils.entities.querySelectorAllInWrite("blockquote")); - if (count !== quotes.length) return html; - - let idx = -1; - html = html.replace(regex, origin => { - idx++; - let result = origin; - - const quote = quotes[idx]; - if (quote && quote.classList.length) { - const type = quote.getAttribute("callout-type"); - result = `
`; + if (!this.check(args)) return + + const quotesInPage = [...this.utils.entities.querySelectorAllInWrite("blockquote")] + if (quotesInPage.length === 0) return + + const doc = new DOMParser().parseFromString(html, "text/html") + const quotesInHTML = [...doc.querySelectorAll("blockquote")] + if (quotesInHTML.length !== quotesInPage.length) return + + const zipArray = this.utils.zip(quotesInPage, quotesInHTML) + for (const [quoteInPage, quoteInHTML] of zipArray) { + if (quoteInPage.classList.length) { + quoteInHTML.className = "plugin-callout" + + const calloutType = quoteInPage.getAttribute("callout-type") + quoteInHTML.setAttribute("callout-type", calloutType) + + const span = quoteInHTML.querySelector(":scope > p:first-child > span:first-child") + if (span) { + span.setAttribute("data-type", calloutType.toUpperCase()) + } } - return result; - }); - - // 补上属性 data-type - const spanRegex = /()\[!(\w+)\](<\/span>)/g; - html = html.replace(spanRegex, (match, openTag, type, closeTag) => { - const typeValue = type.charAt(0).toUpperCase() + type.slice(1).toLowerCase(); - return `[!${type}]`; - }); - - return html; + } + return `\n${doc.documentElement.outerHTML}` } } module.exports = { plugin: calloutsPlugin, -}; +} diff --git a/plugin/global/core/utils/index.js b/plugin/global/core/utils/index.js index 5f5c264f..2eb75f67 100644 --- a/plugin/global/core/utils/index.js +++ b/plugin/global/core/utils/index.js @@ -236,6 +236,15 @@ class utils { return result; } + static zip = (...arrays) => { + const minLength = Math.min(...arrays.map(arr => arr.length)) + let zipArray = [] + for (let i = 0; i < minLength; i++) { + zipArray.push(arrays.map(arr => arr[i])) + } + return zipArray + } + /** @description try not to use it */ static sleep = ms => new Promise(resolve => setTimeout(resolve, ms))