feat: add parseYamlSafe utility (#7706)

* feat: add parseYamlSafe utility function and refactor YAML parsing in components

* fix

* fix

* fix: preserve malformed YAML in profile editors

---------

Co-authored-by: wonfen <eiwonfen@gmail.com>
This commit is contained in:
StarsSail
2026-08-09 06:02:47 +00:00
committed by GitHub
co-authored by wonfen
parent ed4e55a947
commit 117bac8110
4 changed files with 131 additions and 52 deletions
+24
View File
@@ -0,0 +1,24 @@
import { load } from 'js-yaml'
import { debugLog } from './debug'
/**
* 空文档返回 null,解析失败返回 undefined,且不抛出错误。
*/
export function parseYamlSafe(input: string) {
if (
input
.split(/\r?\n/u)
.every((line) => line.trim() === '' || line.trimStart().startsWith('#'))
) {
return null
}
try {
const parsedYaml = load(input)
return parsedYaml ?? null
} catch (e) {
debugLog('parseYamlSafe failed', e)
return undefined
}
}