From ca82cebef51a247b92502a3ffbb0a4bd9a8492ac Mon Sep 17 00:00:00 2001 From: xiguapi <458813868@qq.com> Date: Tue, 28 Apr 2026 21:14:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=BE=AE=E4=BF=A1=E4=B8=8B?= =?UTF-8?q?=E8=BD=BDMD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .claude/settings.local.json | 8 +++ .sassrc.json | 3 + component/ui/customDomSelector.tsx | 1 + contents/custom.tsx | 8 +-- contents/weixin.tsx | 94 +++++++++++++----------------- package.json | 4 +- 6 files changed, 55 insertions(+), 63 deletions(-) create mode 100644 .claude/settings.local.json create mode 100644 .sassrc.json diff --git a/.claude/settings.local.json b/.claude/settings.local.json new file mode 100644 index 0000000..37413c7 --- /dev/null +++ b/.claude/settings.local.json @@ -0,0 +1,8 @@ +{ + "permissions": { + "allow": [ + "Bash(npx update-browserslist-db@latest:*)", + "Bash(pnpm up:*)" + ] + } +} diff --git a/.sassrc.json b/.sassrc.json new file mode 100644 index 0000000..94e0955 --- /dev/null +++ b/.sassrc.json @@ -0,0 +1,3 @@ +{ + "silenceDeprecations": ["legacy-js-api"] +} diff --git a/component/ui/customDomSelector.tsx b/component/ui/customDomSelector.tsx index a8465b1..2f52334 100644 --- a/component/ui/customDomSelector.tsx +++ b/component/ui/customDomSelector.tsx @@ -143,6 +143,7 @@ export default function CustomDomSelector() { } const handleConfirm = () => { + handleOkModal() } const handleCancel = () => { diff --git a/contents/custom.tsx b/contents/custom.tsx index 00eae32..a91ad5b 100644 --- a/contents/custom.tsx +++ b/contents/custom.tsx @@ -1,9 +1,5 @@ -import type { - PlasmoCSConfig, - PlasmoGetShadowHostId, - PlasmoGetStyle -} from "plasmo" -import { useEffect, useRef, useState } from "react" +import type { PlasmoGetShadowHostId, } from "plasmo" +import { useEffect } from "react" import { sendToBackground } from "@plasmohq/messaging" import { useMessage } from "@plasmohq/messaging/hook" diff --git a/contents/weixin.tsx b/contents/weixin.tsx index ccfe4f6..eae4c19 100644 --- a/contents/weixin.tsx +++ b/contents/weixin.tsx @@ -271,59 +271,52 @@ const PlasmoOverlay: FC = ({ anchor }) => { async function downloadMarkdown() { const html = document.querySelector("#img-content") const markdown = turndownService.turndown(html) - await saveMarkdownWithLocalImages(markdown, articleTitle) + await saveMarkdown(markdown, articleTitle) } async function downloadMarkdownWithImages( onProgress?: (current: number, total: number) => void ) { - const article = document.querySelector("#js_content") const html = document.querySelector("#img-content") - if (!article || !html) return + if (!html) return - const images = Array.from(article.getElementsByTagName("img")) - const imageUrls = images - .map((img) => img.dataset.src || img.src) - .filter((url) => url) - .map((url) => - url.replace( - "//res.wx.qq.com/mmbizwap", - "https://res.wx.qq.com/mmbizwap" - ) - ) + // 先生成 markdown,再从中提取实际图片 URL,确保 URL 完全一致 + let markdown = turndownService.turndown(html) + const mdImageRegex = /!\[([^\]]*)\]\(([^)]+)\)/g + const imageEntries: { alt: string; url: string }[] = [] + let match + while ((match = mdImageRegex.exec(markdown)) !== null) { + const url = match[2] + if (url && !url.startsWith("./images/")) { + imageEntries.push({ alt: match[1], url }) + } + } const zip = new JSZip() const title = articleTitle || document.title.trim() - const total = imageUrls.length + 1 + const total = imageEntries.length + 1 - // 下载所有图片 - const imageMap = new Map() - for (let i = 0; i < imageUrls.length; i++) { + const getExt = (url: string) => { + const fmt = url.match(/wx_fmt=(\w+)/)?.[1] || "" + if (fmt === "gif") return ".gif" + if (fmt === "png" || fmt === "webp") return ".png" + if (fmt === "bmp") return ".bmp" + if (url.includes("mmbiz_gif")) return ".gif" + if (url.includes("mmbiz_png") || url.includes("mmbiz_webp")) return ".png" + return ".jpg" + } + + // 下载图片并替换 markdown 中的 URL + for (let i = 0; i < imageEntries.length; i++) { + const { url } = imageEntries[i] try { - const response = await fetch(imageUrls[i]) + const response = await fetch(url) const blob = await response.blob() + const ext = getExt(url) + const localPath = `./images/${i}${ext}` - let ext = ".jpg" - if ( - imageUrls[i].includes("wx_fmt=gif") || - imageUrls[i].includes("mmbiz_gif") - ) { - ext = ".gif" - } else if ( - imageUrls[i].includes("wx_fmt=png") || - imageUrls[i].includes("mmbiz_png") - ) { - ext = ".png" - } else if ( - imageUrls[i].includes("wx_fmt=bmp") || - imageUrls[i].includes("mmbiz_bmp") - ) { - ext = ".bmp" - } - - const filename = `images/${i}${ext}` - zip.file(filename, blob) - imageMap.set(imageUrls[i], filename) + zip.file(`images/${i}${ext}`, blob) + markdown = markdown.replace(url, localPath) if (onProgress) { onProgress(i + 1, total) @@ -333,16 +326,6 @@ const PlasmoOverlay: FC = ({ anchor }) => { } } - // 生成 markdown 并替换图片路径 - let markdown = turndownService.turndown(html) - imageMap.forEach((localPath, originalUrl) => { - const escapedUrl = originalUrl.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") - markdown = markdown.replace( - new RegExp(`\\(${escapedUrl}\\)`, "g"), - `(${localPath})` - ) - }) - // 添加 markdown 文件到 zip zip.file(`${title}.md`, markdown) @@ -359,9 +342,10 @@ const PlasmoOverlay: FC = ({ anchor }) => { saveHtml(dom, articleTitle) } - function parseMarkdown() { - const dom = document.querySelector("#img-content") - setParseContent(dom) + async function parseMarkdown() { + const html = document.querySelector("#img-content") + const markdown = turndownService.turndown(html) + await saveMarkdownWithLocalImages(markdown, articleTitle) } function onClose() { @@ -400,15 +384,15 @@ const PlasmoOverlay: FC = ({ anchor }) => { + - diff --git a/package.json b/package.json index 38b9c13..ef2e50f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "code-box", "displayName": "__MSG_extensionName__", - "version": "2.0.0", + "version": "2.0.1", "description": "__MSG_extensionDescription__", "author": "027xiguapi. <458813868@qq.com>", "scripts": { @@ -48,7 +48,7 @@ "postcss": "^8.2.1", "postcss-modules": "^4.3.0", "prettier": "3.2.4", - "sass": "^1.77.8", + "sass": "^1.99.0", "sharp": "^0.33.5", "typescript": "5.3.3" },