Skip to content

fix(export): keep backslash escapes at the start of a line - #13721

Closed
yuxi-liu-wired wants to merge 5 commits into
logseq:masterfrom
yuxi-liu-wired:fix/export-keep-markdown-escapes
Closed

yuxi-liu-wired wants to merge 5 commits into
logseq:masterfrom
yuxi-liu-wired:fix/export-keep-markdown-escapes

Conversation

@yuxi-liu-wired

@yuxi-liu-wired yuxi-liu-wired commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

A block can keep Markdown syntax as plain text with a backslash, for example \# not a heading or a second line \- not a list item, and it displays as written. Exporting it as text and pasting the text back turns it into what the backslash was there to prevent: a heading, a child block, a quote.

Cause

The text export (frontend.handler.export.text-impl) parses the block's Markdown with mldoc and prints the result. mldoc reads \# x as the plain text # x, and the export prints that plain text as is, so the backslash is gone from the exported text. Paste then reads # x as a heading.

Fix

Only what the DB graph's mldoc reads as structure is escaped. DB graphs parse with parse_marker false, so a task word (TODO, DONE, ...) at the start of a line is plain text and gets no backslash; a lone > is plain text too.

  • A backslash the block's text has at the start of a line is doubled before parsing (keep-line-start-backslashes in logseq.common.export.file), so mldoc reads it as one literal backslash and the export keeps it: \# x, \TODO x and \- x come back as typed.
  • When the export prints plain text at the start of a line that would be read as structure there, it writes a backslash: on the first line a heading (#) or a quote (> followed by text); on a later line also a list item (-, +, 1.).
  • A later line of a block that starts with - , + , # or > text without a backslash is shown by the block as text (the block's title is rendered as - <title>, so such a line is not a list), but the export wrote it as a child block, a list, a heading or a quote. The text export now writes a backslash before such a line (outside code fences). * lines need none: the export writes such a list back as * .
  • A block whose text starts with > (not a quote block) is written as \> text, so it does not come back as a quote block. The DB version has two forms of quote here, see DB: two forms of quote break export/paste round-trip (quote block vs. text starting with "> ") db-test#1439.

All of this is set only by the text export's content config (:escape-block-markers?); the Markdown mirror is left as it is.

Test

frontend.handler.export-escape-test exports blocks with escaped headings, task words, list items and quotes (on the first and on later lines), pastes the text back, and checks each block comes back with the same text and no heading. Blocks that need no escape come back unchanged: a numbered first line, a plain block starting with TODO, a later line starting with TODO, a lone >, a * list inside the block, a - line in a code fence, two backslashes. Blocks with an unescaped later - , + , # or > text line come back as one block with that line escaped. On master 13 assertions fail; with the fix all 257 export, paste and Markdown mirror assertions pass.

mldoc parses \# x as the text # x; the text export printed that text without the backslash, so pasting the export back turned it into a heading, a list item or a quote.
…r heading

A later line of a block starting with "- " or "#" is shown by the block as text, but the text export wrote it as a child block or heading. The text export (not the Markdown mirror) now writes a backslash before it.
The text export wrote both as "> text". A plain block starting with > is now written as "\> text". See #13733 for which form of quote is intended.
@cursor

cursor Bot commented Oct 13, 2026

Copy link
Copy Markdown

escape-plain-start compiles #"(?s)([ \t]*)(.*)". (?s) is a Java inline flag, and this namespace runs as JavaScript. new RegExp("(?s)([ \\t]*)(.*)") throws Invalid regular expression: /(?s)([ \t]*)(.*)/: Invalid group. A normal list item is a Paragraph, so text export reaches this regex and the export throws. Drop the flag and anchor the pattern, for example #"^([ \t]*)(.*)".

After that regex is valid, line-start-markup still prefixes \ onto any later plain line that looks like a heading, list marker, quote, numbered marker, or task word. A stored title such as 1. numbered text is already plain text. escape-paragraph-line-starts runs line-start-markup on the paragraph's first inline, and that pattern includes \d+[.)]. The new round-trip test expects that title back unchanged, so the assertion fails once the regex compiles. The same happens to a continuation line TODO x or a line that is only >. DB mldoc is configured with parse_marker false, so those lines are not structure. Restore a backslash only when the source markdown still contained one.

node -e 'new RegExp("(?s)([ \\t]*)(.*)")' is enough to see the throw. bb dev:test -v frontend.handler.export-escape-test/escaped-markdown-round-trips covers the round trip after the pattern is valid.

DB graphs parse with parse_marker false, so a task word at a line start is text and gets no backslash (a plain block 'TODO x' came back as '\TODO x'); a lone '>' is text too, and '* ' lines are written back as '* '. A '+ ' list line and a '> text' line on a later line are escaped. A backslash the text itself has at a line start is doubled before parsing, so mldoc keeps it.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 13, 2026
A plain block whose text starts with a task marker, a heading marker or >, or has a later line starting with - or #, is written with a backslash, as the text export does (logseq#13721). A block with a status or heading is written as before.
@yuxi-liu-wired

yuxi-liu-wired commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Closing: this patches one symptom of a design problem, logseq/db-test#1442 (a block's body is a Markdown string sharing one grammar with every text format). The bug is listed there; the fix is the design, not this patch.

@yuxi-liu-wired
yuxi-liu-wired deleted the fix/export-keep-markdown-escapes branch October 21, 2026 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant