Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
fix(docs): follow the title the body gives, in the row and in the tree
Pouzor56d83668d4d1
diff --git a/backend/app/api/routes/documents.py b/backend/app/api/routes/documents.pyindex 43d8c3a..d44690d 100644--- a/backend/app/api/routes/documents.py+++ b/backend/app/api/routes/documents.py@@ -97,6 +97,22 @@ def _apply_body(doc: Document, body: str) -> None:doc.tags = tags_from(frontmatter)+async def _adopt_frontmatter_title(db: AsyncSession, doc: Document) -> None:+ """Follow the name the body gives, since the body is what the user edits.++ `title:` in the frontmatter is the only place a document is named — there is+ no rename field anywhere in the UI — so a row that kept the title it was+ created with went stale the moment someone edited that line, and the tree+ went on showing the old name.+ """+ front = doc.frontmatter if isinstance(doc.frontmatter, dict) else {}+ title = front.get("title")+ if not isinstance(title, str) or not title.strip() or title.strip() == doc.title:+ return+ doc.title = title.strip()+ doc.slug = await unique_slug(db, doc.title, parent_id=doc.parent_id, exclude_id=doc.id)++async def _record_revision(db: AsyncSession, doc: Document, reason: str) -> None:"""Snapshot the current body, then prune the document's oldest history."""db.add(DocumentRevision(document_id=doc.id, title=doc.title, body=doc.body or "", reason=reason))@@ -484,6 +500,8 @@ async def update_document(await _record_revision(db, doc, "edit")_apply_body(doc, sent["body"])doc.edited_at = _now()+ # An explicit title in the same request still wins: it is applied below.+ await _adopt_frontmatter_title(db, doc)if "title" in sent and sent["title"]:doc.title = sent["title"]@@ -525,6 +543,8 @@ async def restore_revision(# The body being replaced becomes history too, so a restore is undoable.await _record_revision(db, doc, "restore")_apply_body(doc, revision.body or "")+ # A restored body brings its own title back with it.+ await _adopt_frontmatter_title(db, doc)await db.flush()await doc_search.index_document(db, doc)await db.commit()diff --git a/backend/tests/test_documents.py b/backend/tests/test_documents.pyindex 05f8b3a..29e3fa9 100644--- a/backend/tests/test_documents.py+++ b/backend/tests/test_documents.py@@ -280,6 +280,57 @@ async def test_editing_the_body_refreshes_the_frontmatter_cache(client: AsyncCliassert res.json()["tags"] == ["x"]+async def test_the_body_renames_the_document(client: AsyncClient, headers: dict):+ """`title:` in the frontmatter is the only place a document is named."""+ doc = await _create(client, headers, title="Page")+ res = await client.patch(+ f"/api/v1/documents/{doc['id']}",+ json={"body": "---\ntitle: SMB / CIFS\n---\n\n# SMB / CIFS\n"},+ headers=headers,+ )+ assert res.status_code == 200, res.text+ assert res.json()["title"] == "SMB / CIFS"+ assert res.json()["slug"] == "smb-cifs"+++async def test_a_body_without_a_title_keeps_the_one_it_has(client: AsyncClient, headers: dict):+ doc = await _create(client, headers, title="Page")+ res = await client.patch(+ f"/api/v1/documents/{doc['id']}", json={"body": "---\ntags: [x]\n---\n\nnew"}, headers=headers+ )+ assert res.json()["title"] == "Page"+++async def test_a_blank_title_in_the_body_is_ignored(client: AsyncClient, headers: dict):+ doc = await _create(client, headers, title="Page")+ res = await client.patch(+ f"/api/v1/documents/{doc['id']}", json={"body": "---\ntitle: ' '\n---\n\nnew"}, headers=headers+ )+ assert res.json()["title"] == "Page"+++async def test_an_explicit_title_wins_over_the_body_in_the_same_request(client: AsyncClient, headers: dict):+ doc = await _create(client, headers, title="Page")+ res = await client.patch(+ f"/api/v1/documents/{doc['id']}",+ json={"title": "Chosen", "body": "---\ntitle: From the body\n---\n\nnew"},+ headers=headers,+ )+ assert res.json()["title"] == "Chosen"+++async def test_a_device_document_renamed_in_its_body_keeps_its_device(client: AsyncClient, headers: dict):+ device = await _device(client, headers)+ doc = await _create(client, headers, title="nas-01", kind="device", device_id=device["id"])+ res = await client.patch(+ f"/api/v1/documents/{doc['id']}",+ json={"body": "---\ntitle: The big NAS\n---\n\n# The big NAS\n"},+ headers=headers,+ )+ assert res.json()["title"] == "The big NAS"+ assert res.json()["device_id"] == device["id"]++async def test_renaming_reslugs(client: AsyncClient, headers: dict):doc = await _create(client, headers, title="Page")res = await client.patch(f"/api/v1/documents/{doc['id']}", json={"title": "New name"}, headers=headers)@@ -451,6 +502,21 @@ async def test_restoring_brings_back_an_old_body_and_is_itself_undoable(client:assert [r["reason"] for r in history][0] == "restore"+async def test_restoring_brings_back_the_title_that_body_carried(client: AsyncClient, headers: dict):+ doc = await _create(client, headers, title="Page", body="---\ntitle: First name\n---\n\nfirst")+ await client.patch(+ f"/api/v1/documents/{doc['id']}",+ json={"body": "---\ntitle: Second name\n---\n\nsecond"},+ headers=headers,+ )+ revision = (await client.get(f"/api/v1/documents/{doc['id']}/revisions", headers=headers)).json()[0]++ res = await client.post(+ f"/api/v1/documents/{doc['id']}/revisions/{revision['id']}/restore", headers=headers+ )+ assert res.json()["title"] == "First name"++async def test_restoring_a_revision_of_another_document_is_404(client: AsyncClient, headers: dict):a = await _create(client, headers, title="A", body="one")b = await _create(client, headers, title="B", body="one")diff --git a/frontend/src/documentation/__tests__/tree.test.ts b/frontend/src/documentation/__tests__/tree.test.tsindex 83288b5..3c94b08 100644--- a/frontend/src/documentation/__tests__/tree.test.ts+++ b/frontend/src/documentation/__tests__/tree.test.ts@@ -258,6 +258,26 @@ describe('buildDeviceTree', () => {expect(groups[0].items[0].state).toBe('written')})+ it('shows the name the document gives itself, not the inventory label', () => {+ const groups = buildDeviceTree({+ groupBy: 'flat',+ devices: [device({ label: 'nas-01' })],+ docs: [doc({ device_id: 'dev-1', title: 'The big NAS' })],+ context: emptyContext,+ })+ expect(groups[0].items[0].label).toBe('The big NAS')+ })++ it('falls back to the inventory label while there is nothing to open', () => {+ const groups = buildDeviceTree({+ groupBy: 'flat',+ devices: [device({ label: 'nas-01' })],+ docs: [doc({ device_id: 'dev-1', title: ' ' })],+ context: emptyContext,+ })+ expect(groups[0].items[0].label).toBe('nas-01')+ })+it('lists a multi-homed device under each of its subnets', () => {const groups = buildDeviceTree({groupBy: 'subnet',diff --git a/frontend/src/documentation/tree.ts b/frontend/src/documentation/tree.tsindex cc03f9a..1e65327 100644--- a/frontend/src/documentation/tree.ts+++ b/frontend/src/documentation/tree.ts@@ -243,7 +243,10 @@ export function buildDeviceTree(options: DeviceTreeOptions): TreeGroup[] {const doc = docByDevice.get(device.id)const leaf: TreeLeaf = {id: doc?.id ?? `device:${device.id}`,- label: deviceLabel(device),+ // Once a device is documented the row is that document, so it carries the+ // name the document gives itself — the `title:` line the user edits — and+ // falls back to the inventory's label only while there is nothing to open.+ label: doc?.title?.trim() || deviceLabel(device),docId: doc?.id,deviceId: device.id,kind: doc ? 'device' : 'device-without-doc',