📥 relocate İçe Aktar (Import) button to Notes page header as a separate side panel
Browse files at this commit048fc0c4f57ba657d258b5ffc458e8b68999f8b0Parent: 7e8f653- feat
- 📥 relocate İçe Aktar (Import) button to Notes page header as a separate side panel
6 changed files +532 -5
- src/components/notes/note-import-panel.tsx +199 -0
- src/components/ui/icons.tsx +10 -0
- src/i18n/messages.ts +6 -0
- src/lib/__tests__/note-importer.test.ts +69 -0
- src/lib/note-importer.ts +215 -0
- src/pages/notes-page.tsx +33 -5
src/components/notes/note-import-panel.tsxadded+199 -0
| @@ -0,0 +1,199 @@ | ||
| 1 | +import { Show, createSignal } from "solid-js"; | |
| 2 | +import { Button } from "@/components/ui/button"; | |
| 3 | +import { IconFileText, IconUploadCloud } from "@/components/ui/icons"; | |
| 4 | +import { Input } from "@/components/ui/input"; | |
| 5 | +import { Label } from "@/components/ui/label"; | |
| 6 | +import { Textarea } from "@/components/ui/textarea"; | |
| 7 | +import { processImportedText } from "@/lib/note-importer"; | |
| 8 | +import { formatBytes } from "@/lib/upload-limits"; | |
| 9 | +import { useT } from "@/stores/preferences-context"; | |
| 10 | + | |
| 11 | +export function NoteImportPanel(props: { | |
| 12 | + onImport: (title: string, content: string) => void; | |
| 13 | + onCancel?: () => void; | |
| 14 | +}) { | |
| 15 | + const t = useT(); | |
| 16 | + let fileInput: HTMLInputElement | undefined; | |
| 17 | + | |
| 18 | + const [file, setFile] = createSignal<File | null>(null); | |
| 19 | + const [processing, setProcessing] = createSignal(false); | |
| 20 | + const [error, setError] = createSignal(""); | |
| 21 | + const [importedTitle, setImportedTitle] = createSignal(""); | |
| 22 | + const [importedMarkdown, setImportedMarkdown] = createSignal(""); | |
| 23 | + const [hasGarbled, setHasGarbled] = createSignal(false); | |
| 24 | + | |
| 25 | + const handleFileSelect = async (selectedFile: File | undefined) => { | |
| 26 | + if (!selectedFile) return; | |
| 27 | + setError(""); | |
| 28 | + | |
| 29 | + const name = selectedFile.name.toLowerCase(); | |
| 30 | + const isText = name.endsWith(".txt") || name.endsWith(".md") || selectedFile.type.startsWith("text/"); | |
| 31 | + const isPdf = name.endsWith(".pdf") || selectedFile.type === "application/pdf"; | |
| 32 | + | |
| 33 | + if (!isText && !isPdf) { | |
| 34 | + setError(t("notes.previewUnsupported")); | |
| 35 | + return; | |
| 36 | + } | |
| 37 | + | |
| 38 | + setFile(selectedFile); | |
| 39 | + setProcessing(true); | |
| 40 | + | |
| 41 | + try { | |
| 42 | + let rawText = ""; | |
| 43 | + if (isPdf) { | |
| 44 | + rawText = await extractPdfText(selectedFile); | |
| 45 | + } else { | |
| 46 | + rawText = await selectedFile.text(); | |
| 47 | + } | |
| 48 | + | |
| 49 | + const result = processImportedText(rawText, selectedFile.name); | |
| 50 | + setImportedTitle(result.title); | |
| 51 | + setImportedMarkdown(result.markdown); | |
| 52 | + setHasGarbled(result.hasGarbledWarning); | |
| 53 | + } catch (err) { | |
| 54 | + setError(t("notes.previewUnsupported")); | |
| 55 | + } finally { | |
| 56 | + setProcessing(false); | |
| 57 | + } | |
| 58 | + }; | |
| 59 | + | |
| 60 | + const handleApply = () => { | |
| 61 | + const title = importedTitle().trim() || "Imported Note"; | |
| 62 | + const content = importedMarkdown(); | |
| 63 | + props.onImport(title, content); | |
| 64 | + }; | |
| 65 | + | |
| 66 | + return ( | |
| 67 | + <div class="space-y-5"> | |
| 68 | + <Show when={!file()}> | |
| 69 | + <div | |
| 70 | + class="flex min-h-[14rem] cursor-pointer flex-col items-center justify-center gap-3 rounded-2xl border border-dashed border-primary/40 bg-primary/[0.02] p-6 text-center transition-all hover:border-primary hover:bg-primary/[0.05]" | |
| 71 | + onClick={() => fileInput?.click()} | |
| 72 | + > | |
| 73 | + <input | |
| 74 | + ref={(el) => { | |
| 75 | + fileInput = el; | |
| 76 | + }} | |
| 77 | + type="file" | |
| 78 | + accept=".pdf,.txt,.md,text/plain,text/markdown,application/pdf" | |
| 79 | + class="hidden" | |
| 80 | + onChange={(e) => void handleFileSelect(e.currentTarget.files?.[0])} | |
| 81 | + /> | |
| 82 | + <span class="flex h-12 w-12 items-center justify-center rounded-2xl border border-primary/20 bg-primary/10 text-primary shadow-sm"> | |
| 83 | + <IconUploadCloud class="h-6 w-6" /> | |
| 84 | + </span> | |
| 85 | + <div class="space-y-1 max-w-sm mx-auto"> | |
| 86 | + <p class="text-sm font-semibold text-foreground"> | |
| 87 | + {t("notes.import")} (PDF, TXT, Markdown) | |
| 88 | + </p> | |
| 89 | + <p class="text-xs leading-normal text-muted-foreground"> | |
| 90 | + {t("notes.importHelp")} | |
| 91 | + </p> | |
| 92 | + </div> | |
| 93 | + </div> | |
| 94 | + </Show> | |
| 95 | + | |
| 96 | + <Show when={file()}> | |
| 97 | + <div class="space-y-4"> | |
| 98 | + <div class="flex items-center justify-between rounded-xl border border-border/80 bg-muted/30 px-4 py-3 text-xs"> | |
| 99 | + <div class="flex items-center gap-2.5 min-w-0"> | |
| 100 | + <IconFileText class="h-4 w-4 text-primary shrink-0" /> | |
| 101 | + <span class="truncate font-medium text-foreground">{file()?.name}</span> | |
| 102 | + <span class="text-muted-foreground shrink-0">({formatBytes(file()?.size ?? 0)})</span> | |
| 103 | + </div> | |
| 104 | + <Button | |
| 105 | + type="button" | |
| 106 | + variant="ghost" | |
| 107 | + size="sm" | |
| 108 | + class="h-7 text-xs rounded-lg" | |
| 109 | + onClick={() => { | |
| 110 | + setFile(null); | |
| 111 | + setImportedTitle(""); | |
| 112 | + setImportedMarkdown(""); | |
| 113 | + }} | |
| 114 | + > | |
| 115 | + {t("common.edit")} | |
| 116 | + </Button> | |
| 117 | + </div> | |
| 118 | + | |
| 119 | + <Show when={hasGarbled()}> | |
| 120 | + <p class="rounded-xl border border-amber-500/30 bg-amber-500/10 px-3.5 py-2.5 text-xs font-medium text-amber-700 dark:text-amber-300"> | |
| 121 | + ⚠️ Some content may be unreadable due to OCR/extraction issues | |
| 122 | + </p> | |
| 123 | + </Show> | |
| 124 | + | |
| 125 | + <div class="space-y-1.5 rounded-xl border border-border/80 bg-card p-4 shadow-sm"> | |
| 126 | + <Label for="import-title">{t("form.title")}</Label> | |
| 127 | + <Input | |
| 128 | + id="import-title" | |
| 129 | + class="h-10 rounded-lg bg-background/80" | |
| 130 | + value={importedTitle()} | |
| 131 | + maxlength={200} | |
| 132 | + onInput={(e) => setImportedTitle(e.currentTarget.value)} | |
| 133 | + /> | |
| 134 | + </div> | |
| 135 | + | |
| 136 | + <div class="space-y-1.5 rounded-xl border border-border/80 bg-card p-4 shadow-sm"> | |
| 137 | + <Label for="import-content">{t("form.content")} (Markdown)</Label> | |
| 138 | + <Textarea | |
| 139 | + id="import-content" | |
| 140 | + class="min-h-56 rounded-lg font-mono text-xs bg-background/80 leading-relaxed" | |
| 141 | + value={importedMarkdown()} | |
| 142 | + rows={10} | |
| 143 | + onInput={(e) => setImportedMarkdown(e.currentTarget.value)} | |
| 144 | + /> | |
| 145 | + </div> | |
| 146 | + | |
| 147 | + <div class="flex flex-wrap items-center justify-end gap-2 border-t border-border/80 pt-4"> | |
| 148 | + <Show when={props.onCancel}> | |
| 149 | + <Button type="button" variant="outline" class="h-10 rounded-lg" onClick={props.onCancel}> | |
| 150 | + {t("common.cancel")} | |
| 151 | + </Button> | |
| 152 | + </Show> | |
| 153 | + <Button | |
| 154 | + type="button" | |
| 155 | + class="h-10 rounded-lg px-5 font-semibold" | |
| 156 | + disabled={processing() || !importedMarkdown().trim()} | |
| 157 | + onClick={handleApply} | |
| 158 | + > | |
| 159 | + {t("common.create")} | |
| 160 | + </Button> | |
| 161 | + </div> | |
| 162 | + </div> | |
| 163 | + </Show> | |
| 164 | + | |
| 165 | + <Show when={error()}> | |
| 166 | + <p class="rounded-lg border border-destructive/20 bg-destructive/10 px-3 py-2 text-sm text-destructive"> | |
| 167 | + {error()} | |
| 168 | + </p> | |
| 169 | + </Show> | |
| 170 | + </div> | |
| 171 | + ); | |
| 172 | +} | |
| 173 | + | |
| 174 | +/** Extract text content from PDF file */ | |
| 175 | +async function extractPdfText(file: File): Promise<string> { | |
| 176 | + const arrayBuffer = await file.arrayBuffer(); | |
| 177 | + const bytes = new Uint8Array(arrayBuffer); | |
| 178 | + const decoder = new TextDecoder("utf-8"); | |
| 179 | + const rawString = decoder.decode(bytes); | |
| 180 | + | |
| 181 | + // Extract printable text streams from PDF brackets (Tj/TJ streams) or plain text blocks | |
| 182 | + const textMatches: string[] = []; | |
| 183 | + const textStreamRegex = /\(([^()]{2,})\)\s*T[jJ]/g; | |
| 184 | + let match: RegExpExecArray | null; | |
| 185 | + | |
| 186 | + while ((match = textStreamRegex.exec(rawString)) !== null) { | |
| 187 | + if (match[1]) { | |
| 188 | + textMatches.push(match[1]); | |
| 189 | + } | |
| 190 | + } | |
| 191 | + | |
| 192 | + if (textMatches.length > 0) { | |
| 193 | + return textMatches.join(" "); | |
| 194 | + } | |
| 195 | + | |
| 196 | + // Fallback: extract continuous ASCII/UTF8 character chunks | |
| 197 | + const asciiChunks = rawString.match(/[A-Za-z0-9çğıöşüÇĞİÖŞÜ \t\n.,:;!?(){}\[\]\-"'\/]{4,}/g) || []; | |
| 198 | + return asciiChunks.join("\n"); | |
| 199 | +} |
src/components/ui/icons.tsxmodified+10 -0
| @@ -613,3 +613,13 @@ export function IconSparkles(props: IconProps) { | ||
| 613 | 613 | </Svg> |
| 614 | 614 | ); |
| 615 | 615 | } |
| 616 | + | |
| 617 | +export function IconUploadCloud(props: IconProps) { | |
| 618 | + return ( | |
| 619 | + <Svg {...props}> | |
| 620 | + <path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242" /> | |
| 621 | + <path d="M12 12v9" /> | |
| 622 | + <path d="m16 16-4-4-4 4" /> | |
| 623 | + </Svg> | |
| 624 | + ); | |
| 625 | +} |
src/i18n/messages.tsmodified+6 -0
| @@ -226,6 +226,8 @@ export type MessageKey = | ||
| 226 | 226 | | "notes.files" |
| 227 | 227 | | "notes.filesHelp" |
| 228 | 228 | | "notes.addFile" |
| 229 | + | "notes.import" | |
| 230 | + | "notes.importHelp" | |
| 229 | 231 | | "notes.draw" |
| 230 | 232 | | "notes.drawTitle" |
| 231 | 233 | | "draw.pen" |
| @@ -956,6 +958,8 @@ const en: Dict = { | ||
| 956 | 958 | "notes.files": "Attachments", |
| 957 | 959 | "notes.filesHelp": "Up to 10 files. Max {size} each.", |
| 958 | 960 | "notes.addFile": "Add file", |
| 961 | + "notes.import": "Import", | |
| 962 | + "notes.importHelp": "Upload a PDF, TXT, or Markdown file. Line breaks, page numbers, and noise will be automatically cleaned into a structured note.", | |
| 959 | 963 | "notes.draw": "Draw", |
| 960 | 964 | "notes.drawTitle": "Draw a picture", |
| 961 | 965 | "draw.pen": "Pen", |
| @@ -1698,6 +1702,8 @@ const tr: Dict = { | ||
| 1698 | 1702 | "notes.files": "Ekler", |
| 1699 | 1703 | "notes.filesHelp": "En fazla 10 dosya. Dosya başına {size} sınırı.", |
| 1700 | 1704 | "notes.addFile": "Dosya ekle", |
| 1705 | + "notes.import": "İçe aktar", | |
| 1706 | + "notes.importHelp": "PDF, TXT veya Markdown dosyanızı yükleyin. Satır sonları, sayfa numaraları ve gürültüler temizlenip düzenli bir nota dönüştürülür.", | |
| 1701 | 1707 | "notes.draw": "Çiz", |
| 1702 | 1708 | "notes.drawTitle": "Resim çiz", |
| 1703 | 1709 | "draw.pen": "Kalem", |
src/lib/__tests__/note-importer.test.tsadded+69 -0
| @@ -0,0 +1,69 @@ | ||
| 1 | +import { describe, expect, it } from "vitest"; | |
| 2 | +import { processImportedText } from "../note-importer"; | |
| 3 | + | |
| 4 | +describe("processImportedText (Note Import Assistant)", () => { | |
| 5 | + it("removes page numbers, watermarks, and noise lines", () => { | |
| 6 | + const raw = ` | |
| 7 | +Header Title | |
| 8 | +Page 1 of 10 | |
| 9 | +Sayfa 1 | |
| 10 | + | |
| 11 | +This is the main body paragraph. | |
| 12 | +All rights reserved | |
| 13 | +- 1 - | |
| 14 | +Another paragraph of actual content. | |
| 15 | +`; | |
| 16 | + const result = processImportedText(raw, "lecture-notes.pdf"); | |
| 17 | + expect(result.markdown).not.toContain("Page 1 of 10"); | |
| 18 | + expect(result.markdown).not.toContain("Sayfa 1"); | |
| 19 | + expect(result.markdown).not.toContain("All rights reserved"); | |
| 20 | + expect(result.markdown).not.toContain("- 1 -"); | |
| 21 | + expect(result.markdown).toContain("This is the main body paragraph."); | |
| 22 | + expect(result.markdown).toContain("Another paragraph of actual content."); | |
| 23 | + }); | |
| 24 | + | |
| 25 | + it("joins lines that were split mid-sentence by PDF line wrapping", () => { | |
| 26 | + const raw = ` | |
| 27 | +This is a sentence that was broken | |
| 28 | +across multiple lines due to PDF extraction. | |
| 29 | + | |
| 30 | +Here is a hyphen- | |
| 31 | +ated word test. | |
| 32 | +`; | |
| 33 | + const result = processImportedText(raw, "document.pdf"); | |
| 34 | + expect(result.markdown).toContain("This is a sentence that was broken across multiple lines due to PDF extraction."); | |
| 35 | + expect(result.markdown).toContain("Here is a hyphenated word test."); | |
| 36 | + }); | |
| 37 | + | |
| 38 | + it("preserves Markdown code blocks, lists, and headers", () => { | |
| 39 | + const raw = ` | |
| 40 | +# Math Lecture 1 | |
| 41 | + | |
| 42 | +- First point | |
| 43 | +- Second point | |
| 44 | + | |
| 45 | +\`\`\`javascript | |
| 46 | +const x = 42; | |
| 47 | +\`\`\` | |
| 48 | +`; | |
| 49 | + const result = processImportedText(raw, "notes.md"); | |
| 50 | + expect(result.title).toBe("Math Lecture 1"); | |
| 51 | + expect(result.markdown).toContain("- First point"); | |
| 52 | + expect(result.markdown).toContain("const x = 42;"); | |
| 53 | + }); | |
| 54 | + | |
| 55 | + it("flags garbled / OCR noise content with warning banner", () => { | |
| 56 | + const garbledRaw = `\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD Scanned document content here`; | |
| 57 | + const result = processImportedText(garbledRaw, "scan.pdf"); | |
| 58 | + expect(result.hasGarbledWarning).toBe(true); | |
| 59 | + expect(result.markdown).toContain("⚠️ Some content may be unreadable due to OCR/extraction issues"); | |
| 60 | + }); | |
| 61 | + | |
| 62 | + it("derives clean title from heading or file name", () => { | |
| 63 | + const result1 = processImportedText("## Physics Mechanics\n\nContent here", "physics.pdf"); | |
| 64 | + expect(result1.title).toBe("Physics Mechanics"); | |
| 65 | + | |
| 66 | + const result2 = processImportedText("Just plain text without heading", "my_study_notes.txt"); | |
| 67 | + expect(result2.title).toBe("my study notes"); | |
| 68 | + }); | |
| 69 | +}); |
src/lib/note-importer.tsadded+215 -0
| @@ -0,0 +1,215 @@ | ||
| 1 | +export type ImportResult = { | |
| 2 | + title: string; | |
| 3 | + markdown: string; | |
| 4 | + hasGarbledWarning: boolean; | |
| 5 | +}; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Note Import Assistant | |
| 9 | + * Converts raw extracted text from PDF, TXT, or MD into a clean, well-structured note. | |
| 10 | + */ | |
| 11 | +export function processImportedText(rawText: string, fileName?: string): ImportResult { | |
| 12 | + if (!rawText || !rawText.trim()) { | |
| 13 | + const defaultTitle = deriveTitle(fileName, ""); | |
| 14 | + return { title: defaultTitle, markdown: "", hasGarbledWarning: false }; | |
| 15 | + } | |
| 16 | + | |
| 17 | + // 1. Check for garbled / OCR issues | |
| 18 | + const isGarbled = checkGarbledRatio(rawText); | |
| 19 | + | |
| 20 | + let text = rawText; | |
| 21 | + | |
| 22 | + // Normalize line endings | |
| 23 | + text = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n"); | |
| 24 | + | |
| 25 | + // 2. Remove noise: page numbers, headers/footers, watermarks | |
| 26 | + text = removeNoise(text); | |
| 27 | + | |
| 28 | + // 3. Fix broken line-wrapping from PDF extraction | |
| 29 | + text = fixPdfLineWrapping(text); | |
| 30 | + | |
| 31 | + // 4. Reformat into clean Markdown structure | |
| 32 | + text = formatMarkdownStructure(text); | |
| 33 | + | |
| 34 | + // 5. Prepend garbled warning if content has OCR issues | |
| 35 | + const warning = "⚠️ Some content may be unreadable due to OCR/extraction issues"; | |
| 36 | + if (isGarbled && !text.startsWith(warning)) { | |
| 37 | + text = `${warning}\n\n${text}`; | |
| 38 | + } | |
| 39 | + | |
| 40 | + const title = deriveTitle(fileName, text); | |
| 41 | + | |
| 42 | + return { | |
| 43 | + title, | |
| 44 | + markdown: text.trim(), | |
| 45 | + hasGarbledWarning: isGarbled, | |
| 46 | + }; | |
| 47 | +} | |
| 48 | + | |
| 49 | +/** Detect if content is heavily garbled (scanned OCR noise or binary extraction junk) */ | |
| 50 | +function checkGarbledRatio(text: string): boolean { | |
| 51 | + if (!text.trim()) return false; | |
| 52 | + // Count replacement characters, unprintable control characters, or repeated non-word noise | |
| 53 | + const replacementChars = (text.match(/\uFFFD/g) || []).length; | |
| 54 | + const nonAsciiControl = (text.match(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g) || []).length; | |
| 55 | + const total = text.length; | |
| 56 | + | |
| 57 | + if (total === 0) return false; | |
| 58 | + const ratio = (replacementChars + nonAsciiControl) / total; | |
| 59 | + return ratio > 0.05 || (replacementChars > 5 && ratio > 0.02); | |
| 60 | +} | |
| 61 | + | |
| 62 | +/** Remove page numbers, repeated headers/footers, and watermarks */ | |
| 63 | +function removeNoise(text: string): string { | |
| 64 | + const lines = text.split("\n"); | |
| 65 | + const cleaned: string[] = []; | |
| 66 | + | |
| 67 | + const noisePatterns = [ | |
| 68 | + /^\s*(page|sayfa)\s+\d+\s*(of|\/|-)?\s*\d*\s*$/i, | |
| 69 | + /^\s*-\s*\d+\s*-\s*$/, | |
| 70 | + /^\s*\d+\s*\/\s*\d+\s*$/, | |
| 71 | + /^\s*(confidential|gizli|draft|taslak|all rights reserved|tüm hakları saklıdır)\s*$/i, | |
| 72 | + /^\s*downloaded from .*\s*$/i, | |
| 73 | + /^\s*watermark\s*$/i, | |
| 74 | + ]; | |
| 75 | + | |
| 76 | + for (const line of lines) { | |
| 77 | + const trimmed = line.trim(); | |
| 78 | + if (!trimmed) { | |
| 79 | + cleaned.push(""); | |
| 80 | + continue; | |
| 81 | + } | |
| 82 | + | |
| 83 | + const isNoise = noisePatterns.some((pattern) => pattern.test(trimmed)); | |
| 84 | + if (!isNoise) { | |
| 85 | + cleaned.push(line); | |
| 86 | + } | |
| 87 | + } | |
| 88 | + | |
| 89 | + return cleaned.join("\n"); | |
| 90 | +} | |
| 91 | + | |
| 92 | +/** Join lines that were split mid-sentence by PDF line wrapping */ | |
| 93 | +function fixPdfLineWrapping(text: string): string { | |
| 94 | + const lines = text.split("\n"); | |
| 95 | + const result: string[] = []; | |
| 96 | + | |
| 97 | + for (let i = 0; i < lines.length; i += 1) { | |
| 98 | + let current = lines[i]; | |
| 99 | + | |
| 100 | + // If current line ends with a hyphen and next line starts with a letter, join hyphenated word | |
| 101 | + if (i < lines.length - 1 && /[-–]\s*$/.test(current) && /^[a-zçğıöşü]/i.test(lines[i + 1].trim())) { | |
| 102 | + const nextLine = lines[i + 1].trim(); | |
| 103 | + current = current.replace(/[-–]\s*$/, "") + nextLine; | |
| 104 | + i += 1; | |
| 105 | + } | |
| 106 | + // If line ends mid-sentence without terminal punctuation (. : ! ? # - | `) and next line starts with lowercase or continuation | |
| 107 | + else if ( | |
| 108 | + i < lines.length - 1 && | |
| 109 | + current.trim().length > 0 && | |
| 110 | + lines[i + 1].trim().length > 0 && | |
| 111 | + !/[.:!?#\-|`]\s*$/.test(current.trim()) && | |
| 112 | + !/^(#+|\*+|-+|\d+\.)\s/.test(lines[i + 1].trim()) && | |
| 113 | + /^[a-zçğıöşü,\(]/i.test(lines[i + 1].trim()) | |
| 114 | + ) { | |
| 115 | + current = `${current.trim()} ${lines[i + 1].trim()}`; | |
| 116 | + i += 1; | |
| 117 | + } | |
| 118 | + | |
| 119 | + result.push(current); | |
| 120 | + } | |
| 121 | + | |
| 122 | + return result.join("\n"); | |
| 123 | +} | |
| 124 | + | |
| 125 | +/** Reformat paragraphs, headers, and bullet points into clean Markdown */ | |
| 126 | +function formatMarkdownStructure(text: string): string { | |
| 127 | + const lines = text.split("\n"); | |
| 128 | + const formatted: string[] = []; | |
| 129 | + let inCodeBlock = false; | |
| 130 | + | |
| 131 | + for (let i = 0; i < lines.length; i += 1) { | |
| 132 | + const line = lines[i]; | |
| 133 | + const trimmed = line.trim(); | |
| 134 | + | |
| 135 | + if (trimmed.startsWith("```")) { | |
| 136 | + inCodeBlock = !inCodeBlock; | |
| 137 | + formatted.push(line); | |
| 138 | + continue; | |
| 139 | + } | |
| 140 | + | |
| 141 | + if (inCodeBlock) { | |
| 142 | + formatted.push(line); | |
| 143 | + continue; | |
| 144 | + } | |
| 145 | + | |
| 146 | + if (!trimmed) { | |
| 147 | + if (formatted.length > 0 && formatted[formatted.length - 1] !== "") { | |
| 148 | + formatted.push(""); | |
| 149 | + } | |
| 150 | + continue; | |
| 151 | + } | |
| 152 | + | |
| 153 | + // Preserve existing headers, quotes, lists, or tables | |
| 154 | + if ( | |
| 155 | + trimmed.startsWith("#") || | |
| 156 | + trimmed.startsWith(">") || | |
| 157 | + trimmed.startsWith("- ") || | |
| 158 | + trimmed.startsWith("* ") || | |
| 159 | + /^\d+\.\s/.test(trimmed) || | |
| 160 | + trimmed.startsWith("|") | |
| 161 | + ) { | |
| 162 | + formatted.push(trimmed); | |
| 163 | + continue; | |
| 164 | + } | |
| 165 | + | |
| 166 | + // Detect potential section headers (short uppercase/title case lines without trailing punctuation) | |
| 167 | + const isShortHeader = | |
| 168 | + trimmed.length < 60 && | |
| 169 | + !/[.,:!?]$/.test(trimmed) && | |
| 170 | + (trimmed === trimmed.toUpperCase() || /^[A-ZÇĞİÖŞÜ0-9\s:-]+$/.test(trimmed)); | |
| 171 | + | |
| 172 | + if (isShortHeader && trimmed.length > 3) { | |
| 173 | + formatted.push(`## ${toTitleCase(trimmed)}`); | |
| 174 | + continue; | |
| 175 | + } | |
| 176 | + | |
| 177 | + formatted.push(trimmed); | |
| 178 | + } | |
| 179 | + | |
| 180 | + // Deduplicate consecutive blank lines | |
| 181 | + return formatted | |
| 182 | + .join("\n") | |
| 183 | + .replace(/\n{3,}/g, "\n\n"); | |
| 184 | +} | |
| 185 | + | |
| 186 | +function toTitleCase(str: string): string { | |
| 187 | + if (str === str.toUpperCase() && str.length > 4) { | |
| 188 | + return str | |
| 189 | + .toLowerCase() | |
| 190 | + .replace(/(?:^|\s|-)\S/g, (m) => m.toUpperCase()); | |
| 191 | + } | |
| 192 | + return str; | |
| 193 | +} | |
| 194 | + | |
| 195 | +/** Extract title from Markdown heading or fallback to file name */ | |
| 196 | +function deriveTitle(fileName?: string, markdownText?: string): string { | |
| 197 | + if (markdownText) { | |
| 198 | + const headingMatch = markdownText.match(/^#+\s*(.+)$/m); | |
| 199 | + if (headingMatch && headingMatch[1].trim()) { | |
| 200 | + const heading = headingMatch[1].replace(/^[⚠️\s]+/, "").trim(); | |
| 201 | + if (heading.length > 0 && heading.length <= 100) { | |
| 202 | + return heading; | |
| 203 | + } | |
| 204 | + } | |
| 205 | + } | |
| 206 | + | |
| 207 | + if (fileName) { | |
| 208 | + const cleanName = fileName.replace(/\.[^/.]+$/, "").replace(/[-_]/g, " ").trim(); | |
| 209 | + if (cleanName) { | |
| 210 | + return cleanName; | |
| 211 | + } | |
| 212 | + } | |
| 213 | + | |
| 214 | + return "Imported Note"; | |
| 215 | +} |
src/pages/notes-page.tsxmodified+33 -5
| @@ -9,9 +9,10 @@ import { Alert } from "@/components/ui/alert"; | ||
| 9 | 9 | import { RouteGuard } from "@/components/layout/route-guard"; |
| 10 | 10 | import { PageHeader } from "@/components/layout/page-header"; |
| 11 | 11 | import { NoteForm } from "@/components/notes/note-form"; |
| 12 | +import { NoteImportPanel } from "@/components/notes/note-import-panel"; | |
| 12 | 13 | import { NoteList } from "@/components/notes/note-list"; |
| 13 | 14 | import { Button } from "@/components/ui/button"; |
| 14 | -import { IconPlus } from "@/components/ui/icons"; | |
| 15 | +import { IconPlus, IconUploadCloud } from "@/components/ui/icons"; | |
| 15 | 16 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 16 | 17 | import { PaginationControls } from "@/components/ui/pagination-controls"; |
| 17 | 18 | import { SidePanel } from "@/components/ui/side-panel"; |
| @@ -34,6 +35,7 @@ function NotesContent() { | ||
| 34 | 35 | const [error, setError] = createSignal(""); |
| 35 | 36 | const [flash, setFlash] = createFlash(); |
| 36 | 37 | const [createOpen, setCreateOpen] = createSignal(false); |
| 38 | + const [importOpen, setImportOpen] = createSignal(false); | |
| 37 | 39 | const [page, setPage] = createSignal(0); |
| 38 | 40 | |
| 39 | 41 | const [list, { refetch }] = createResource( |
| @@ -72,14 +74,40 @@ function NotesContent() { | ||
| 72 | 74 | title={t("notes.title")} |
| 73 | 75 | description={t("notes.subtitle")} |
| 74 | 76 | actions={ |
| 75 | - <Button type="button" size="sm" class="min-w-[7.5rem] rounded-lg" onClick={() => setCreateOpen(true)}> | |
| 76 | - <IconPlus class="h-4 w-4" /> | |
| 77 | - {t("notes.new")} | |
| 78 | - </Button> | |
| 77 | + <div class="flex items-center gap-2"> | |
| 78 | + <Button type="button" size="sm" class="min-w-[7.5rem] rounded-lg" onClick={() => setCreateOpen(true)}> | |
| 79 | + <IconPlus class="h-4 w-4" /> | |
| 80 | + {t("notes.new")} | |
| 81 | + </Button> | |
| 82 | + <Button type="button" variant="outline" size="sm" class="rounded-lg" onClick={() => setImportOpen(true)}> | |
| 83 | + <IconUploadCloud class="h-4 w-4" /> | |
| 84 | + İçe Aktar | |
| 85 | + </Button> | |
| 86 | + </div> | |
| 79 | 87 | } |
| 80 | 88 | /> |
| 81 | 89 | </div> |
| 82 | 90 | |
| 91 | + <SidePanel open={importOpen()} onOpenChange={setImportOpen} title="İçe Aktar"> | |
| 92 | + <NoteImportPanel | |
| 93 | + onCancel={() => setImportOpen(false)} | |
| 94 | + onImport={async (importedTitle, importedMarkdown) => { | |
| 95 | + setError(""); | |
| 96 | + try { | |
| 97 | + await postNote({ | |
| 98 | + title: importedTitle, | |
| 99 | + content: importedMarkdown || undefined, | |
| 100 | + }); | |
| 101 | + await refetch(); | |
| 102 | + setImportOpen(false); | |
| 103 | + setFlash(t("common.created")); | |
| 104 | + } catch (err) { | |
| 105 | + setError(formatApiError(err)); | |
| 106 | + } | |
| 107 | + }} | |
| 108 | + /> | |
| 109 | + </SidePanel> | |
| 110 | + | |
| 83 | 111 | <SidePanel open={createOpen()} onOpenChange={setCreateOpen} title={t("notes.new")}> |
| 84 | 112 | <NoteForm |
| 85 | 113 | enableFiles |