| 123456789101112131415161718192021222324252627 |
- // SKILL.md 读取工具:Claude 插件、Codex 官方技能、第三方技能共用
- /** 读取 SKILL.md 头部的 YAML 字段(只处理单层键值,支持 > / | 多行写法) */
- export function frontmatter(md: string): Record<string, string> {
- const m = /^---\r?\n([\s\S]*?)\r?\n---/.exec(md)
- const out: Record<string, string> = {}
- const lines = m?.[1]?.split(/\r?\n/) ?? []
- for (let i = 0; i < lines.length; i++) {
- const kv = /^([A-Za-z_-]+):\s*(.*)$/.exec(lines[i]!)
- if (!kv) continue
- let value = kv[2]!.trim()
- if (/^[>|][+-]?$/.test(value)) {
- const block: string[] = []
- while (i + 1 < lines.length && (/^\s+\S/.test(lines[i + 1]!) || lines[i + 1] === '')) block.push(lines[++i]!.trim())
- value = block.join(value.startsWith('>') ? ' ' : '\n').trim()
- }
- out[kv[1]!] = value.replace(/^["']|["']$/g, '').trim()
- }
- return out
- }
- /** SKILL.md 中声明的版本号:frontmatter 顶层的 version,或 metadata 下的 version */
- export function declaredVersion(md: string): string | null {
- const fm = /^---\r?\n([\s\S]*?)\r?\n---/.exec(md)?.[1] ?? ''
- const v = /^version:\s*(.+)$/m.exec(fm) ?? /^metadata:[^\n]*\n(?:[ \t]+[^\n]*\n)*?[ \t]+version:\s*(.+)$/m.exec(fm)
- return v ? v[1]!.trim().replace(/^["']|["']$/g, '').slice(0, 40) || null : null
- }
|