Skip to content

feat: add support for LaTeX inline math replacement in MarkdownDecoder - #21

Closed
fazliddin-swe wants to merge 1 commit into
DoctorinaAI:masterfrom
fazliddin-swe:master
Closed

feat: add support for LaTeX inline math replacement in MarkdownDecoder#21
fazliddin-swe wants to merge 1 commit into
DoctorinaAI:masterfrom
fazliddin-swe:master

Conversation

@fazliddin-swe

Copy link
Copy Markdown

No description provided.

@mike-doctorina

Copy link
Copy Markdown
Collaborator

Thanks a lot for kicking this off, @ibragimov05! 🙏

Inline LaTeX math ships in #23 (0.0.9). It was reimplemented so that it is code-span/code-block safe and does not touch currency like $5, made opt-in (MarkdownDecoder(inlineMath: true)) so it never surprises non-math documents, and extended with superscripts/subscripts and a configurable command table. Your commit is credited via Co-authored-by on the implementing commit. Closing in favor of #23 — thank you for the idea and the initial version!

mike-doctorina pushed a commit that referenced this pull request Jul 29, 2026
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
mike-doctorina added a commit that referenced this pull request Jul 30, 2026
…uite + coverage (#23)

* feat: GitHub alerts, task lists, table alignment + parser fixes & tests

New syntax
- GitHub alert blocks (> [!NOTE]/[!TIP]/[!IMPORTANT]/[!WARNING]/[!CAUTION])
  via a new MD$Alert block, MD$AlertType, and BlockPainter$Alert.
- Task-list items (- [ ] / - [x]) via MD$ListItem.checked/isTask, rendered
  with a checkbox.
- Table column alignment (:---, :--:, ---:) captured on MD$Table.alignments
  and applied when rendering.
- Guarded $...$ inline LaTeX-to-Unicode math (code-span/code-block safe;
  currency like $5 preserved). Reworks PR #21 into a robust form.

Theme
- Adopt linkStyle (PR #22) cleanly; add per-type alert colors
  (alertColors / alertColorFor).
- Fix MarkdownThemeData.copyWith dropping builder and onLinkTap.

Parser correctness fixes (from an adversarial bug hunt)
- Emphasis no longer leaks to end-of-line for stray/unterminated markers
  (5 * 6 = 30, **oops) and no longer treats intraword _ as emphasis
  (snake_case, object_id) — CommonMark-inspired flanking + closer checks.
- Thematic breaks support ***, ___ and spaced variants; --- no longer
  greedily swallows trailing text.
- ATX headings require a space after #, reject 7+ #, and strip trailing #.
- ~~~ fenced code blocks recognized alongside ```.
- Emphasis surrounding a link/image is merged onto the link span.
- Link/image targets support <url> and single-quoted titles.

Tests
- Add 153 tests across parser (inline, block, gfm, edge cases), theme, and
  widget rendering; total suite now 215 and green (was 62).

BREAKING: MD$Block.map/maybeMap gain an `alert` branch for MD$Alert.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* Add comprehensive tests for Markdown parser functionality

- Introduced a golden characterization test to capture the exact structural output of the Markdown parser, ensuring stability against future changes.
- Created a regression test suite targeting edge cases and performance-related changes in the parser, documenting specific behaviors and invariants.
- Updated the unit test runner to include new test files for block parsing, inline parsing, GitHub Flavored Markdown (GFM), and edge cases.
- Added a variety of test cases covering prose, emphasis, links, images, lists, tables, and more to ensure robust parsing behavior.

* feat: make inline LaTeX math opt-in and configurable + improvements

Inline `$...$` math is now DISABLED by default, so literal dollar signs
(prices like `$5`, shell variables like `$HOME`) are never altered. Enable it
with `MarkdownDecoder(inlineMath: true)` or
`Markdown.fromString(text, inlineMath: true)`.

Improvements to the math feature:
- Configurable command table via `mathReplacements`; the defaults are exported
  as `kMarkdownMathCommands` so callers can extend rather than replace them.
- Superscript/subscript conversion: `x^2` -> x², `H_2O` -> H₂O, `x^{10}` -> x¹⁰,
  `a_i` -> aᵢ. Unmappable runs are left literal.
- More commands (implies/iff, perp/parallel, oplus/otimes, hbar/ell, Re/Im,
  therefore/because, extra arrows and relations).
- `\$` is now a recognized escape (literal dollar); an escaped opening `$`
  never starts a math run, so `\$\alpha\$` stays literal `$\alpha$`.

Making math opt-in also speeds up the default parse path (it no longer calls
the math pass at all): escapes-heavy text is now ~68% faster than baseline.

Tests: new test/parser/math_test.dart (commands, super/subscripts, code
protection, escaping, configurability, span integrity). The golden snapshot is
regenerated to reflect the new default (only the two math entries changed).

Co-authored-by: Fazliddin <faz.ibragimovv@gmail.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs: credit #22/#21 contributors in changelog

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test: cover phase-1 escape/link edges and unterminated math brace

Bring parser.dart line coverage to 99.6% (the remaining two lines are an
unreachable defensive branch in the list-traverse).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test: cover nodes, theme, and widget to 100% line coverage

Add nodes_test.dart (MD$Style helpers, block type/toString/maybeMap,
MD$ListItem/MD$TableRow, Markdown facade), extend theme_test.dart
(headingStyleFor, type/toString, MarkdownTheme.of/maybeOf), and add
widget_test.dart (context theme fallback in create/updateRenderObject).

nodes.dart, markdown.dart, theme.dart, widget.dart now at 100%; parser.dart
99.6%. render.dart left untouched pending its refactor.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs: expand README (GFM blocks, opt-in math), add codecov; release 0.1.0

- README: document GitHub alerts, task lists, table alignment, thematic
  breaks and opt-in inline math; refresh feature list and performance section.
- Add a Codecov coverage badge and a CI upload step (codecov-action).
- Bump version 0.0.9 -> 0.1.0 and retitle the CHANGELOG section.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Mike Matiunin <plugfox@gmail.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Fazliddin <faz.ibragimovv@gmail.com>
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.

2 participants