RichTextMarkdown.h header

#include <ew/app/RichTextMarkdown.h>

Namespace ew::app

Functions

QString ew::app::document::escapeMarkdownBlockLeader(const QString &text)

Backslash-escapes a leading Markdown block marker on text so a plain paragraph is not re-read as a different block on export. When prose happens to start with a list bullet (- , + , * ), an ordered marker (1. / 1) ), a blockquote (> ), an ATX heading (# .. ###### ), or forms a thematic break (a whole line of three or more -, *, or _), the marker is escaped (e.g. - 20 rounds -> \- 20 rounds, 1. First -> 1\. First, --- -> \---) so it renders as the literal characters. Each list/quote/heading marker must be followed by whitespace to count, so an emphasis run that opens the line (*word*, **bold**) – whose marker hugs non-whitespace – is left untouched. text with no leading block marker is returned unchanged. Intended for the plain-paragraph case of rich-text export, after inline emphasis has been applied (a literal */ ` is already escaped by then, so a bullet * never reaches here as a bare marker).

QString ew::app::document::plainTextAsMarkdown(const QString &text)

text – prose from a source that is NOT Markdown (a .txt manuscript, a yWriter scene, RTF, a Word paragraph) – prepared to be stored as a Markdown body, by escaping the block leader of every line (see escapeMarkdownBlockLeader). Without this the storage format silently reinterprets ordinary prose: "1918. The year everything changed." becomes a numbered list item and the year is gone from the book, "- 20 rounds were left" loses its dash, and "# 3 was the number on the door." becomes a chapter heading.

Only block leaders are escaped. An inline *emphasis* is left alone deliberately: marking emphasis with asterisks is how plain-text manuscripts have always been written, so a writer importing one means it.

QString ew::app::document::wrapInlineMarkdown(const QString &text, bool bold, bool italic, bool strikeOut)

Wraps text in the inline Markdown for the given emphases – bold (**), italic (*), both (***), and/or strikethrough (~~) – for the desktop editor's rich-text export. Two fidelity rules the naive "marks + text + marks" approach gets wrong:

  • Markers must hug non-whitespace. Any leading/trailing whitespace in text is lifted outside the markers, because CommonMark's flanking rule renders **word ** (a space before the closing **) as the literal characters, not bold. A trailing space inside a run is common: extending a selection to the next word (Shift+Ctrl+Right) includes the space.
  • Literal emphasis characters are escaped. A real * or ` the writer typed as prose is backslash-escaped so it is not reinterpreted as formatting on re-parse. Square brackets are intentionally left untouched so the app's ![[…]] / [^id] markers survive verbatim. With no emphasis set, returns text with only that escaping applied; empty for empty text; an all-whitespace text is returned unchanged (nothing to emphasize).