|
1 | | -import { json } from '@sveltejs/kit'; |
2 | | -import type { RequestHandler } from './$types'; |
3 | | -import PocketBase from 'pocketbase'; |
4 | | -import { promises as fs } from 'fs'; |
5 | | -import { getAuthenticatedPocketBase } from '$lib/server/auth'; |
| 1 | +import { json } from "@sveltejs/kit"; |
| 2 | +import type { RequestHandler } from "./$types"; |
| 3 | +import PocketBase from "pocketbase"; |
| 4 | +import { promises as fs } from "fs"; |
| 5 | +import { getAuthenticatedPocketBase } from "$lib/server/auth"; |
6 | 6 |
|
7 | 7 | const pb = await getAuthenticatedPocketBase(); |
8 | 8 |
|
9 | 9 | async function getBacklinks(url) { |
10 | | - const mdbaseCollection = pb.collection('mdbase'); |
11 | | - const documentUrl = url; |
12 | | - try { |
13 | | - if (!documentUrl) { |
14 | | - return new Response(JSON.stringify({ message: 'URL parameter is required' }), { |
15 | | - status: 400 |
16 | | - }); |
17 | | - } |
| 10 | + const mdbaseCollection = pb.collection("mdbase"); |
| 11 | + const documentUrl = url; |
| 12 | + try { |
| 13 | + if (!documentUrl) { |
| 14 | + return new Response( |
| 15 | + JSON.stringify({ message: "URL parameter is required" }), |
| 16 | + { |
| 17 | + status: 400, |
| 18 | + }, |
| 19 | + ); |
| 20 | + } |
18 | 21 |
|
19 | | - const documents = await mdbaseCollection.getList(1, 1, { |
20 | | - filter: `url="${documentUrl}"`, |
21 | | - expand: 'backlinks' |
22 | | - }); |
| 22 | + const documents = await mdbaseCollection.getList(1, 1, { |
| 23 | + filter: `url="${documentUrl}"`, |
| 24 | + expand: "backlinks", |
| 25 | + }); |
23 | 26 |
|
24 | | - if (documents.items.length === 0) { |
25 | | - return new Response(JSON.stringify({ message: 'Document not found' }), { status: 404 }); |
26 | | - } |
| 27 | + if (documents.items.length === 0) { |
| 28 | + return new Response(JSON.stringify({ message: "Document not found" }), { |
| 29 | + status: 404, |
| 30 | + }); |
| 31 | + } |
27 | 32 |
|
28 | | - const document = documents.items[0]; |
| 33 | + const document = documents.items[0]; |
29 | 34 |
|
30 | | - const backLinks = (document.expand?.backlinks || []).map((link) => ({ |
31 | | - id: link.id, |
32 | | - title: link.title, |
33 | | - url: link.url |
34 | | - })); |
| 35 | + const backLinks = (document.expand?.backlinks || []).map((link) => ({ |
| 36 | + id: link.id, |
| 37 | + title: link.title, |
| 38 | + url: link.url, |
| 39 | + })); |
35 | 40 |
|
36 | | - return backLinks; |
37 | | - } catch (error: any) { |
38 | | - console.error('Error in backlinks API:', error); |
39 | | - return {}; |
40 | | - } |
| 41 | + return backLinks; |
| 42 | + } catch (error: any) { |
| 43 | + console.error("Error in backlinks API:", error); |
| 44 | + return {}; |
| 45 | + } |
41 | 46 | } |
42 | 47 |
|
43 | 48 | async function computeGraphData(fileUrl) { |
44 | | - const currentPage = await pb.collection('mdbase').getFirstListItem(`url="${fileUrl}"`); |
45 | | - const relatedPages = await pb.collection('mdbase').getList(1, 50, { |
46 | | - filter: `id ?~ "${currentPage.backlinks}" || id ?~ "${currentPage.links}"` |
47 | | - }); |
| 49 | + const currentPage = await pb |
| 50 | + .collection("mdbase") |
| 51 | + .getFirstListItem(`url="${fileUrl}"`); |
| 52 | + const relatedPages = await pb.collection("mdbase").getList(1, 50, { |
| 53 | + filter: `id ?~ "${currentPage.backlinks}" || id ?~ "${currentPage.links}"`, |
| 54 | + }); |
48 | 55 |
|
49 | | - // Use a Set to store unique node IDs |
50 | | - const uniqueNodeIds = new Set([currentPage.id]); |
| 56 | + // Use a Set to store unique node IDs |
| 57 | + const uniqueNodeIds = new Set([currentPage.id]); |
51 | 58 |
|
52 | | - // Create nodes array with current page |
53 | | - const nodes = [{ id: currentPage.id, label: currentPage.title, color: '#ff0000' }]; |
| 59 | + // Create nodes array with current page |
| 60 | + const nodes = [ |
| 61 | + { id: currentPage.id, label: currentPage.title, color: "#ff0000" }, |
| 62 | + ]; |
54 | 63 |
|
55 | | - // Add related pages to nodes array, avoiding duplicates |
56 | | - relatedPages.items.forEach((p) => { |
57 | | - if (!uniqueNodeIds.has(p.id)) { |
58 | | - uniqueNodeIds.add(p.id); |
59 | | - nodes.push({ id: p.id, label: p.title, color: '#00ff00' }); |
60 | | - } |
61 | | - }); |
| 64 | + // Add related pages to nodes array, avoiding duplicates |
| 65 | + relatedPages.items.forEach((p) => { |
| 66 | + if (!uniqueNodeIds.has(p.id)) { |
| 67 | + uniqueNodeIds.add(p.id); |
| 68 | + nodes.push({ id: p.id, label: p.title, color: "#00ff00" }); |
| 69 | + } |
| 70 | + }); |
62 | 71 |
|
63 | | - // Create edges array |
64 | | - const edges = [ |
65 | | - ...currentPage.links.map((link) => ({ from: currentPage.id, to: link })), |
66 | | - ...currentPage.backlinks.map((backlink) => ({ from: backlink, to: currentPage.id })) |
67 | | - ]; |
| 72 | + // Create edges array |
| 73 | + const edges = [ |
| 74 | + ...currentPage.links.map((link) => ({ from: currentPage.id, to: link })), |
| 75 | + ...currentPage.backlinks.map((backlink) => ({ |
| 76 | + from: backlink, |
| 77 | + to: currentPage.id, |
| 78 | + })), |
| 79 | + ]; |
68 | 80 |
|
69 | | - return { nodes, edges }; |
| 81 | + return { nodes, edges }; |
70 | 82 | } |
71 | 83 | // Main load function |
72 | 84 | export async function load({ params, fetch, locals }) { |
73 | | - try { |
74 | | - // Step 1: Authenticate |
75 | | - /* console.log(pb); */ |
76 | | - console.log(params.post); |
77 | | - const post = await pb |
78 | | - .collection('mdbase') |
79 | | - .getFirstListItem(`url="${params.post}.md"`, { expand: 'tags' }); |
80 | | - const backlinks = await getBacklinks(`${params.post}.md`); |
81 | | - const graphData = await computeGraphData(`${params.post}.md`); |
| 85 | + try { |
| 86 | + // Step 1: Authenticate |
| 87 | + /* console.log(pb); */ |
| 88 | + console.log(params.post); |
| 89 | + const post = await pb |
| 90 | + .collection("mdbase") |
| 91 | + .getFirstListItem(`url="${params.post}.md"`, { expand: "tags" }); |
| 92 | + const backlinks = await getBacklinks(`${params.post}.md`); |
| 93 | + // const graphData = await computeGraphData(`${params.post}.md`); |
82 | 94 |
|
83 | | - const tags = post.expand?.tags.map((tag) => { |
84 | | - return { |
85 | | - name: tag.tag |
86 | | - }; |
87 | | - }); |
88 | | - console.log(tags); |
| 95 | + const tags = post.expand?.tags.map((tag) => { |
| 96 | + return { |
| 97 | + name: tag.tag, |
| 98 | + }; |
| 99 | + }); |
| 100 | + console.log(tags); |
89 | 101 |
|
90 | | - return { post, title: post.title, backlinks, tags }; |
91 | | - } catch (error) { |
92 | | - console.error(`Failed to fetch post: ${error}`); |
93 | | - return { message: `Failed to fetch post: ${error}` }; |
94 | | - } |
| 102 | + return { post, title: post.title, backlinks, tags }; |
| 103 | + } catch (error) { |
| 104 | + console.error(`Failed to fetch post: ${error}`); |
| 105 | + return { message: `Failed to fetch post: ${error}` }; |
| 106 | + } |
95 | 107 | } |
0 commit comments