diff --git a/.gitignore b/.gitignore index 52bb891..8d9d6dc 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ node_modules /.svelte-kit /build /visitcount +/notes +/note # OS .DS_Store diff --git a/bun.lockb b/bun.lockb index 212876d..c4f5fc3 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 88064aa..1ab631d 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "dependencies": { "@std/toml": "npm:@jsr/std__toml", "base64url": "^3.0.1", + "nanoid": "^5.0.7", "rehype-autolink-headings": "^7.1.0", "rehype-slug": "^6.0.0", "typescript-svelte-plugin": "^0.3.42" diff --git a/src/lib/index.ts b/src/lib/index.ts index 9e826a2..85b0821 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,7 +1,7 @@ import type { Cookies } from '@sveltejs/kit' import { env } from '$env/dynamic/private' -import { existsSync, readFileSync } from 'fs' import { writable } from 'svelte/store' +import { existsSync, readFileSync } from 'fs' export const scopeCookies = (cookies: Cookies, path: string) => { return { @@ -18,4 +18,4 @@ export const scopeCookies = (cookies: Cookies, path: string) => { } export const visitCountFile = `${env.WEBSITE_DATA_DIR}/visitcount` -export const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0')); \ No newline at end of file +export const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0')); diff --git a/src/lib/notes.ts b/src/lib/notes.ts new file mode 100644 index 0000000..1510224 --- /dev/null +++ b/src/lib/notes.ts @@ -0,0 +1,40 @@ +import { existsSync, readFileSync, writeFileSync } from 'fs' +import { nanoid } from 'nanoid' +import { env } from '$env/dynamic/private' + +export interface Note { + content: string, + published: Date, +} +type NoteId = string + +export const notesFolder = `${env.WEBSITE_DATA_DIR}/note` +export const notesListFile = `${env.WEBSITE_DATA_DIR}/notes` + +export const getNotePath = (id: NoteId) => { return `${notesFolder}/${id}` } +export const genNoteId = () => { + let id = nanoid(8) + while (existsSync(getNotePath(id))) { + id = nanoid(8) + } + return id +} +export const noteExists = (id: NoteId) => { return existsSync(getNotePath(id)) } +export const readNote = (id: NoteId): Note => { + return JSON.parse(readFileSync(getNotePath(id)).toString()) +} +export const writeNote = (id: NoteId, note: Note) => { + writeFileSync(getNotePath(id), JSON.stringify(note)) + // only append to note list if its not in it yet + let noteList = readNotesList() + if (noteList.indexOf(id) === -1) { + writeNotesList(noteList.concat([id])) + } +} + +export const readNotesList = (): NoteId[] => { + return JSON.parse(readFileSync(notesListFile).toString()) +} +export const writeNotesList = (note_ids: NoteId[]) => { + writeFileSync(notesListFile, JSON.stringify(note_ids)) +} diff --git a/src/routes/notes/+page.server.ts b/src/routes/notes/+page.server.ts new file mode 100644 index 0000000..c73804a --- /dev/null +++ b/src/routes/notes/+page.server.ts @@ -0,0 +1,34 @@ +import { noteExists, readNote, readNotesList } from '$lib/notes' + +const notesPerPage: number = 5 + +export const load = ({url}) => { + // get the note id to search for and display the page it is in + const noteId = url.searchParams.get("id") + // get the page no if one is provided, otherwise default to 1 + let page = parseInt(url.searchParams.get("page") || "1") + if (isNaN(page)) { page = 1 } + + // calculate page count + const notesList = readNotesList() + const pageCount = Math.ceil(notesList.length / notesPerPage) + + // find what page the note id if supplied is from + if (noteId !== null && noteExists(noteId)) { + const noteIndex = notesList.lastIndexOf(noteId) + if (noteIndex > -1) { + page = Math.floor(noteIndex / notesPerPage) + 1 + } + } + + // clamp page between our min and max + page = Math.min(page, pageCount) + page = Math.max(page, 1) + + // get the notes from the chosen page + const notes = notesList.slice((page - 1) * notesPerPage, page * notesPerPage).map( + (id) => {return {noteId: id, note: readNote(id)}} + ) + + return {notes, highlightedNote: noteId, page} +} diff --git a/src/routes/notes/+page.svelte b/src/routes/notes/+page.svelte new file mode 100644 index 0000000..0538a43 --- /dev/null +++ b/src/routes/notes/+page.svelte @@ -0,0 +1,18 @@ + + +
+ +
+
[gazesystems /notes/]$ cat *
+{#each data.notes as {noteId, note}}
+
+{noteId}|> {note.content} +{/each} +
+
+
+
diff --git a/src/routes/notes/create/+page.server.ts b/src/routes/notes/create/+page.server.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/routes/notes/create/+page.svelte b/src/routes/notes/create/+page.svelte new file mode 100644 index 0000000..e69de29