fix(watchlist): preserve custom body sections through sync rewrite

renderNote wiped any ## heading not owned by the renderer (Synopsis,
Cast, Links, My Notes) on every sync, destroying user content like
## Collection graph-link sections. extractCustomSections now pulls
non-owned sections out of the existing body before rewrite, and
renderNote re-emits them verbatim, in original order, between Links
and My Notes.
This commit is contained in:
afiqzudinhadi 2026-07-30 14:59:24 +08:00
parent 2c663568ca
commit fb3f52e4b0
6 changed files with 130 additions and 4 deletions

View file

@ -1,5 +1,5 @@
import { describe, expect, test } from 'bun:test';
import { parseNote, extractMyNotes, noteTmdbRef } from 'packages/obsidian/src/watchlist/parse';
import { parseNote, extractMyNotes, noteTmdbRef, extractCustomSections } from 'packages/obsidian/src/watchlist/parse';
const NOTE = `---
type: watchlist_item
@ -55,3 +55,32 @@ describe('noteTmdbRef', () => {
expect(noteTmdbRef({ type: 'list' })).toBeNull();
});
});
describe('extractCustomSections', () => {
test('extracts non-owned section', () => {
const body = `\n# Loki\n\n## Links\n- [IMDb](x)\n\n## Collection\nPart of [[Movies]]\n\n## My Notes\n\ngreat finale\n`;
expect(extractCustomSections(body)).toEqual([{ heading: 'Collection', content: 'Part of [[Movies]]' }]);
});
test('owned headings excluded (Synopsis, Cast, Links, My Notes)', () => {
const body = `\n## Synopsis\nblah\n\n## Cast\nA, B\n\n## Links\n- x\n\n## My Notes\n\nnote\n`;
expect(extractCustomSections(body)).toEqual([]);
});
test('two custom sections keep order', () => {
const body = `\n## Collection\nPart of [[Movies]]\n\n## Rewatch Log\n- 2024-01-01\n- 2025-02-02\n\n## My Notes\n\nnote\n`;
expect(extractCustomSections(body)).toEqual([
{ heading: 'Collection', content: 'Part of [[Movies]]' },
{ heading: 'Rewatch Log', content: '- 2024-01-01\n- 2025-02-02' },
]);
});
test('preserves internal blank lines, trims trailing', () => {
const body = `\n## Collection\nline one\n\nline two\n\n\n## My Notes\n\nnote\n`;
expect(extractCustomSections(body)).toEqual([{ heading: 'Collection', content: 'line one\n\nline two' }]);
});
test('no custom sections → empty array', () => {
const body = `\n# Loki\n\n## Links\n- x\n\n## My Notes\n\nnote\n`;
expect(extractCustomSections(body)).toEqual([]);
});
test('empty/missing body → empty array', () => {
expect(extractCustomSections('')).toEqual([]);
});
});

View file

@ -37,3 +37,28 @@ describe('renderNote golden', () => {
expect(renderNote(r, '')).not.toContain('**Rating:**');
});
});
describe('renderNote custom sections', () => {
test('no custom sections → output unchanged (golden)', () => {
const expected = readFileSync(join(import.meta.dir, 'fixtures', 'canonical-series.md'), 'utf-8');
expect(renderNote(LOKI, '', [])).toBe(expected);
});
test('single custom section placed after Links, before My Notes', () => {
const out = renderNote(LOKI, '', [{ heading: 'Collection', content: 'Part of [[Movies]]' }]);
const linksIdx = out.indexOf('## Links');
const collectionIdx = out.indexOf('## Collection');
const myNotesIdx = out.indexOf('## My Notes');
expect(linksIdx).toBeGreaterThan(-1);
expect(collectionIdx).toBeGreaterThan(linksIdx);
expect(myNotesIdx).toBeGreaterThan(collectionIdx);
expect(out).toContain('## Collection\nPart of [[Movies]]\n');
});
test('multiple custom sections keep relative order', () => {
const out = renderNote(LOKI, '', [
{ heading: 'Collection', content: 'Part of [[Movies]]' },
{ heading: 'Rewatch Log', content: '- 2024-01-01' },
]);
expect(out.indexOf('## Collection')).toBeLessThan(out.indexOf('## Rewatch Log'));
expect(out.indexOf('## Rewatch Log')).toBeLessThan(out.indexOf('## My Notes'));
});
});

View file

@ -22,6 +22,13 @@ keep me
const AIRING_NOTE = ENDED_NOTE.replace('status: Ended', 'status: Returning Series').replace('last_air_date: 2023-11-09', 'last_air_date: 2023-10-01');
const NOTE_WITH_COLLECTION = ENDED_NOTE.replace('## My Notes', '## Collection\nPart of [[Movies]]\n\n## My Notes');
const NOTE_WITH_TWO_CUSTOM_SECTIONS = ENDED_NOTE.replace(
'## My Notes',
'## Collection\nPart of [[Movies]]\n\n## Rewatch Log\n- 2024-01-01\n- 2025-02-02\n\n## My Notes',
);
function makeDeps(notes: { path: string; content: string }[], detail: any = tvDetail) {
const contents = new Map(notes.map(n => [n.path, n.content]));
const writes: { path: string; content: string }[] = [];
@ -163,6 +170,38 @@ describe('syncFolder', () => {
expect(report.errors[0].path).toBe('Bad.md');
expect(report.synced).toBe(1);
});
test('custom section round-trips through rewrite, positioned after Links and before My Notes', async () => {
const { deps, writes } = makeDeps([{ path: 'Loki.md', content: NOTE_WITH_COLLECTION }]);
await syncFolder(deps, { full: true });
const out = writes[0].content;
expect(out).toContain('## Collection\nPart of [[Movies]]\n');
const linksIdx = out.indexOf('## Links');
const collectionIdx = out.indexOf('## Collection');
const myNotesIdx = out.indexOf('## My Notes');
expect(collectionIdx).toBeGreaterThan(linksIdx);
expect(myNotesIdx).toBeGreaterThan(collectionIdx);
});
test('two custom sections keep relative order through rewrite', async () => {
const { deps, writes } = makeDeps([{ path: 'Loki.md', content: NOTE_WITH_TWO_CUSTOM_SECTIONS }]);
await syncFolder(deps, { full: true });
const out = writes[0].content;
expect(out.indexOf('## Collection')).toBeLessThan(out.indexOf('## Rewatch Log'));
expect(out.indexOf('## Rewatch Log')).toBeLessThan(out.indexOf('## My Notes'));
});
test('idempotence: custom section stable across two sync passes (render→parse→extract→render byte-identical)', async () => {
const { deps, writes } = makeDeps([{ path: 'Loki.md', content: NOTE_WITH_COLLECTION }]);
await syncFolder(deps, { full: true });
const rendered = writes[0].content;
const second = makeDeps([{ path: 'Loki.md', content: rendered }]);
const report = await syncFolder(second.deps, { full: true });
expect(second.writes.length).toBe(0);
expect(report.written).toBe(0);
});
test('golden: note with no custom sections unaffected by custom-section plumbing', async () => {
const { deps, writes } = makeDeps([{ path: 'Loki.md', content: ENDED_NOTE }]);
await syncFolder(deps, { full: true });
expect(writes[0].content).not.toContain('## Collection');
});
test('stale-read guard: content edited mid-sync is re-read fresh, not clobbered by early snapshot', async () => {
const { deps, writes, contents } = makeDeps([
{ path: 'A.md', content: AIRING_NOTE },