Skip to content

Table fix - #68

Merged
letmutex merged 6 commits into
letmutex:mainfrom
bjones1:table-fix
Dec 16, 2025
Merged

letmutex merged 6 commits into
letmutex:mainfrom
bjones1:table-fix

Conversation

@bjones1

@bjones1 bjones1 commented Dec 10, 2025 •

Copy link
Copy Markdown
Contributor

Please review and merge #67 before this.

This fixes a bug -- table cells in faithful mode cannot be translated to Markdown if they contain block elements.

Summary by CodeRabbit

  • New Features

    • Added support for mathematical notation rendering in span elements with inline and display LaTeX formats.
    • Enabled span element processing in the conversion pipeline.
  • Improvements

    • Enhanced table cell handling for content with block-level elements.
    • Refined serialization behavior in faithful rendering mode.
  • Tests

    • Added tests for math notation formatting.
    • Added tests for table cells containing block elements.
  • Chores

    • Updated project dependencies to latest versions.

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

@coderabbitai

coderabbitai Bot commented Dec 10, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

The pull request introduces support for detecting and formatting math content within span elements (using LaTeX notation), enhances table cell handling to detect block-level children, and updates several dependencies. The serialize_if_faithful macro is modified to accept signed integer thresholds for more flexible serialization control.

Changes

Cohort / File(s) Summary
Dependency updates
Cargo.toml
Updated html5ever and markup5ever_rcdom from 0.35.0 to 0.36; scraper from 0.23.1 to 0.25; criterion from 0.7 to 0.8. Moved indoc from dev-dependency to regular dependency.
Macro enhancement
src/element_handler/element_util.rs
Modified serialize_if_faithful macro to cast attribute count to i64, allowing negative thresholds (e.g., -1) to force faithful-mode HTML serialization regardless of attribute count.
New span handler
src/element_handler/span.rs
src/element_handler/mod.rs
Added new span_handler module that detects math spans (class "math math-inline" or "math math-display") and formats them as LaTeX ("$...$" or "$$...$$"). Registered span handler in element handler initialization.
Table cell block detection
src/element_handler/td_th.rs
Enhanced td_th_handler to detect block-level child elements in Faithful translation mode using new imports (dom_walker, node_util, options). Passes -1 to serialize_if_faithful when block children exist, otherwise 0.
Formatting adjustment
src/element_handler/table.rs
Applied multi-line formatting to conditional check; logic unchanged.
Test additions
tests/basic_tests.rs
tests/table_tests.rs
Added block math tests for inline and display math formatting; refactored attribute check from find().is_some() to any(). Added test for table cells containing block elements with indoc import.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Macro change implications: The i64 cast in serialize_if_faithful changes comparison semantics and interaction with callers (especially -1 usage in td_th handler); verify no unexpected behavior from signed/unsigned boundary conditions.
  • Math content detection: Span handler logic that checks for class attributes and extracts LaTeX content needs validation of edge cases (multiple children, missing class attribute, whitespace handling).
  • Block element detection in table cells: New is_block_element check alters serialization behavior for td/th; verify this correctly identifies block elements and doesn't interfere with nested content handling.
  • Test coverage: New math tests and table block cell test should be verified for correctness of expected outputs and escaping behavior.

Possibly related PRs

Poem

🐰 A span now knows its math, dear friend,
Where "math-inline" and "display" blend,
With dollar signs and faithful care,
Block cells in tables get their share! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Table fix' is vague and generic, failing to convey the specific nature of the fix—that it addresses handling block elements in table cells during faithful mode translation. Consider a more descriptive title such as 'Fix table cell translation with block elements in faithful mode' or 'Handle block elements in table cells for faithful mode' to clearly communicate the specific bug being fixed.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ 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: 0

🧹 Nitpick comments (3)
tests/basic_tests.rs (1)

548-553: Use of .any improves clarity without changing behavior.

Switching from find(...).is_some() to .any(...) on element.attrs is idiomatic and keeps the fallback logic unchanged.

src/element_handler/td_th.rs (1)

3-8: Faithful‑mode handling for td/th with block children is sound and matches the new tests.

The has_block_elements check, gated on TranslationMode::Faithful, and the use of serialize_if_faithful! with -1 cleanly force HTML serialization for table cells containing block elements while preserving prior behavior for “simple” cells and all non‑faithful modes. Delegation to handle_or_serialize_by_parent still correctly enforces <td>/<th> inside <tr>.

If you ever want to micro‑optimize, consider changing handle_or_serialize_by_parent to accept a slice (&[&str]) so you can pass a static &["tr"] instead of allocating a Vec on each call.

Also applies to: 11-26

src/element_handler/span.rs (1)

1-36: Span math handling and faithful‑mode behavior are well factored.

The handler correctly:

  • Detects simple math spans (class="math math-inline" / "math math-display", single text child) and emits $…$ / $$…$$ in all modes.
  • Preserves prior faithful‑mode semantics for non‑math spans by using serialize_if_faithful!(…, -1) to always serialize as HTML.
  • Unwraps generic spans to their contents in non‑faithful modes via walk_children, which is a sensible default.

Minor nits you can ignore or address later:

  • The doc comment string "math math-inline/display> looks slightly off; consider fixing the "/> placement.
  • If you ever need to support more flexible class ordering (e.g. additional classes around math-inline), you could expand the class check to split on whitespace instead of strict equality.
📜 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 e6cbda9.

📒 Files selected for processing (8)
  • Cargo.toml (1 hunks)
  • src/element_handler/element_util.rs (1 hunks)
  • src/element_handler/mod.rs (3 hunks)
  • src/element_handler/span.rs (1 hunks)
  • src/element_handler/table.rs (1 hunks)
  • src/element_handler/td_th.rs (1 hunks)
  • tests/basic_tests.rs (2 hunks)
  • tests/table_tests.rs (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (4)
src/element_handler/mod.rs (1)
src/element_handler/span.rs (1)
  • span_handler (10-36)
tests/table_tests.rs (2)
src/lib.rs (2)
  • convert (30-32)
  • convert (106-144)
tests/common/mod.rs (1)
  • convert (8-16)
src/element_handler/td_th.rs (3)
src/dom_walker.rs (1)
  • is_block_element (433-435)
src/element_handler/element_util.rs (1)
  • handle_or_serialize_by_parent (16-42)
src/node_util.rs (1)
  • get_node_tag_name (5-11)
src/element_handler/element_util.rs (2)
src/element_handler/mod.rs (2)
  • options (317-317)
  • options (354-356)
src/lib.rs (1)
  • options (171-174)
🔇 Additional comments (6)
Cargo.toml (1)

15-19: Dependency moves and bumps look consistent; rely on test suite for compatibility.

Moving indoc to [dependencies] for integration tests and bumping html5ever, markup5ever_rcdom, scraper, and criterion are all reasonable and self‑contained here. Just make sure the full test suite passes to catch any behavioral changes in parsing/serialization from these version upgrades.

Also applies to: 23-24

src/element_handler/table.rs (1)

126-132: Faithful‑mode fallback condition remains correct.

The TranslationMode::Faithful && !all_children_translated guard is unchanged logically and still correctly forces HTML serialization when any child fails Markdown translation.

tests/table_tests.rs (1)

7-7: New block‑cell table test correctly captures the faithful‑mode behavior.

Importing indoc here and asserting that a table with <th><p>…</p></th> round‑trips as HTML in faithful mode is aligned with the new td_th/table logic and should guard against regressions in block‑content handling.

Also applies to: 183-224

src/element_handler/mod.rs (1)

17-18: Span handler wiring is consistent with existing handler registration.

Adding the span module, importing span_handler, and registering it for "span" integrates cleanly into ElementHandlers without altering other tag behavior. The ordering is safe since span had no prior handler.

Also applies to: 49-49, 181-182

src/element_handler/element_util.rs (1)

188-203: Updated serialize_if_faithful macro correctly supports negative thresholds.

Documenting -1 as “always serialize in faithful mode” and comparing element.attrs.len() as i64 > $num_attrs_allowed gives the intended behavior while preserving existing semantics for non‑negative thresholds and non‑faithful modes.

tests/basic_tests.rs (1)

656-673: Verify backslash count in the math escaping test expectation.

The final assertion tests escaping behavior with input … a \; b in the HTML, expecting output … a \\; b$$ in the result. This requires verification of the span_handler implementation to confirm whether it preserves the original single backslash or if additional escaping is applied. Check the actual behavior and adjust the expected string accordingly if needed.

@bjones1 bjones1 mentioned this pull request Dec 12, 2025
@letmutex
letmutex merged commit e6cbda9 into letmutex:main Dec 16, 2025
@bjones1
bjones1 deleted the table-fix branch December 16, 2025 14:02
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