Skip to content

Add: support pulldown-cmark math. - #66

Merged
letmutex merged 2 commits into
letmutex:mainfrom
bjones1:math-support
Dec 16, 2025
Merged

letmutex merged 2 commits into
letmutex:mainfrom
bjones1:math-support

Conversation

@bjones1

@bjones1 bjones1 commented Dec 9, 2025 •

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Support for LaTeX-style math in spans: inline math → $...$, display math → $$...$$.
  • Bug Fixes

    • Corrected attribute-count comparison that affected faithful serialization thresholds.
  • Tests

    • Added tests covering math conversion and escaping behavior.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Dec 9, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Adds a new span handler to detect and convert math-marked spans to Markdown ($...$ or $$...$$) and integrates it into the element handler dispatch; also adjusts the serialize_if_faithful macro to cast attribute count to i64 before comparison.

Changes

Cohort / File(s) Summary
Macro utility update
src/element_handler/element_util.rs
Changed serialize_if_faithful macro comparison to cast element.attrs.len() from usize to i64 before comparing to the allowed maximum
Module registration and dispatch
src/element_handler/mod.rs
Added mod span; and wired span::span_handler into the handlers registration for the "span" tag
Math span handler implementation
src/element_handler/span.rs
New span_handler that recognizes class="math math-inline" and class="math math-display", returning $...$ or $$...$$ respectively; falls back to faithful serialization or concatenated child text
Test coverage
tests/basic_tests.rs
Added math test asserting inline and display math conversions and an escaping edge case

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review focus:
    • The i64 cast in serialize_if_faithful — ensure comparison semantics match intent and no implicit sign issues.
    • Class-matching logic in span_handler (exact class string vs. tolerant matching).
    • Correct handling of child nodes and trimming/escaping inside math spans.
    • Test assertions for edge-case escaping.

Possibly related PRs

Poem

🐰
I nibble dots and dollar signs,
wrapping math in tidy lines.
Inline hops with single flair,
Display sprawls with double care.
Hoppity, convert — polished and fair!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add: support pulldown-cmark math.' accurately reflects the main purpose of the PR: adding mathematical expression support via pulldown-cmark's math syntax.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
src/element_handler/element_util.rs (1)

192-192: Consider using a more explicit sentinel mechanism than -1.

Using -1 as a sentinel value requires the i64 cast and makes the API less type-safe. Consider alternatives such as passing an Option<i64> or a dedicated enum to make the "always serialize" case more explicit.

For example:

enum AttrLimit {
    Max(usize),
    AlwaysSerialize,
}

This would make the intent clearer and avoid the need for casting and negative sentinels.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3d71970 and 232bc9f.

📒 Files selected for processing (4)
  • src/element_handler/element_util.rs (1 hunks)
  • src/element_handler/mod.rs (3 hunks)
  • src/element_handler/span.rs (1 hunks)
  • tests/basic_tests.rs (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/element_handler/span.rs (2)
src/element_handler/list.rs (1)
  • attr (88-88)
src/element_handler/code.rs (1)
  • content (121-121)
src/element_handler/mod.rs (2)
src/element_handler/element_util.rs (1)
  • serialize_element (46-177)
src/element_handler/span.rs (1)
  • span_handler (10-36)
🔇 Additional comments (7)
tests/basic_tests.rs (1)

669-673: Good test coverage for escaping behavior.

This test correctly verifies that Markdown special characters (like _, *, [, ], <, >) inside math delimiters are not escaped, which is the expected behavior for math content.

src/element_handler/mod.rs (1)

17-17: LGTM! Proper integration of the span handler.

The span module is correctly declared, imported, and registered following the established pattern for other element handlers. The handler is registered before the generic block handlers, which ensures the specific span logic takes precedence.

Also applies to: 26-26, 180-180

src/element_handler/span.rs (4)

19-20: Verify that HTML entities are properly decoded before wrapping in math delimiters.

The code accesses text content via contents.borrow().to_string(), which should provide decoded text. Confirm that HTML entities like &lt;, &gt;, &amp; are properly decoded to their character equivalents before being wrapped in $ or $$ delimiters.

The test on line 672 of tests/basic_tests.rs includes HTML entities and expects them decoded in the output, which suggests this works correctly. However, explicit verification would be valuable.

Also applies to: 23-24


29-29: Note: Reliance on i64 cast in serialize_if_faithful macro.

This call uses -1 as a sentinel value to mean "always serialize in faithful mode", which requires the as i64 cast added to the serialize_if_faithful! macro in element_util.rs line 192. While functional, this creates a subtle dependency between this handler and the macro implementation.


31-35: Fallback logic is appropriate.

The fallback correctly returns the concatenated text content of children when not in faithful mode and when the span doesn't match the math pattern. This aligns with the expected behavior for generic span elements.


10-26: [rewritten comment]
[classification tag]

src/element_handler/element_util.rs (1)

192-192: Verify that the cast to i64 doesn't affect other usages of this macro.

The cast from usize to i64 enables the use of -1 as a sentinel value (as seen in span.rs). Ensure that all existing call sites of serialize_if_faithful! throughout the codebase are compatible with this change. Check that all invocations pass values appropriate for i64 parameters and that no edge cases arise from the type conversion.

Comment thread tests/basic_tests.rs Resolved
Comment thread tests/basic_tests.rs Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/element_handler/element_util.rs (1)

188-194: serialize_if_faithful sentinel semantics look correct

Casting attrs.len() to i64 makes the -1 sentinel work as documented: in faithful mode, any attribute count (including zero) will trigger HTML serialization when $num_attrs_allowed is -1. This aligns with the new comment and keeps the existing “more than N attributes” behavior for non‑negative limits. If you expect broader usage, you might consider explicitly documenting that $num_attrs_allowed is intended to be an i64 to avoid surprising type errors at call sites.

tests/basic_tests.rs (1)

657-674: Math conversion tests are well‑targeted and readable

The three assertions cover the core behaviors: inline vs. display math wrapping and the “no extra escaping inside math” requirement with a good variety of characters. This is solid coverage for the new span handler. If you later expand math support, you might add follow‑up tests for mixed content (e.g., math spans adjacent to regular text) or faithful mode, but that’s not required for this PR.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 232bc9f and af6bc12.

📒 Files selected for processing (2)
  • src/element_handler/element_util.rs (1 hunks)
  • tests/basic_tests.rs (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/element_handler/element_util.rs (2)
src/element_handler/mod.rs (2)
  • options (316-316)
  • options (353-355)
src/lib.rs (1)
  • options (171-174)

This was referenced Dec 9, 2025
@letmutex
letmutex merged commit af6bc12 into letmutex:main Dec 16, 2025
@bjones1
bjones1 deleted the math-support branch December 16, 2025 14:01
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