obsidian-douban/src/org/wanxp/utils/YamlUtil.ts

32 lines
839 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export default class YamlUtil {
public static hasSpecialChar(str: string): boolean {
return SPECIAL_CHAR_REG.test(str);
}
public static handleSpecialChar(text: string): string {
// return this.hasSpecialChar(text) ? text.replace(SPECIAL_CHAR_REG, (match, p1) => {
// return SPECIAL_CHAR_REG_REPLACE.get(p1) || p1;
// }) : text;
//temp solution
return '"' + text + '"';
}
public static handleText(text: string) {
return YamlUtil.hasSpecialChar(text) ? YamlUtil.handleSpecialChar(text.replaceAll('"', '\\"'))
.replaceAll('\n', '。')
.replaceAll('。。', '。') : text;
}
}
export const SPECIAL_CHAR_REG = /[{}\[\]&*#?|\-<>=!%@:"`,\n]/;
export const TITLE_ALIASES_SPECIAL_CHAR_REG_G = /[{}\[\]&*#?|\-<>=!%@:"`, \n]/g;
const SPECIAL_CHAR_REG_REPLACE: Map<string, string> = new Map([
['{', '\\{'],
]);