Skip to main content

plain_text_to_djot

Function plain_text_to_djot 

Source
pub fn plain_text_to_djot(s: &str) -> String
Expand description

Convert a whole plain-text string into Djot that parses back to exactly that text.

Escapes inline markup everywhere and guards each line’s own start, since a line beginning - is a list item wherever it sits in the string, not only in the first.

§Each line becomes its own paragraph, and that is forced, not chosen

A single newline inside a Djot paragraph is a soft break, and djot_to_plain_text collapses it to a space — so emitting the lines as one paragraph loses every line ending. Blocks, meanwhile, are joined by exactly one \n when read back. One paragraph per line is therefore the only shape whose round trip is the identity, and it is also what the text it will meet already means: a .docx/.odt comment body is assembled by joining its paragraphs with \n, so each newline in such a string is a paragraph boundary.

§The contract, stated exactly

djot_to_plain_text(plain_text_to_djot(s)) == s for every s that contains no blank line — i.e. no two consecutive newlines, and no leading or trailing one.

That restriction is not a gap left open; it is the shape of the target. djot_to_plain_text never emits two consecutive newlines, because blocks are joined by exactly one — so no string containing a blank line is in the image of the parse, and none can be recovered by any encoding. Blank lines in the input collapse, which for the paragraph-joined text this serves is a no-op. Use needs_djot_escaping to find values a conversion would alter.