mirror of
https://github.com/JimLiu/baoyu-skills.git
synced 2026-08-08 09:53:02 +08:00
feat(baoyu-post-to-wechat,baoyu-post-to-weibo,baoyu-post-to-x): cascade Mermaid PNG preprocessing into publishing pipelines
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
extractSummaryFromBody,
|
||||
extractTitleFromMarkdown,
|
||||
parseFrontmatter,
|
||||
preprocessMermaidInMarkdown,
|
||||
renderMarkdownDocument,
|
||||
replaceMarkdownImagesWithPlaceholders,
|
||||
resolveColorToken,
|
||||
@@ -15,6 +16,7 @@ import {
|
||||
serializeFrontmatter,
|
||||
stripWrappingQuotes,
|
||||
} from "baoyu-md";
|
||||
import { closeRenderer, renderMermaidToPng } from "baoyu-chrome-cdp/mermaid";
|
||||
|
||||
interface ImageInfo {
|
||||
placeholder: string;
|
||||
@@ -55,8 +57,27 @@ export async function convertMarkdown(
|
||||
summary = extractSummaryFromBody(body, 120);
|
||||
}
|
||||
|
||||
const { markdown: mermaidProcessedBody, images: mermaidImages } =
|
||||
await preprocessMermaidInMarkdown(body, {
|
||||
baseDir,
|
||||
renderFn: renderMermaidToPng,
|
||||
onError: (error, block) => {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.error(
|
||||
`[md-to-wechat] mermaid render failed (${block.code.slice(0, 40).replace(/\s+/g, " ")}…): ${message}`,
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
if (mermaidImages.length > 0) {
|
||||
const fresh = mermaidImages.filter((image) => !image.cached).length;
|
||||
console.error(
|
||||
`[md-to-wechat] mermaid: ${mermaidImages.length} block(s), ${fresh} rendered, ${mermaidImages.length - fresh} cached`,
|
||||
);
|
||||
}
|
||||
|
||||
const { images, markdown: rewrittenBody } = replaceMarkdownImagesWithPlaceholders(
|
||||
body,
|
||||
mermaidProcessedBody,
|
||||
"WECHATIMGPH_",
|
||||
);
|
||||
const rewrittenMarkdown = `${serializeFrontmatter(frontmatter)}${rewrittenBody}`;
|
||||
@@ -166,7 +187,11 @@ async function main(): Promise<void> {
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
}
|
||||
|
||||
await main().catch((error) => {
|
||||
try {
|
||||
await main();
|
||||
} catch (error) {
|
||||
console.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
|
||||
process.exit(1);
|
||||
});
|
||||
process.exitCode = 1;
|
||||
} finally {
|
||||
await closeRenderer();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user