iter(g246): ua info 增加 --md,输出可直接粘贴的 Markdown 片段

This commit is contained in:
HIsheR
2026-10-26 22:00:07 +08:00
parent ab2bd00614
commit c5238c6f3a
6 changed files with 39 additions and 16 deletions
+25 -2
View File
@@ -173,6 +173,25 @@ function formatComponent(pkg, comp) {
return lines.join('\n');
}
/*** 作用:把组件渲染成可直接贴进 issue / 文档 / 回复的 Markdown 片段(标题 + 参数表 + 用法 + 示例)
* 与文本版共用同一份清单数据:Agent 拿到片段就能贴,不必自己从参数表里再抄一遍格式。 ***/
function formatComponentMd(pkg, comp) {
const cell = (v) => String(v ?? '').replace(/\|/g, '\\|'); // 联合类型里的竖线会截断表格,必须转义
const lines = [`### ${comp.id} · ${comp.title ?? comp.id}`, '', `\`${pkg.name}\`${comp.demo ? ` · 演示锚点 \`${comp.demo}\`` : ''}`, ''];
if ((comp.props ?? []).length) {
lines.push('| 参数 | 类型 | 必填 | 说明 |', '| --- | --- | --- | --- |');
for (const x of comp.props) lines.push(`| \`${cell(x.n)}\` | \`${cell(x.t)}\` | ${x.r ? '是' : '否'} | ${cell(x.d)} |`);
lines.push('');
}
if ((comp.steps ?? []).length) {
lines.push('**用法**', '');
comp.steps.forEach((t, i) => lines.push(`${i + 1}. ${t}`));
lines.push('');
}
if (comp.example) lines.push('```ts', comp.example, '```', '');
return lines.join('\n');
}
/*** 反查某组件出现在哪些配方里:查参数时顺带给出「它通常和谁一起用」的组合线索 ***/
function recipesFor(compId) {
const re = new RegExp(`\\b${compId}\\b`);
@@ -313,8 +332,12 @@ function cmdInfo(name, args = []) {
}
if (asJson) return console.log(JSON.stringify({ package: hit.pkg.name, ...hit.comp, recipes: recipesFor(hit.comp.id) }, null, 2));
const related = recipesFor(hit.comp.id);
const tail = related.length ? `\n\n出现在配方:${related.map((r) => `${r.id}(${r.title})`).join('、')}(ua mcp 的 get_recipe 可取骨架)` : '';
console.log(formatComponent(hit.pkg, hit.comp) + tail);
const recipeTail = related.length ? `\n\n出现在配方:${related.map((r) => `${r.id}(${r.title})`).join('、')}(ua mcp 的 get_recipe 可取骨架)` : '';
if (args.includes('--md')) {
// Markdown 片段:贴进文档 / issue / 回复时不用手抄参数表;配方清单压成一行尾注
return console.log(formatComponentMd(hit.pkg, hit.comp) + recipeTail);
}
console.log(formatComponent(hit.pkg, hit.comp) + recipeTail);
}
// ---- 静态文档站生成 ----