| 1234567891011121314151617181920 |
- // 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
- }
|