Files
clash-verge-rev/src/utils/yaml.ts
T
StarsSailandwonfen 117bac8110 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>
2026-08-09 06:02:47 +00:00

25 lines
504 B
TypeScript

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