mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-29 00:40:18 +08:00
* 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>
25 lines
504 B
TypeScript
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
|
|
}
|
|
}
|