implement search

This commit is contained in:
2023-04-29 14:31:31 +02:00
parent b7055eb8d6
commit 1d6826e255
14 changed files with 508 additions and 279 deletions

View File

@@ -1,4 +1,4 @@
export const defaultNotes: Note[] = [
export const defaultNotes: BaseNote[] = [
{
isRoot: true,
title: 'Your first note',

39
src/utils/markdown.ts Normal file
View File

@@ -0,0 +1,39 @@
import { marked } from 'marked'
import DOMPurify from 'dompurify'
export function mdToHtml(
mdText: string,
contextedPrefix: string,
getNoteById: (id: string) => Note | undefined
) {
const renderer = new marked.Renderer()
renderer.link = (href, _, text) => {
const isContextedLink = href?.startsWith(contextedPrefix)
if (isContextedLink) {
const re = new RegExp(`${contextedPrefix}([^]+)`)
const match = re.exec(href || '')
const contextedLinkOptions = match ? match[1].split(';') : []
const noteId = contextedLinkOptions[0] || ''
const note = getNoteById(noteId)
const contextedHref = ''
const contextedLink = `<a data-contexted-link="${noteId}" title="${note?.title}"${contextedHref}>${text}</a>`
return note?.title ? contextedLink : text
} else {
return `<a target="_blank" href="${href}">${text}</a>`
}
}
const html = DOMPurify.sanitize(marked.parse(mdText, { renderer }))
const re = /(\[\[)(.*?)(\]\])/g
const doc = new DOMParser().parseFromString(html, 'text/html')
doc.querySelectorAll('p, b, u, i, li, h1, h2, h3').forEach((element) => {
// if (element.childElementCount === 0) {
element.innerHTML = element.innerHTML.replace(re, (_, p1, p2, p3) => {
// const { id: noteId } = getters.getNoteByTitle(p2) || {}
return `${p1}<a data-contexted-link="true">${p2}</a>${p3}`
})
// }
})
return doc.body.innerHTML
}