+
-
+
diff --git a/src/composables/useNotes.ts b/src/composables/useNotes.ts
index dc410dd..dd57a6f 100644
--- a/src/composables/useNotes.ts
+++ b/src/composables/useNotes.ts
@@ -1,11 +1,24 @@
-import { ref, computed, watch } from 'vue'
+import { ref, reactive, computed, watch } from 'vue'
import { useTitle } from '@vueuse/core'
import { mdToHtml } from '@/utils/markdown'
+import { getAllMatches } from '@/utils/helpers'
+import shortid from 'shortid'
+
+const contextedPrefix = 'c@'
+
+const baseNotes = reactive<{ [noteId: string]: BaseNote }>({})
-const baseNotes = ref([])
export const notes = computed(() => {
- // extract links and add word count
- return baseNotes.value as Note[]
+ return Object.entries(baseNotes)
+ .map(([_, note]) => ({
+ ...note,
+ wordCount: note.content.split(' ').length,
+ }))
+ .sort((a, b) => b.modified - a.modified) as Note[]
+})
+watch(notes, () => {
+ if (notes.value.length > 0 && !activeNote.value)
+ activeNote.value = rootNote.value
})
export const activeNote = ref()
@@ -16,16 +29,11 @@ watch(activeNote, () => {
export const rootNote = computed(() =>
notes.value.find((note: Note) => note.isRoot)
)
-watch(
- rootNote,
- () => {
- if (rootNote.value) activeNote.value = rootNote.value
- },
- { immediate: true }
-)
export const setDefaultNotes = (defaultNotes: BaseNote[]) => {
- baseNotes.value = defaultNotes
+ defaultNotes.forEach((defaultNote) => {
+ baseNotes[defaultNote.id] = defaultNote
+ })
}
export const getNoteById = (noteId: string) => {
@@ -35,7 +43,7 @@ export const getNoteById = (noteId: string) => {
export const findNotes = (query: string): Note[] => {
const removeMdFromText = (mdText: string): string => {
const div = document.createElement('div')
- div.innerHTML = mdToHtml(mdText, 'c@', getNoteById)
+ div.innerHTML = mdToHtml(mdText, contextedPrefix, getNoteById)
const textWithoutMd = div.textContent || div.innerText || ''
return textWithoutMd
}
@@ -47,3 +55,80 @@ export const findNotes = (query: string): Note[] => {
return matchTitle || matchContent
})
}
+
+export const updateNote = (noteId: string, note: Note) => {
+ const updatedNote: Note = {
+ ...note,
+ modified: new Date().getTime(),
+ }
+ baseNotes[noteId] = updatedNote
+}
+
+export const addNote = (
+ title: string,
+ content: string,
+ goToNote: boolean = false
+) => {
+ const id = shortid.generate()
+ const newNote: BaseNote = {
+ id,
+ title,
+ content,
+ created: new Date().getTime(),
+ modified: new Date().getTime(),
+ }
+ baseNotes[id] = newNote
+ if (goToNote)
+ activeNote.value = notes.value.find((note) => note.id === newNote.id)
+}
+
+const getNoteLinksByNoteId = (noteId: string): string[] => {
+ const note = baseNotes[noteId]
+ const regex = /\[\[(.*?)\]\]/g
+ const links = getAllMatches(regex, note.content || '')
+ .map((to) => notes.value.find((note) => note.title === to[1])?.id || '')
+ .filter((noteId) => Object.keys(baseNotes).includes(noteId))
+ return [...links]
+}
+
+interface NoteRelations {
+ id: string
+ to: string[]
+ from: string[]
+}
+
+interface NotesRelations {
+ [noteId: string]: {
+ to: string[]
+ from: string[]
+ }
+}
+export const notesRelations = computed(() => {
+ const noteIds = Object.keys(baseNotes)
+ const relations = noteIds
+ .filter((id) => id !== undefined)
+ .map((id) => {
+ const to = getNoteLinksByNoteId(id)
+ return { id, to }
+ })
+ .map((noteRelations, _, notesRelations): NoteRelations => {
+ const from = [...notesRelations]
+ .map((noteRelation) =>
+ noteRelation.to
+ .filter((toId) => toId === noteRelations.id)
+ .map(() => noteRelation.id)
+ )
+ .reduce((arr, elem) => arr.concat(elem), [])
+ .filter((value, index, self) => self.indexOf(value) === index)
+ return {
+ id: noteRelations.id,
+ to: noteRelations.to,
+ from,
+ }
+ })
+ .reduce((notes, { id, to, from }) => {
+ notes[id] = { to, from }
+ return notes
+ }, {} as NotesRelations)
+ return relations
+})
diff --git a/src/global.d.ts b/src/global.d.ts
index 7822dcd..dcf1ec6 100644
--- a/src/global.d.ts
+++ b/src/global.d.ts
@@ -10,10 +10,6 @@ declare global {
interface Note extends BaseNote {
wordCount: number
- links: {
- to: string[]
- from: string[]
- }
}
interface ViewMode {
diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts
new file mode 100644
index 0000000..418c3bd
--- /dev/null
+++ b/src/utils/helpers.ts
@@ -0,0 +1,26 @@
+import { formatDistance, format, isToday } from 'date-fns'
+
+export const formatDate = (date: Date | number): string => {
+ const dateToFormat = ['number', 'string'].includes(typeof date)
+ ? new Date(date)
+ : date
+ const dateDistanceInWords = formatDistance(dateToFormat, new Date()) + ' ago'
+
+ return isToday(date)
+ ? dateDistanceInWords
+ : format(date, 'D MMMM [at] HH:mm ')
+}
+
+export const getAllMatches = (
+ regex: RegExp,
+ input: string
+): RegExpExecArray[] => {
+ const matches = []
+ let m
+ do {
+ m = regex.exec(input)
+ // console.log(m)
+ if (m) matches.push(m)
+ } while (m)
+ return matches
+}