mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-19 02:12:10 +08:00
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:
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user