feat(watchlist): yaml helpers + schema types
This commit is contained in:
parent
e7714736ce
commit
cade7104db
3 changed files with 118 additions and 0 deletions
25
packages/obsidian/src/watchlist/yaml.ts
Normal file
25
packages/obsidian/src/watchlist/yaml.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
const NEEDS_QUOTE = /[:,#\[\]{}"&*!|>%@`]/;
|
||||
const KEYWORDS = new Set(['null', 'true', 'false', 'yes', 'no']);
|
||||
|
||||
export function yamlScalar(v: unknown): string {
|
||||
if (v === null || v === undefined || v === '') return '';
|
||||
const s = String(v);
|
||||
if (NEEDS_QUOTE.test(s) || s.trim() !== s || KEYWORDS.has(s.toLowerCase())) {
|
||||
return '"' + s.replace(/"/g, '\\"') + '"';
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
export function yamlList(items: string[]): string {
|
||||
const parts: string[] = [];
|
||||
for (const raw of items) {
|
||||
const g = String(raw).trim();
|
||||
if (!g) continue;
|
||||
parts.push(/^[A-Za-z0-9 ]+$/.test(g) ? g : '"' + g.replace(/"/g, '\\"') + '"');
|
||||
}
|
||||
return '[' + parts.join(', ') + ']';
|
||||
}
|
||||
|
||||
export function quotedOrNull(v: string | null | undefined): string {
|
||||
return v ? '"' + v + '"' : 'null';
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue