From 558b5c59627862098b0cf1e781271de009ad6605 Mon Sep 17 00:00:00 2001 From: mrparalon Date: Fri, 27 Oct 2023 23:25:19 +0400 Subject: [PATCH] Fix empty alias fail to load (#51) --- package.json | 2 +- src/__snapshots__/graph.ts.snap | 44 +++++++++++++++++++++++++++ src/graph.ts | 53 +++++++++++++++++++++++++++------ src/logseq.ts | 2 +- 4 files changed, 90 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index f711775..60e789b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "logseq-graph-analysis", - "version": "0.10.2", + "version": "0.10.3", "type": "module", "main": "./dist/index.html", "license": "GPL", diff --git a/src/__snapshots__/graph.ts.snap b/src/__snapshots__/graph.ts.snap index 60b408f..5a10f7b 100644 --- a/src/__snapshots__/graph.ts.snap +++ b/src/__snapshots__/graph.ts.snap @@ -1,5 +1,49 @@ // Vitest Snapshot v1 +exports[`buildGraph > alias nodes empty cases shoud not fail 1`] = ` +{ + "attributes": { + "isInited": true, + }, + "edges": [], + "nodes": [ + { + "attributes": { + "aliases": [], + "label": "A", + "rawAliases": [], + "type": "circle", + }, + }, + { + "attributes": { + "aliases": [ + "TEST", + ], + "label": "B", + "rawAliases": [ + "test", + ], + "type": "circle", + }, + }, + { + "attributes": { + "aliases": [], + "label": "C", + "rawAliases": [], + "type": "circle", + }, + }, + ], + "options": { + "allowSelfLoops": true, + "multi": false, + "type": "mixed", + }, +} +`; + exports[`buildGraph > creates a graph with edges 1`] = ` { "attributes": { diff --git a/src/graph.ts b/src/graph.ts index fd5507d..c460cb8 100644 --- a/src/graph.ts +++ b/src/graph.ts @@ -50,12 +50,12 @@ export async function buildGraph( g.addNode(page.id, { ...(icon ? { - type: "image", - image: `data:image/svg+xml,${icon}`, - } + type: "image", + image: `data:image/svg+xml,${icon}`, + } : { - type: "circle", - }), + type: "circle", + }), label: page.name, aliases: pageToAliases(page, true), rawAliases: pageToAliases(page, false), @@ -96,7 +96,7 @@ export async function buildGraph( console.log("graph complete", g.size); - if (!g.getAttribute("isInited")){ + if (!g.getAttribute("isInited")) { random.assign(g); g.setAttribute("isInited", true); } @@ -108,7 +108,7 @@ export function pagesToAliasMap(pages: Page[]): Map { const aliases = new Map(); for (const page of pages) { if (page.properties && page.properties.alias) { - const aliasedPages = page.properties.alias.map((a) => + const aliasedPages = normalizeAlias(page).map((a) => pages.find((p) => p.name.toUpperCase() === a.toUpperCase()) ); for (const alias of aliasedPages) { @@ -121,6 +121,20 @@ export function pagesToAliasMap(pages: Page[]): Map { return aliases; } +function normalizeAlias(input: Page): string[] { + if (!input.properties?.alias) { + return []; + } + if (input.properties.alias === '') { + return []; + } else if (typeof input.properties.alias === "string") { + return [input.properties.alias]; + } + else { + return input.properties.alias; + } +} + export function removeAliases( aliases: Map, pages: Page[] @@ -129,8 +143,10 @@ export function removeAliases( } export function pageToAliases(page: Page, upper: boolean): string[] { - return (page.properties?.alias ?? []).map((a) => - upper ? a.toUpperCase() : a + console.log(normalizeAlias(page)); + return (normalizeAlias(page)).map((a) =>{ + return upper ? a.toUpperCase() : a + } ); } @@ -455,6 +471,25 @@ if (import.meta.vitest) { ); expect(graphToJson(graph)).toMatchSnapshot(); }); + it("alias nodes empty cases shoud not fail", async () => { + const getAllPages = async () => [ + { id: 1, "journal?": false, name: "A", properties: { alias: "" } }, + { id: 2, "journal?": false, name: "B", properties: { alias: ["test"] } }, + { id: 3, "journal?": false, name: "C" }, + ]; + const getBlockReferences = async () => [ + [], + ]; + const getSettings = () => ({ journal: false }); + const getBlock = async (ref: BlockIdentity | EntityID) => null; + const graph = await buildGraph( + getAllPages, + getBlockReferences, + getSettings, + getBlock + ); + expect(graphToJson(graph)).toMatchSnapshot(); + }); it("links shared references in a block", async () => { const getAllPages = async () => [ diff --git a/src/logseq.ts b/src/logseq.ts index 7fe5b37..ce0b3cd 100644 --- a/src/logseq.ts +++ b/src/logseq.ts @@ -10,7 +10,7 @@ export interface Page { name: string; properties?: { graphHide?: boolean; - alias?: string[]; + alias?: string[] | string; icon?: string; pageIcon?: string; };