Escape Djot-significant characters when converting HTML text to Djot#202
Merged
Conversation
HtmlToDjot emitted text verbatim, so HTML originating outside Djot
(WYSIWYG editors, CMS exports, pasted content) silently changed meaning
on the next parse: a literal `*b*` became emphasis, a leading `- x`
became a list item, `[text](url)` became a link.
Escape inline markers (backslash, backtick, `*`, `[`, `{`, `~`, `^`, `<`)
anywhere, and block markers (leading `-`, `+`, `#`, `>` and ordered
`1.` / `1)`) at line start, walking up through inline wrappers so
`<span>- x</span>` is handled too. Intraword underscores stay literal to
keep identifiers, filenames and URLs readable; asterisks and sub/super
markers keep escaping since they are significant intraword. Verbatim
contexts (pre/code) are never escaped.
Also fix processLink double-escaping a label built from already-escaped
child text; it now only escapes the label-closing bracket there.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #202 +/- ##
============================================
+ Coverage 91.78% 91.80% +0.01%
- Complexity 3441 3452 +11
============================================
Files 104 104
Lines 9753 9788 +35
============================================
+ Hits 8952 8986 +34
- Misses 801 802 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This was referenced Jun 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
HtmlToDjotemitted text-node content verbatim (only normalizing whitespace). When the HTML originates outside Djot - WYSIWYG editors, CMS exports, pasted content, web scraping, the documented primary use case - that text routinely contains characters that are Djot markup. On the next parse they are re-interpreted as markup and the document silently changes meaning.Concrete round-trips that were broken (
HTML -> Djot -> HTML):A paragraph turning into a list or an emphasis appearing out of nowhere is a correctness bug, not cosmetic normalization.
Fix
Escape Djot-significant characters when emitting HTML text as Djot, in
escapeDjotText():\`*[{~^<. These can open inline constructs (emphasis, code, links/spans, sub/superscript, autolinks, escapes).-+#>and ordered-list markers1./1). This is what keeps a paragraph from becoming a list, heading or blockquote. A leading dash run also no longer becomes a thematic break.snake_case,SCREAMING_CASE,file_namestay literal. Djot only treats_as emphasis at word boundaries, so escaping intraword_is unnecessary noise - and import output is full of identifiers, filenames and URLs. Asterisks get no exemption (Djot emphasis works intraword);~/^get none either (H~2~Ois a real subscript).processLinkbuilt its label from already-escaped child text and then ran the label escaper again, doubling backslashes. It now only escapes the label-closing]on child-derived text; a rawhrefused as the label still gets full escaping.Verbatim contexts are untouched: text inside
<pre>/code blocks is never escaped.Scope and non-goals
--/---becoming en/em dashes. A leading dash run is still prevented from becoming a thematic break (the structural risk); only the inner dashes get smart-punctuation, exactly like smart quotes.Examples after the fix