| 1 | import * as YAML from "yaml"; |
| 2 | |
| 3 | import { getLastNPathParts } from "../../util/uri"; |
| 4 | |
| 5 | export function extractName(preamble: { name?: string }, path: string): string { |
| 6 | return preamble.name ?? getLastNPathParts(path, 1).split(".prompt")[0]; |
| 7 | } |
| 8 | |
| 9 | export function getPreambleAndBody(content: string): [string, string] { |
| 10 | let [preamble, body] = content.split("\n---\n"); |
| 11 | if (body === undefined) { |
| 12 | // This means there is no preamble |
| 13 | body = preamble; |
| 14 | preamble = ""; |
| 15 | } |
| 16 | return [preamble, body]; |
| 17 | } |
| 18 | |
| 19 | export function parsePreamble( |
| 20 | path: string, |
| 21 | content: string, |
| 22 | ): { [key: string]: any; name: string; description: string } { |
| 23 | const [preambleRaw, _] = getPreambleAndBody(content); |
| 24 | |
| 25 | const preamble = YAML.parse(preambleRaw) ?? {}; |
| 26 | const name = extractName(preamble, path); |
| 27 | const description = preamble.description ?? name; |
| 28 | |
| 29 | return { |
| 30 | ...preamble, |
| 31 | name, |
| 32 | description, |
| 33 | }; |
| 34 | } |