feat: notes wip, viewing + data structure
This commit is contained in:
parent
85bef3d9f5
commit
64869c91c4
2
.gitignore
vendored
2
.gitignore
vendored
@ -6,6 +6,8 @@ node_modules
|
||||
/.svelte-kit
|
||||
/build
|
||||
/visitcount
|
||||
/notes
|
||||
/note
|
||||
|
||||
# OS
|
||||
.DS_Store
|
||||
|
@ -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"
|
||||
|
@ -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'));
|
||||
export const visitCount = writable(parseInt(existsSync(visitCountFile) ? readFileSync(visitCountFile).toString() : '0'));
|
||||
|
40
src/lib/notes.ts
Normal file
40
src/lib/notes.ts
Normal file
@ -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))
|
||||
}
|
34
src/routes/notes/+page.server.ts
Normal file
34
src/routes/notes/+page.server.ts
Normal file
@ -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}
|
||||
}
|
18
src/routes/notes/+page.svelte
Normal file
18
src/routes/notes/+page.svelte
Normal file
@ -0,0 +1,18 @@
|
||||
<script lang="ts">
|
||||
import Window from '../../components/window.svelte';
|
||||
|
||||
export let data;
|
||||
</script>
|
||||
|
||||
<div class="flex">
|
||||
<Window title="notes">
|
||||
<div class="prose prose-ralsei">
|
||||
<pre class="language-bash"><code class="language-bash"><span class="token punctuation">[</span>gazesystems <span class="token keyword">/notes/</span><span class="token punctuation">]$</span> <span class="token function">cat</span> *
|
||||
{#each data.notes as {noteId, note}}
|
||||
<br>
|
||||
<span class="token keyword">{noteId}</span><span class="token punctuation">|> </span><span class="token string">{note.content}</span>
|
||||
{/each}
|
||||
</code></pre>
|
||||
</div>
|
||||
</Window>
|
||||
</div>
|
0
src/routes/notes/create/+page.server.ts
Normal file
0
src/routes/notes/create/+page.server.ts
Normal file
0
src/routes/notes/create/+page.svelte
Normal file
0
src/routes/notes/create/+page.svelte
Normal file
Loading…
Reference in New Issue
Block a user