ai-tube / src /app /api /parsers /parseRawStringToYAML.ts
jbilcke-hf's picture
jbilcke-hf HF staff
upgraded to @aitube/client 0.0.12
f24ad59
raw
history blame
No virus
470 Bytes
import YAML from "yaml"
export function parseRawStringToYAML<T>(input: any, defaultValue: T) {
try {
let rawString = `${input || ""}`.trim()
rawString = rawString
.replaceAll("```yaml\n", "")
.replaceAll("```yaml", "")
// we remove everything after the last ``` (or ``)
rawString = rawString.split(/```?/)[0].trim()
const something: any = YAML.parse(rawString)
return something as T
} catch (err) {
return defaultValue
}
}