-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.ts
53 lines (43 loc) · 1.11 KB
/
example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { myLab } from "./my_lab.ts";
if (import.meta.main) {
main();
}
function main() {
const note1 = myLab.execute(
"notes.add",
{ title: "Hi", content: "Hello, world!" },
);
const note2 = myLab.execute(
"notes.add",
{ title: "Bye", content: "Goodbye, world!" },
);
myLab.execute(
"links.link",
{ ids: [note1.id, note2.id] },
);
console.log(renderNote(note1.id));
}
function renderNote(id: string): string {
const note = myLab.execute("notes.get", { id });
if (!note) {
throw new Error(`Note not found: ${id}`);
}
const title = renderTitle(note.title);
const links = myLab.execute("links.get", { id });
return `# [${title}](${id})
${note.content ?? "No content."}
## Links
${
!links || links.links.length === 0 ? "None" : links.links.map(({ id }) => {
const linkedNote = myLab.execute("notes.get", { id });
if (!linkedNote) {
throw new Error(`Linked note not found: ${id}`);
}
return `- [${renderTitle(linkedNote.title)}](${id})`;
}).join("\n")
}
`;
}
function renderTitle(title?: string): string {
return title ?? "Untitled";
}