Skip to content

Commit

Permalink
add titles to example notes
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Apr 20, 2024
1 parent fa6b3a9 commit 1ee9c98
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ Run `deno lint` to lint the code.

### Maintenance

Run `deno task generate:example` to generate the generated code for the example.
Run `deno task generate` to generate the generated code for the project.

### Example

Run `deno task example` to run the example,
[./example/example.ts](./example/example.ts).
Expand Down
49 changes: 38 additions & 11 deletions example/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,47 @@ if (import.meta.main) {
function main() {
const note1 = myLab.execute(
"notes.add",
{ content: "Hello, world!" },
{ title: "Hi", content: "Hello, world!" },
);
const note2 = myLab.execute(
"notes.add",
{ content: "Goodbye, world!" },
{ title: "Bye", content: "Goodbye, world!" },
);
myLab.execute("links.link", { ids: [note1.id, note2.id] });

console.log(`Note 1: ${note1.id}`);
console.log(`Note 2: ${note2.id}`);
console.log("Linked notes:");
for (const linkable of myLab.execute("links.list", {})) {
console.log(
`- ${linkable.id} => ${linkable.links.map(({ id }) => id).join(", ")}`,
);
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";
}

0 comments on commit 1ee9c98

Please sign in to comment.