✨ NEW: Add section reference plugin (section_ref)#144
Open
chrisjsewell wants to merge 4 commits into
Open
Conversation
Captures section-sign references such as §1, §1.1 and §2.3.4 — the
syntax LLMs commonly use to refer to numbered headings — into dedicated
`section_ref` inline tokens (meta["number"] holds the number string),
so downstream renderers such as MyST-Parser can resolve them to heading
cross-references. Default rendering is a `<span class="section-ref">`.
Uses `md.inline.add_terminator_char("§")` (markdown-it-py >= 4.1.0),
since § is not a default text-rule terminator character.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LwRkZLzpbHdb3pgmctmn6H
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #144 +/- ##
==========================================
- Coverage 92.80% 92.79% -0.02%
==========================================
Files 31 40 +9
Lines 1835 2289 +454
==========================================
+ Hits 1703 2124 +421
- Misses 132 165 +33
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LwRkZLzpbHdb3pgmctmn6H
meta["numbers"] now holds e.g. [1, 1] for §1.1, so downstream renderers do not need to re-parse the dotted number string. A list (not tuple) is used so Token.as_dict()/JSON round-trips are type-stable. The original matched text remains available in token.content. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LwRkZLzpbHdb3pgmctmn6H
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LwRkZLzpbHdb3pgmctmn6H
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a new
section_refinline plugin that captures section-sign references —§1,§1.1,§2.3.4— the syntax LLMs commonly use to refer to numbered headings in documents.Each reference becomes a dedicated
section_reftoken:content: the matched text (e.g."§1.1")markup:"§"meta["numbers"]: the parsed section number as a list of ints (e.g.[1, 1]), so consumers don't re-parse the dotted string (a list rather than a tuple soToken.as_dict()/JSON round-trips are type-stable; leading zeros are normalized, with the original text preserved incontent)so downstream renderers (notably MyST-Parser) can resolve them to heading cross-references. The default rendering is a neutral
<span class="section-ref">§1.1</span>, overridable viamd.add_render_rule("section_ref", ...).Initial use case
executablebooks/MyST-Parser#1170 (draft) consumes this plugin as a new
section_refMyST extension, resolving the references to internal links targeting the correspondingly numbered heading of the document. That PR is blocked on this one being merged and released (it needsmdit-py-plugins >= 0.7).Matching rules
§must be immediately followed by ASCII digits ([0-9], never unicode digits), optionally in further dot-separated groups —§ 1and bare§stay literal textsee §1.captures§1§1a,§1.2b), while non-ASCII letters do not block it (见§3章captures§3)\§1stays literalImplementation notes
§is not one of markdown-it's default text-rule terminator characters, so a plain inline rule would never fire mid-paragraph. The plugin therefore usesmd.inline.add_terminator_char("§"), the same mechanism as thegfm_autolinkplugin, and raises aRuntimeErroron markdown-it-py < 4.1.0 (package dependency range unchanged).Testing
tests/fixtures/section_ref.md) covering emphasis/link/code/heading contexts, boundary and escaping edge cases--strictclean under both markdown-it-py 3.x and 4.xattrs,myst_role,dollarmath,gfm_autolink,footnote,deflist,anchors), GFM autolink URLs containing§, link-labelposMaxslices, the typographer, and cross-instance state leakage🤖 Generated with Claude Code
https://claude.ai/code/session_01LwRkZLzpbHdb3pgmctmn6H