Skip to content

A self-organizing Obsidian vault that gives AI coding agents persistent memory.

fix: a CRLF note lost every alias, and three shapes lost some (#214)

HoangThai18bece7bd1d32a
diff --git a/.claude/scripts/lib/wikilinks.ts b/.claude/scripts/lib/wikilinks.ts
index 4294c5b..19e22b6 100644
--- a/.claude/scripts/lib/wikilinks.ts
+++ b/.claude/scripts/lib/wikilinks.ts
@@ -103,15 +103,73 @@ export function extractWikilinkTargets(content: string): string[] {
return targets;
}
+/**
+ * Split a YAML inline list's contents on its SEPARATORS.
+ *
+ * A blind `split(",")` cannot be used here, because a comma inside a quoted
+ * scalar is content: `["Smith, John", Nickname]` is two aliases, and splitting
+ * it blindly yields three fragments, each carrying a stray quote character —
+ * names that match nothing and that nobody could type to work around.
+ *
+ * Quote state is all this needs to track. A backslash escape is consumed whole
+ * inside a double-quoted scalar, and the `''` of a single-quoted scalar is
+ * stepped over rather than read as a close, so neither hides a separator or
+ * invents one.
+ */
+function splitInlineList(inner: string): string[] {
+ const out: string[] = [];
+ let cur = "";
+ let quote: '"' | "'" | null = null;
+ for (let i = 0; i < inner.length; i++) {
+ const ch = inner[i]!;
+ if (quote === null) {
+ if (ch === '"' || ch === "'") quote = ch;
+ else if (ch === ",") {
+ out.push(cur);
+ cur = "";
+ continue;
+ }
+ } else if (ch === "\\" && quote === '"' && i + 1 < inner.length) {
+ cur += ch + inner[++i]!;
+ continue;
+ } else if (ch === quote) {
+ if (quote === "'" && inner[i + 1] === "'") {
+ cur += "''";
+ i++;
+ continue;
+ }
+ quote = null;
+ }
+ cur += ch;
+ }
+ out.push(cur);
+ return out;
+}
+
/**
* Minimal frontmatter `aliases:` reader — YAML block list and inline
* `[a, b]` forms, quoted or bare. Returns [] when absent/malformed.
+ *
+ * "Minimal" is bounded by ownership. `parseFrontmatter` in memory-recall may
+ * accept a small subset because this repo writes every file it reads; these
+ * aliases come from USER notes, written by Obsidian, by hand, by templates and
+ * by other agents, so the shapes below are ordinary input rather than
+ * malformed input — and against a zero gate, a shape this misreads costs the
+ * whole check, not one link.
*/
export function extractAliases(content: string): string[] {
if (!content.startsWith("---")) return [];
- const end = content.indexOf("\n---", 3);
+ // CRLF first, and before anything indexes or matches. Obsidian-on-Windows
+ // rewrites LF to CRLF on edit (the reason .gitattributes normalizes), and
+ // `.` does not match CR — it is a line terminator — while `$` without `m`
+ // anchors past it. The block-list item pattern therefore failed on item
+ // ONE of every CRLF note, and `break` read that as an empty list, so the
+ // file's whole alias set went missing with nothing to distinguish it from
+ // a note that declares none.
+ const text = content.replace(/\r\n/g, "\n");
+ const end = text.indexOf("\n---", 3);
if (end === -1) return [];
- const fm = content.slice(3, end);
+ const fm = text.slice(3, end);
const lines = fm.split("\n");
const idx = lines.findIndex((l) => /^aliases:\s*(\[.*\])?\s*$/.test(l.trim()) || /^aliases:\s*\[/.test(l.trim()));
if (idx === -1) return [];
@@ -135,10 +193,12 @@ export function extractAliases(content: string): string[] {
}
return t;
};
- const inline = head.match(/^aliases:\s*\[(.*)\]\s*$/);
+ // A trailing `# comment` is ordinary YAML. Without tolerating it the exact
+ // match failed, the reader fell through to the block branch, found no
+ // indented items, and reported the list as empty.
+ const inline = head.match(/^aliases:\s*\[(.*)\]\s*(?:#.*)?$/);
if (inline) {
- return (inline[1] ?? "")
- .split(",")
+ return splitInlineList(inline[1] ?? "")
.map(unquote)
.filter((s) => s !== "");
}
@@ -146,7 +206,13 @@ export function extractAliases(content: string): string[] {
for (let i = idx + 1; i < lines.length; i++) {
const line = lines[i] ?? "";
const item = line.match(/^\s+-\s+(.*)$/);
- if (!item) break; // end of the block list
+ if (!item) {
+ // A blank line or a comment between entries is legal YAML and the
+ // sequence continues past it; only a line that is neither ends it.
+ // Stopping on them truncated the list at the first one.
+ if (line.trim() === "" || line.trim().startsWith("#")) continue;
+ break; // end of the block list
+ }
const value = unquote(item[1] ?? "");
if (value !== "") out.push(value);
}
diff --git a/.claude/scripts/tests/wikilinks.test.ts b/.claude/scripts/tests/wikilinks.test.ts
index e244f66..c60f5de 100644
--- a/.claude/scripts/tests/wikilinks.test.ts
+++ b/.claude/scripts/tests/wikilinks.test.ts
@@ -195,3 +195,55 @@ describe("extractWikilinkTargets — parser gaps found in the field (2026-07-14)
);
});
});
+
+/**
+ * Shapes the alias reader met in the field and misread. Each cost the WHOLE
+ * alias set or a whole entry, and each surfaced as a broken-link report against
+ * a note whose alias is plainly there — a zero gate red for a reason no author
+ * can act on.
+ */
+describe("extractAliases — ordinary YAML the reader used to lose", () => {
+ const crlf = (...lines: string[]): string => lines.join("\r\n");
+
+ // Obsidian-on-Windows rewrites LF to CRLF on edit; .gitattributes exists
+ // because of it, and normalizes what enters a commit, not what is on disk
+ // while the gate walks the working tree.
+ test("a CRLF note keeps its aliases", () => {
+ const md = crlf("---", "aliases:", " - One", " - Two", "---", "body", "");
+ assert.deepEqual(extractAliases(md), ["One", "Two"]);
+ });
+
+ // Item one is where it broke, so a single-alias note pins the total failure
+ // rather than a truncation that happens to look similar.
+ test("a CRLF note with one alias keeps it", () => {
+ assert.deepEqual(extractAliases(crlf("---", "aliases:", " - Only", "---", "b", "")), ["Only"]);
+ });
+
+ test("an LF note is unaffected by the normalization", () => {
+ const md = "---\naliases:\n - One\n - Two\n---\nbody\n";
+ assert.deepEqual(extractAliases(md), ["One", "Two"]);
+ });
+
+ test("a comma inside a quoted alias is content, not a separator", () => {
+ const md = '---\naliases: ["Smith, John", Nickname]\n---\n';
+ assert.deepEqual(extractAliases(md), ["Smith, John", "Nickname"]);
+ });
+
+ test("a trailing YAML comment does not empty an inline array", () => {
+ assert.deepEqual(extractAliases("---\naliases: [One, Two] # my aliases\n---\n"), ["One", "Two"]);
+ });
+
+ test("a blank line between block entries does not end the list", () => {
+ assert.deepEqual(extractAliases("---\naliases:\n - One\n\n - Two\n---\n"), ["One", "Two"]);
+ });
+
+ test("a comment between block entries does not end the list", () => {
+ assert.deepEqual(extractAliases("---\naliases:\n - One\n # note\n - Two\n---\n"), ["One", "Two"]);
+ });
+
+ // The blank/comment tolerance must not swallow the terminator it was added
+ // beside: a real key still ends the sequence.
+ test("a following scalar key still ends the list", () => {
+ assert.deepEqual(extractAliases("---\naliases:\n - One\n\ntags:\n - brain\n---\n"), ["One"]);
+ });
+});