()
| 9 | } |
| 10 | |
| 11 | function createNoteStore() { |
| 12 | const { subscribe, set, update } = writable<NoteState>({ |
| 13 | content: '', |
| 14 | lastSaved: null, |
| 15 | isDirty: false |
| 16 | }); |
| 17 | |
| 18 | const createFrontmatter = (content: string): Frontmatter => { |
| 19 | const now = new Date(); |
| 20 | const dateStr = now.toISOString(); |
| 21 | const title = `Note ${now.toLocaleString()}`; |
| 22 | const cleanContent = content |
| 23 | .replace(/[#*`_]/g, '') |
| 24 | .replace(/\s+/g, ' ') |
| 25 | .trim(); |
| 26 | |
| 27 | return { |
| 28 | title, |
| 29 | aliases: [''], |
| 30 | description: cleanContent.slice(0, 150) + (cleanContent.length > 150 ? '...' : ''), |
| 31 | date: dateStr, |
| 32 | tags: ['inbox', 'note'], |
| 33 | updated: dateStr, |
| 34 | author: 'User', |
| 35 | }; |
| 36 | }; |
| 37 | |
| 38 | const generateUniqueFilename = () => { |
| 39 | const now = new Date(); |
| 40 | const date = now.toISOString().split('T')[0]; |
| 41 | const time = now.toISOString().split('T')[1] |
| 42 | .replace(/:/g, '-') |
| 43 | .split('.')[0]; |
| 44 | return `${date}-${time}.md`; |
| 45 | }; |
| 46 | |
| 47 | const saveToFile = async (content: string) => { |
| 48 | if (!browser) return; |
| 49 | |
| 50 | const filename = generateUniqueFilename(); |
| 51 | const frontmatter = createFrontmatter(content); |
| 52 | const fileContent = `--- |
| 53 | title: ${frontmatter.title} |
| 54 | aliases: [${(frontmatter.aliases || []).map(alias => `"${alias}"`).join(', ')}] |
| 55 | description: ${frontmatter.description} |
| 56 | date: ${frontmatter.date} |
| 57 | tags: [${(frontmatter.tags || []).map(tag => `"${tag}"`).join(', ')}] |
| 58 | updated: ${frontmatter.updated} |
| 59 | author: ${frontmatter.author} |
| 60 | --- |
| 61 | |
| 62 | ${content}`; |
| 63 | |
| 64 | const response = await fetch('/notes', { |
| 65 | method: 'POST', |
| 66 | headers: { |
| 67 | 'Content-Type': 'application/json', |
| 68 | }, |
no test coverage detected