fix: preserve problem Markdown and add full-statement copying

Merged
BO
2mo ago
+660-46

What does this PR aim to accomplish?:

Fix copied problem Markdown losing TeX expressions and paragraph/line breaks, and add the requested section, sample, and full-statement copying controls. Closes #1030. Closes #1011.

How does this PR accomplish the above?:

  • Recover original TeX from KaTeX and MathJax, including XMOJ's MathJax 3.0.5, without modifying the rendered math.
  • Preserve paragraph and hard line breaks, normalize HTML formatting indentation in prose, preserve preformatted explanations as fenced code blocks, keep display formulas and paragraph separation within table cells, preserve empty columns, and retain list separation. Remove paragraph collapsing from the legacy copy path.
  • Serialize ordinary header tables as Markdown with alignment, escaped pipes and multiline cells. Preserve headerless, merged, nested, preformatted and linked tables as HTML, restoring TeX and resolving image/link URLs.
  • Add a copy button beside the problem title and on the samples section in both interfaces. Every problem section copy includes its ## heading.
  • Build complete statements with a # title, ordered section headings, numbered sample headings and plain code fences, sample explanations, data ranges and hints. Omit absent/empty sections and preserve sample whitespace.
  • Read the currently displayed language, exclude copy-button labels and injected problem IDs from the output, and avoid duplicate controls during rerenders.
  • Run 44 browser fixture cases automatically with npm test and the Test workflow. Playwright remains test tooling; no userscript runtime dependency is added.

Validation:

  • node --check XMOJ.user.js and git diff --check passed.
  • npm test: all 15 Node test entries passed, including 44 browser fixture cases, the existing contest tests, and missing/duplicate extraction-anchor checks.
  • Reproduce with npm install, npx playwright install chromium, and npm test. XMOJ_CHROMIUM can select an existing Chromium executable.
  • The actual Vue contest bundle was tested against mocked read-only API data: full-statement and samples buttons, paragraph/TeX preservation, active-language switching, empty-section omission, and duplicate-control prevention passed.
  • The human table review is covered by section/full-copy callbacks and table regressions. A Marked 4.3.0 render check verifies 14 table examples, including formulas, merged cells, nested tables, captions, links and preformatted whitespace.
  • The human review’s boarding example is covered by section/full-copy regressions: seat diagrams remain fenced, and prose has no extra blank lines or leading tabs. A separate Marked 4.3.0 render check preserves all four code blocks and both diagrams exactly.
  • Classic markup fixtures exercise the real shared clipboard callbacks, visible-language switching in both directions, wrapped sample containers, numbered samples, and untrimmed preformatted whitespace.
  • Real KaTeX 0.16.11 and XMOJ's MathJax 3.0.5 were also verified in the original copy-button checks.

By submitting this pull request, I confirm the following:

  • 1. Read the contributor's guide and the entire template; this branch is based on and targets dev.
  • 2. Commented on the proposed changes within the code.
  • 3. Tested the changes as described above.
  • 4. Maintainer confirmation: willingness to help maintain this change.
  • 5. Compatible with the repository's GNU General Public License v3.
  • 6. The fix and review feedback are grouped into focused commits.
  • 7. Checked for another open PR for this fix; none exists.
  • 8. The change fixes the reported loss of formulas and formatting.
  • 9. Maintainer confirmation: acceptance of the submission policy.
  • 10. Maintainer confirmation: contribution terms.
  • 11. Human verification on the live site in both the new and old/classic UIs is pending. Browser fixtures and automated tests were verified; live-site coverage is not claimed.

  • I have read the above and my PR is ready for review. Check this box to confirm

Summary by Sourcery

Preserve formulas and Markdown formatting when copying content from contest problem and solution pages.

Bug Fixes:

  • Preserve TeX source, math delimiters, paragraph spacing, Markdown hard breaks, and preformatted whitespace when copying contest content as Markdown.

Enhancements:

  • Support TeX extraction from KaTeX, MathML, and both legacy and current MathJax APIs without modifying the rendered DOM.

CI:

  • Run browser-based Markdown copy regression tests in CI with Playwright and Chromium.

Tests:

  • Add browser regression coverage for mathematical content, Markdown formatting, tables, lists, legacy copying, whitespace, DOM preservation, and extraction-anchor validation.

Chores:

  • Bump the userscript and package version to 3.7.2.

Summary by cubic

Preserves TeX source and Markdown formatting when copying contest problem and solution content, and adds copy buttons for full statements, titled sections, and samples. Closes #1030 and #1011.

  • Recovers TeX from KaTeX annotations, MathML, and both modern and legacy MathJax expression lists without touching rendered math.
  • Converts simple tables to pipe tables with alignment; keeps merged, nested, math-bearing, or link-bearing tables as HTML with TeX restored and absolute URLs. Preserves math delimiters, paragraph and hard line breaks, preformatted explanations and samples as fenced code blocks, list separation, and wrapped sample containers; code fences keep the language from pre and code classes and outlast backtick runs in the source.
  • Adds copy buttons beside titles and section headings; full statements use # and ## headings, numbered samples with plain fences, explanations, ranges, and hints, omitting empty sections.
  • Reads the currently displayed language and excludes copy-button labels and injected problem IDs from the output; controls stay idempotent across rerenders.
  • Adds Playwright-based browser fixture coverage run through npm test and the Test workflow, and bumps the version to 3.7.2.