Skip to content
desktop-appPublic

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

9 Commits

Folders and files

Repository files navigation

tmarkdown

The Markdown parser of cmark-gfm in safe Rust, for Telegram Desktop.

For every input, tmarkdown builds the tree cmark-gfm builds: the same nodes, the same source positions, the same literals, URLs, list, table and task list data, and the same footnote links. That includes cmark-gfm's quirks. The engine crate forbids unsafe. The C interface crate holds the only unsafe code, and only what a C interface needs.

tmarkdown is a port of cmark-gfm at the commit desktop-app/cmark-gfm pins, which is the oracle it is checked against. It carries cmark-gfm's tables (HTML entities, Unicode case folding), so cmark-gfm's license notice is kept in NOTICE.

What it covers

The block and inline parsers, reference and footnote resolution, and the core extensions as cmark-gfm attaches them: table, strikethrough, autolink, tagfilter and tasklist. The options that change the tree are supported: SOURCEPOS, FOOTNOTES, STRIKETHROUGH_DOUBLE_TILDE, SMART, LIBERAL_HTML_TAG and VALIDATE_UTF8. Rendering is left to the host: Telegram Desktop converts the tree to its own document model.

Layout

  • engine/: the tmarkdown crate. blocks.rs and inlines.rs follow blocks.c and inlines.c function by function, with the extensions' hooks in place; scanners.rs is a hand port of the re2c scanners.
  • ffi/: the tmarkdown-c crate, the C interface in include/tmarkdown.h. A parse returns the tree as a flat array of nodes in preorder, linked by indexes, and one byte array its strings point into.
  • oracle/: cmark-gfm as a submodule and small C programs around it: cmark_oracle prints every node attribute its API exposes, cmark_util_oracle calls the UTF-8 and entity functions, and tmarkdown_abi_check prints the same trees through tmarkdown.h.
  • compare/: tmarkdown_compare prints the engine's trees in the oracle's format, and the tests replay the corpora against their goldens.
  • corpus/: the corpora, with the oracle's digests in corpus/golden.
  • tools/: corpus generators, the runners that compare against the oracle, and the speed and memory comparisons.

Checks

cargo test replays every committed corpus against the oracle's goldens, without the oracle:

  • spec.mdc: every example of the GFM spec and of cmark-gfm's own test files, in three configurations: Telegram Desktop's options and extensions, every option that changes the tree with every extension, and no extensions.
  • pathological.mdc: deep nesting, long delimiter runs, unclosed constructs, wide tables, many references and footnotes, at 10 to 10000 repetitions.
  • fuzz.mdc: 20000 random documents built from every construct, mutated and with mixed line endings, NUL bytes and invalid UTF-8, in the first two configurations.
  • scans.mdc and utils.mdc: every scanner, and the UTF-8, case folding and entity functions, with exhaustive sweeps.

With the oracle built (cmake -S oracle -B out/oracle and cmake --build out/oracle --config Release), tools/compare_run.py compares full trees on any corpus, tools/corpus_fuzz.py --seed N makes new random campaigns, and tools/abi_run.py checks the C interface. Beyond the committed corpora, 400000 more random documents matched.

tools/bench.py times both parsers on the same inputs: the engine takes about 0.6 of cmark-gfm's time over the set, from 0.46 on lists and tables to 1.05 on long backtick runs.

Differences

  • The memory limit. parse_with_limit abandons a parse once the tree holds more than a given number of bytes, where Telegram Desktop ran cmark-gfm under a budgeting allocator with an abort flag. The engine counts its node arena, strings, block contents and references, not allocator calls, so the threshold is not at the same byte. tools/memory.py compares the two counts on large inputs of many shapes: at a 120 MB limit both stop the same ones. The engine also checks the limit while it splits email autolinks out of text, where cmark-gfm finishes the pass before it looks at the flag, so a paragraph of addresses stops near the limit instead of holding several hundred megabytes first.
  • Undefined behaviour in C. Where cmark-gfm reads past the end of a buffer (an entity name followed by a NUL, a scanner that accepts NUL at the end of a subject), tmarkdown stops at the end. The parser never reaches these cases, as it replaces NUL bytes before scanning.

tmarkdown is released under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages