Skip to content

Commit

Permalink
Fix empty alias fail to load (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrparalon authored Oct 27, 2023
1 parent 0ef5028 commit 558b5c5
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logseq-graph-analysis",
"version": "0.10.2",
"version": "0.10.3",
"type": "module",
"main": "./dist/index.html",
"license": "GPL",
Expand Down
44 changes: 44 additions & 0 deletions src/__snapshots__/graph.ts.snap
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
53 changes: 44 additions & 9 deletions src/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ export async function buildGraph(
g.addNode(page.id, {
...(icon
? {
type: "image",
image: `data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='1.1em' x='0.2em' font-size='70'>${icon}</text></svg>`,
}
type: "image",
image: `data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='1.1em' x='0.2em' font-size='70'>${icon}</text></svg>`,
}
: {
type: "circle",
}),
type: "circle",
}),
label: page.name,
aliases: pageToAliases(page, true),
rawAliases: pageToAliases(page, false),
Expand Down Expand Up @@ -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);
}
Expand All @@ -108,7 +108,7 @@ export function pagesToAliasMap(pages: Page[]): Map<number, number> {
const aliases = new Map<number, number>();
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) {
Expand All @@ -121,6 +121,20 @@ export function pagesToAliasMap(pages: Page[]): Map<number, number> {
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<number, number>,
pages: Page[]
Expand All @@ -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
}
);
}

Expand Down Expand Up @@ -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 () => [
Expand Down
2 changes: 1 addition & 1 deletion src/logseq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface Page {
name: string;
properties?: {
graphHide?: boolean;
alias?: string[];
alias?: string[] | string;
icon?: string;
pageIcon?: string;
};
Expand Down

0 comments on commit 558b5c5

Please sign in to comment.