Skip to content

Citation scanner respects inline code-span boundaries - #53

Merged
mmcky merged 1 commit into
mainfrom
feature/cite-code-span
Jun 12, 2026
Merged

mmcky merged 1 commit into
mainfrom
feature/cite-code-span

Conversation

@mmcky

@mmcky mmcky commented Jun 12, 2026

Copy link
Copy Markdown

Fixes #46

What

The citation auto-detector's bracket branch regex-scans the raw text between […] for pandoc @key tokens. Because the citation inline rule intercepts [ before the tokenizer ever reaches the backticks inside, a fully code-span-enclosed @-token like

**[NEW: wrap it in `@tf.function` for speed]**

became a broken cite node (label: tf.function, backtick swallowed into the prefix text), destroying the bracket text and code formatting — plus an unresolved-citation warning.

How

In markdown-it-myst's citation rule, compute the CommonMark inline code-span ranges within the bracket content (backtick-run matching: a span opened by N backticks closes only at the next run of exactly N; escaped backticks don't open spans) and back off the bracket-citation parse when a matched @ falls inside one.

Notes on scope:

  • The in-text @ branch needs no guard: the tokenizer never visits positions inside a code span, so `@tf.function` outside brackets was already safe (confirmed by the issue's control line).
  • Backing off degrades gracefully: in [@real; `@code`] the bracket parse is rejected but @real still cites via the in-text rule and the code span survives.
  • A legitimate citation whose suffix contains a code span ([see @smith2021 and `@tf.function`]) still cites smith2021 — the matched @ is outside any span, and the suffix's parseInline tokenizes the code span correctly.

Relation to upstream

jupyter-book#1618 tracks the overzealous-@ class for the [@handle](url) link shape (already guarded here by the charAfter != 0x28 check and the fork's isLinkContext/isUrlContext guards). This PR addresses the inline-code shape with a code-span-boundary guard rather than the link-label heuristic — a separate, complementary fix and an upstream candidate.

Testing

  • 5 new token-level cases in packages/markdown-it-myst/tests/citations.yml: the issue repro, citation-with-code-span-suffix, the degraded [@real; @code] case, double-backtick spans, and escaped backticks (which must not suppress citation).
  • Full monorepo build + test (73 turbo tasks) and lint pass.
  • The Deep-Learning book's **\[NEW: … escaping workaround becomes unnecessary once this lands.

🤖 Generated with Claude Code

The bracket branch of the citation rule regex-scans the raw text
between [ and ] before the tokenizer ever reaches enclosed backticks,
so an @-token inside a code span became a broken cite node that
swallowed the bracket text and code formatting. Compute CommonMark
code-span ranges within the bracket and back off when a matched @
falls inside one; legitimate keys elsewhere in the bracket still cite,
and the in-text @ rule is unaffected (positions inside code spans are
never rule starts).

Fixes #46

Co-Authored-By: Claude Fable 5 <[email protected]>
Copilot AI review requested due to automatic review settings June 12, 2026 02:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Updates markdown-it-myst’s bracket citation auto-detector to respect CommonMark inline code-span boundaries, preventing @key-like tokens inside backticks from being mis-parsed as citations (fixes #46).

Changes:

  • Add a code-span range scanner for bracket contents and abort bracket-citation parsing when the matched @ is inside a code span.
  • Expand citation token tests to cover inline-code @ tokens, double-backtick spans, escaped backticks, and mixed cite/code cases.
  • Add a patch changeset describing the behavioral fix.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
packages/markdown-it-myst/src/citations.ts Adds codeSpanRanges() and uses it to guard bracket citation parsing against @ inside inline code spans.
packages/markdown-it-myst/tests/citations.yml Adds regression tests covering code-span-contained @ tokens and mixed citation/code scenarios.
.changeset/cite-code-span.md Records a patch release note for the citation parsing fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mmcky
mmcky merged commit 00740bc into main Jun 12, 2026
7 checks passed
@mmcky
mmcky deleted the feature/cite-code-span branch June 12, 2026 02:19
mmcky added a commit to QuantEcon/claude-latex-to-myst that referenced this pull request Jun 12, 2026
The warning was fixed upstream in the publisher: the qe mystmd fork's
citation scanner now respects inline code-span boundaries
(QuantEcon/mystmd#53, merged 2026-06-12), so the `@tf.function` inside
a code span in ch02_deqns no longer parses as a broken cite node. The
warning's disappearance during the #144-#147 close-out was initially
read as build flakiness on byte-identical input — it was actually the
local myst binary picking up the fix. Re-baselining locks the
improvement: a reappearance now fails the render gate.

Co-authored-by: Claude Fable 5 <[email protected]>
mmcky added a commit that referenced this pull request Jun 12, 2026
* Track PR #53 (cite-code-span) in VERSION.yml and UPSTREAM-PRS.yml

Records squash commit 00740bc as merged feature 9 (tag pending) and
adds a standalone upstream candidate carrying the upstream-context
notes from #54 (escaping cannot cover code spans; precedent in
jupyter-book#2684 and jupyter-book#2891).

Co-Authored-By: Claude Fable 5 <[email protected]>

* Reword "un-escapable" in cite-code-span candidate notes

Co-Authored-By: Claude Fable 5 <[email protected]>

---------

Co-authored-by: Claude Fable 5 <[email protected]>
mmcky added a commit to QuantEcon/claude-latex-to-myst that referenced this pull request Jun 13, 2026
#151) (#153)

* ci: render gate — run build_smoke.py per book in Job 2 on a pinned mystmd fork (#151)

CI gated on the unit suite, both golden tiers, the differential gate,
and the per-book count diff — but never ran `myst build`, so output
passing every structural check could still fail as a product
(lesson 046: structural parity is not render parity).

Job 2 (fixture-counts, label-gated, non-blocking) now runs the
existing render gate per book after the count check: build_smoke.py
overlays regen onto the worked-on mystmd/, runs `myst build --html`,
and multiset-diffs normalized warnings against
tests/baselines/build-<book>.txt (dp1's empty baseline is the
zero-warning contract; any NEW warning line fails).

The build uses a pinned QuantEcon/mystmd FORK ref (MYSTMD_REF =
d1a942ca, v1.10.1 qe-v8) — not upstream npm mystmd: the committed
baselines assume fork behavior (qe-v5 book-mode numbering, fancy
lists from QuantEcon/mystmd#50, the cite-scanner fix from
QuantEcon/mystmd#53). The qe-v8 *tag* builds v1.9.1, so the pin is
the exact main SHA the baselines were validated against. The fork CLI
bundles to one self-contained dist/myst.cjs, cached keyed on the ref
so repeat runs skip the clone + bun + turbo build. init+fetch (not
clone --branch) so the ref can be a tag or a bare SHA.

Verified locally with the exact CI recipe: fetch d1a942ca, bun
install --frozen-lockfile, bun run build -> myst.cjs reports
"v1.10.1 (qe-v8)"; build_smoke --check passes all three books
(dp1 0 / dp2 3 / deep-learning 18 baseline lines, exact match).

Deferred from the issue (recorded there): the always-on Job-1
mini-project smoke and the pdftotext numbering comparison.

Closes #151.

Co-Authored-By: Claude Fable 5 <[email protected]>

* ci: trigger Job 2 with fixture-check label applied

Co-Authored-By: Claude Fable 5 <[email protected]>

* ci: install bun via pinned oven-sh/setup-bun, not curl|bash

Copilot review: the job pins pandoc and MYSTMD_REF but installed bun
unpinned from bun.sh — runner drift could pull a different bun.
Pin bun-version to 1.3.10, the fork's own packageManager declaration.

Co-Authored-By: Claude Fable 5 <[email protected]>

---------

Co-authored-by: Claude Fable 5 <[email protected]>
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.

Citation scanner parses @-tokens inside inline code spans: [… @tf.function …] becomes a broken cite node

2 participants