notes: markdown export no longer trims the first block's indentation away
- Commit
c4963498655aed00bc10724634c199c065d0f2ee- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 11:28 UTC (5 days ago)
- Parents
d793f93
exportNoteMarkdown ended with joinToString(...).trim() despite its own doc that text blocks are emitted verbatim. trim() on the whole document stripped the first block's leading indentation: an indented code block (four leading spaces) exported as a plain paragraph, and a round-trip changed the note's meaning. Trailing whitespace of the last block was trimmed the same way. The joined blocks are now emitted verbatim, guaranteeing only a final newline; blank-block filtering and the block join are unchanged, and an empty note still exports just a newline. Regression test covers both the imported body path and the block path (failed pre-fix on exactly this mismatch). Block operations, image dedupe and list rendering were audited and cleared. Verified: :notes:testDevUnitTest 253 tests and :notes:lint BUILD SUCCESSFUL.
Diff
notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.kt b/notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.ktindex 5a3d98ffbd..2b35635693 100644--- a/notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.kt+++ b/notes/src/main/java/com/vayunmathur/notes/platform/NoteMarkdownExporter.kt@@ -38,8 +38,8 @@ private const val UTF8_CONTINUATION_BITS = 0x80 * Export-only: the stored JSON blocks stay the source of truth; this never * touches storage. Reads image files, so call it off the main thread. */-fun exportNoteMarkdown(context: Context, note: Note): String =- note.body().blocks+fun exportNoteMarkdown(context: Context, note: Note): String {+ val joined = note.body().blocks .map { block -> when (block) { is NoteBlock.Text -> block.markdown@@ -49,7 +49,12 @@ fun exportNoteMarkdown(context: Context, note: Note): String = } .filter { it.isNotBlank() } .joinToString("\n\n")- .trim() + "\n"+ // Verbatim, not trimmed: trimming the joined document strips the leading+ // indentation of a first indented code block (and any intentional leading+ // blank lines), turning code into a plain paragraph on a round trip. Only+ // guarantee the file ends with a newline.+ return if (joined.endsWith("\n")) joined else "$joined\n"+} /** * Writes [markdown] to `cacheDir/shared_notes/<title>.md` and returns a shareable
notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt
diff --git a/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt b/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.ktindex 7849da0f28..37db93cbe0 100644--- a/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt+++ b/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt@@ -121,6 +121,20 @@ class NoteStoresTest { assertTrue(svg.contains("height=\"222\""), svg) } + @Test fun exportPreservesLeadingIndentedCodeBlock() {+ // Regression: text blocks are documented as emitted verbatim, but the+ // export trimmed the whole document. An imported markdown file whose+ // first line is an indented code block (four leading spaces) came back+ // out as a plain paragraph, losing the code block.+ val markdown = " val x = 1\n val y = 2"+ val imported = Note(title = "code", content = markdown)+ assertEquals("$markdown\n", exportNoteMarkdown(context, imported))+ val blocked = Note(title = "code", content = "").withBody(+ NoteBody(listOf(NoteBlock.Text(markdown, id = "t1"))),+ )+ assertEquals("$markdown\n", exportNoteMarkdown(context, blocked))+ }+ @Test fun exportInlinesRealImageWithWidthPercent() { writeImageFile("real.jpg", 60, 30) val note = Note(title = "Img", content = "").withBody(NoteBody(listOf(