Skip to content

Commit f59a7d2

Browse files
committed
fix(yapi-docs-sync): log mermaid rendering
1 parent 191ce10 commit f59a7d2

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

packages/yapi-mcp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@leeguoo/yapi-mcp",
3-
"version": "0.3.6",
3+
"version": "0.3.7",
44
"description": "YApi Auto MCP Server - Model Context Protocol server for YApi integration, enables AI tools like Cursor to interact with YApi API documentation",
55
"main": "dist/index.js",
66
"bin": {

packages/yapi-mcp/src/docs/markdown.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import MarkdownIt from "markdown-it";
66

77
export type MarkdownRenderOptions = {
88
noMermaid?: boolean;
9+
logMermaid?: boolean;
10+
logger?: (message: string) => void;
911
};
1012

1113
let cachedPandocAvailable: boolean | null = null;
@@ -67,6 +69,11 @@ function renderMermaidWithMmdc(source: string): string {
6769
}
6870
}
6971

72+
type MermaidRenderResult = {
73+
svg: string;
74+
normalized: boolean;
75+
};
76+
7077
function normalizeMermaidLabels(source: string): string {
7178
return source.replace(/(?<!\[)\[([^\]\n]+)\](?!\])/g, (match, label: string) => {
7279
const trimmed = String(label || "").trim();
@@ -83,15 +90,15 @@ function normalizeMermaidLabels(source: string): string {
8390
});
8491
}
8592

86-
function renderMermaidToSvg(source: string): string {
93+
function renderMermaidToSvg(source: string): MermaidRenderResult {
8794
ensureMmdc();
88-
const attempts = [source];
8995
const normalized = normalizeMermaidLabels(source);
90-
if (normalized !== source) attempts.push(normalized);
96+
const attempts: Array<{ text: string; normalized: boolean }> = [{ text: source, normalized: false }];
97+
if (normalized !== source) attempts.push({ text: normalized, normalized: true });
9198
let lastError: unknown;
9299
for (const attempt of attempts) {
93100
try {
94-
return renderMermaidWithMmdc(attempt);
101+
return { svg: renderMermaidWithMmdc(attempt.text), normalized: attempt.normalized };
95102
} catch (error) {
96103
lastError = error;
97104
}
@@ -104,11 +111,28 @@ export function preprocessMarkdown(markdown: string, options: MarkdownRenderOpti
104111
if (options.noMermaid) return markdown;
105112
if (!isMmdcAvailable()) return markdown;
106113
const pattern = /```mermaid\s*\r?\n([\s\S]*?)\r?\n```/g;
114+
const shouldLog = Boolean(options.logMermaid);
115+
const logger = options.logger || console.log;
116+
let index = 0;
107117
return markdown.replace(pattern, (match, content: string) => {
118+
index += 1;
119+
if (shouldLog) {
120+
logger(`已识别 Mermaid 块 #${index},开始渲染...`);
121+
}
108122
try {
109-
const svg = renderMermaidToSvg(String(content || "").trim());
110-
return `<div class="mermaid-diagram">\n${svg}\n</div>`;
123+
const result = renderMermaidToSvg(String(content || "").trim());
124+
if (shouldLog) {
125+
if (result.normalized) {
126+
logger(`Mermaid 块 #${index} 渲染成功(已自动修正 label)。`);
127+
} else {
128+
logger(`Mermaid 块 #${index} 渲染成功。`);
129+
}
130+
}
131+
return `<div class="mermaid-diagram">\n${result.svg}\n</div>`;
111132
} catch {
133+
if (shouldLog) {
134+
logger(`Mermaid 块 #${index} 渲染失败,保持代码块原样。`);
135+
}
112136
return match;
113137
}
114138
});

packages/yapi-mcp/src/yapi-cli.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,12 @@ async function syncDocsDir(
10591059
}
10601060

10611061
const markdown = fs.readFileSync(mdPath, "utf8");
1062-
const html = renderMarkdownToHtml(markdown, { noMermaid: options.noMermaid });
1062+
const logPrefix = `[docs-sync:${relName}]`;
1063+
const html = renderMarkdownToHtml(markdown, {
1064+
noMermaid: options.noMermaid,
1065+
logMermaid: true,
1066+
logger: (message) => console.log(`${logPrefix} ${message}`),
1067+
});
10631068
if (!options.dryRun && docId) {
10641069
await updateInterface(docId, markdown, html, request);
10651070
}

0 commit comments

Comments
 (0)