























@@ -26,11 +26,17 @@ export function setMdOcPath(ast: MdAst, path: OcPath, newValue: string): MdEditR
2626guardSentinel(newValue, formatOcPath(path));
2727if (path.section === "[frontmatter]") {
2828const key = path.item ?? path.field;
29-if (key === undefined) {return { ok: false, reason: "unresolved" };}
29+if (key === undefined) {
30+return { ok: false, reason: "unresolved" };
31+}
3032const idx = ast.frontmatter.findIndex((e) => e.key === key);
31-if (idx === -1) {return { ok: false, reason: "unresolved" };}
33+if (idx === -1) {
34+return { ok: false, reason: "unresolved" };
35+}
3236const existing = ast.frontmatter[idx];
33-if (existing === undefined) {return { ok: false, reason: "unresolved" };}
37+if (existing === undefined) {
38+return { ok: false, reason: "unresolved" };
39+}
3440const newEntry: FrontmatterEntry = { ...existing, value: newValue };
3541const newFm = ast.frontmatter.slice();
3642newFm[idx] = newEntry;
@@ -43,16 +49,26 @@ export function setMdOcPath(ast: MdAst, path: OcPath, newValue: string): MdEditR
43494450const sectionSlug = path.section.toLowerCase();
4551const blockIdx = ast.blocks.findIndex((b) => b.slug === sectionSlug);
46-if (blockIdx === -1) {return { ok: false, reason: "unresolved" };}
52+if (blockIdx === -1) {
53+return { ok: false, reason: "unresolved" };
54+}
4755const block = ast.blocks[blockIdx];
48-if (block === undefined) {return { ok: false, reason: "unresolved" };}
56+if (block === undefined) {
57+return { ok: false, reason: "unresolved" };
58+}
49595060const itemSlug = path.item.toLowerCase();
5161const itemIdx = block.items.findIndex((i) => i.slug === itemSlug);
52-if (itemIdx === -1) {return { ok: false, reason: "unresolved" };}
62+if (itemIdx === -1) {
63+return { ok: false, reason: "unresolved" };
64+}
5365const item = block.items[itemIdx];
54-if (item === undefined) {return { ok: false, reason: "unresolved" };}
55-if (item.kv === undefined) {return { ok: false, reason: "no-item-kv" };}
66+if (item === undefined) {
67+return { ok: false, reason: "unresolved" };
68+}
69+if (item.kv === undefined) {
70+return { ok: false, reason: "no-item-kv" };
71+}
5672if (item.kv.key.toLowerCase() !== path.field.toLowerCase()) {
5773return { ok: false, reason: "unresolved" };
5874}
@@ -78,9 +94,15 @@ function rebuildBlockBody(block: AstBlock, newItems: readonly AstItem[]): string
7894for (let i = 0; i < newItems.length; i++) {
7995const newItem = newItems[i];
8096const oldItem = block.items[i];
81-if (newItem === undefined || oldItem === undefined) {continue;}
82-if (newItem.kv === undefined || oldItem.kv === undefined) {continue;}
83-if (newItem.kv.value === oldItem.kv.value) {continue;}
97+if (newItem === undefined || oldItem === undefined) {
98+continue;
99+}
100+if (newItem.kv === undefined || oldItem.kv === undefined) {
101+continue;
102+}
103+if (newItem.kv.value === oldItem.kv.value) {
104+continue;
105+}
84106const re = new RegExp(`^(\\s*-\\s*${escapeRegex(oldItem.kv.key)}\\s*:\\s*).*$`, "m");
85107body = body.replace(re, `$1${newItem.kv.value}`);
86108}
@@ -101,19 +123,29 @@ function finalize(ast: MdAst): MdEditResult {
101123parts.push("---");
102124}
103125if (ast.preamble.length > 0) {
104-if (parts.length > 0) {parts.push("");}
126+if (parts.length > 0) {
127+parts.push("");
128+}
105129parts.push(ast.preamble);
106130}
107131for (const block of ast.blocks) {
108-if (parts.length > 0) {parts.push("");}
132+if (parts.length > 0) {
133+parts.push("");
134+}
109135parts.push(`## ${block.heading}`);
110-if (block.bodyText.length > 0) {parts.push(block.bodyText);}
136+if (block.bodyText.length > 0) {
137+parts.push(block.bodyText);
138+}
111139}
112140return { ok: true, ast: { ...ast, raw: parts.join("\n") } };
113141}
114142115143function formatFrontmatterValue(value: string): string {
116-if (value.length === 0) {return '""';}
117-if (/[:#&*?|<>=!%@`,[\]{}\r\n]/.test(value)) {return JSON.stringify(value);}
144+if (value.length === 0) {
145+return '""';
146+}
147+if (/[:#&*?|<>=!%@`,[\]{}\r\n]/.test(value)) {
148+return JSON.stringify(value);
149+}
118150return value;
119151}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。