Skip to content

Commit 8ef0dfc

Browse files
committed
refactor: treat the first Markdown line as content
1 parent 532816e commit 8ef0dfc

3 files changed

Lines changed: 6 additions & 46 deletions

File tree

‎EdgeMark/Core/Editor/MarkdownEditorView.swift‎

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct EdgeMarkImageProvider: EmbeddedImageProvider {
2929
// MARK: - MarkdownEditorView
3030

3131
/// SwiftUI wrapper around NativeTextViewWrapper (swift-markdown-engine).
32-
/// Manages heading stripping, save debouncing, font observation, and the
32+
/// Manages full-document editing, save debouncing, font observation, and the
3333
/// slash-command popup.
3434
struct MarkdownEditorView: View {
3535
private struct EditorRebuildIdentity: Hashable {
@@ -55,7 +55,6 @@ struct MarkdownEditorView: View {
5555
var onNavigatePrevious: (() -> Void)?
5656

5757
@State private var text: String
58-
@State private var hiddenHeadingLine: String
5958
@State private var saveDebouncer = Debouncer(delay: 1.0)
6059
@State private var slashHandler = SlashCommandHandler()
6160
@State private var noteNavMonitor: Any?
@@ -91,9 +90,7 @@ struct MarkdownEditorView: View {
9190
self.showFindBar = showFindBar
9291
self.onNavigateNext = onNavigateNext
9392
self.onNavigatePrevious = onNavigatePrevious
94-
let (heading, body) = Self.splitHeading(initialContent)
95-
_text = State(initialValue: Self.imagesToEmbeds(body))
96-
_hiddenHeadingLine = State(initialValue: heading)
93+
_text = State(initialValue: Self.imagesToEmbeds(initialContent))
9794
_stableNoteID = State(initialValue: noteID)
9895
}
9996

@@ -177,39 +174,20 @@ struct MarkdownEditorView: View {
177174
))
178175
.onChange(of: text) { _, newText in
179176
slashHandler.contentDidSynchronize(newText)
180-
let heading = hiddenHeadingLine
181177
let noteIDSnapshot = stableNoteID
182178
saveDebouncer.call { [onContentChanged] in
183179
// Convert display-layer ![[path]] embeds back to on-disk ![]( path) before saving.
184180
let storage = Self.embedsToImages(newText)
185-
let full = heading.isEmpty ? storage : heading + "\n\n" + storage
186-
onContentChanged(noteIDSnapshot, full)
181+
onContentChanged(noteIDSnapshot, storage)
187182
}
188183
}
189184
.onChange(of: pendingReload) { _, newContent in
190185
guard let newContent else { return }
191186
slashHandler.dismiss()
192187
saveDebouncer.cancel()
193-
let (heading, body) = Self.splitHeading(newContent)
194-
hiddenHeadingLine = heading
195-
text = Self.imagesToEmbeds(body)
188+
text = Self.imagesToEmbeds(newContent)
196189
pendingReload = nil
197190
}
198-
.onChange(of: noteTitle) { _, _ in
199-
// A rename performed from the sticky title updates NoteStore's full
200-
// content while this editor remains mounted. Keep the editor's hidden
201-
// heading snapshot in sync so a pending debounce or onDisappear flush
202-
// cannot write the previous title back into the note.
203-
guard noteID == stableNoteID else { return }
204-
slashHandler.dismiss()
205-
saveDebouncer.cancel()
206-
let (heading, body) = Self.splitHeading(initialContent)
207-
hiddenHeadingLine = heading
208-
let displayBody = Self.imagesToEmbeds(body)
209-
if text != displayBody {
210-
text = displayBody
211-
}
212-
}
213191
.overlay(
214192
ImageDropOverlay { [noteID, noteTitle, noteFolder] url in
215193
guard let data = try? Data(contentsOf: url) else { return }
@@ -278,8 +256,7 @@ struct MarkdownEditorView: View {
278256
let capturedID = stableNoteID
279257
saveDebouncer.cancel()
280258
let storage = Self.embedsToImages(text)
281-
let full = hiddenHeadingLine.isEmpty ? storage : hiddenHeadingLine + "\n\n" + storage
282-
onContentChanged(capturedID, full)
259+
onContentChanged(capturedID, storage)
283260
slashHandler.dismiss()
284261
if let m = noteNavMonitor {
285262
NSEvent.removeMonitor(m); noteNavMonitor = nil
@@ -289,16 +266,6 @@ struct MarkdownEditorView: View {
289266

290267
// MARK: - Helpers
291268

292-
static func splitHeading(_ content: String) -> (heading: String, body: String) {
293-
let lines = content.components(separatedBy: "\n")
294-
guard let first = lines.first, first.hasPrefix("#") else { return ("", content) }
295-
var rest = Array(lines.dropFirst())
296-
while rest.first == "" {
297-
rest.removeFirst()
298-
}
299-
return (first, rest.joined(separator: "\n"))
300-
}
301-
302269
private static func resolvedFontFamily(from postscriptName: String?) -> String? {
303270
guard let name = postscriptName, let font = NSFont(name: name, size: 16) else { return nil }
304271
return font.familyName

‎EdgeMark/Core/Storage/Note.swift‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ extension Note {
9292
/// otherwise pin the main thread in `String.index` / `String.distance` for seconds
9393
/// when the note list renders (e.g. at launch, when the panel's view tree is built
9494
/// eagerly even while hidden). See devlog-0721, issue #56. 4 000 graphemes is far
95-
/// beyond any real title line, so dropping the first line (the H1 heading) still
96-
/// works; the 120-char preview output is unchanged.
95+
/// beyond any ordinary preview source; the 120-char preview output is unchanged.
9796
var previewText: String {
9897
let head = content.prefix(4000)
9998
// Surface pathological first lines so a reported blank or truncated subtitle

‎EdgeMark/Core/Storage/NoteStore.swift‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,10 +1786,4 @@ final class NoteStore {
17861786
try? SidecarStore.shared.save()
17871787
}
17881788
}
1789-
1790-
private static func extractTitle(from content: String) -> String {
1791-
let firstLine = content.split(separator: "\n", maxSplits: 1).first.map(String.init) ?? ""
1792-
let stripped = firstLine.drop { $0 == "#" || $0 == " " }
1793-
return stripped.isEmpty ? "Untitled" : String(stripped)
1794-
}
17951789
}

0 commit comments

Comments
 (0)