Skip to content

✨Allow plugins to register inline terminator characters#391

Open
Copilot wants to merge 6 commits intomasterfrom
copilot/allow-plugins-register-terminators
Open

✨Allow plugins to register inline terminator characters#391
Copilot wants to merge 6 commits intomasterfrom
copilot/allow-plugins-register-terminators

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 4, 2026

The inline text rule used a hardcoded, unexpandable set of terminator characters, forcing plugins that need to trigger on non-terminator characters (e.g. w for GFM www. autolinks) to resort to core-rule post-processing workarounds.

Changes

  • parser_inline.py: Moves the terminator set onto ParserInline as _terminator_chars (a set[str] seeded from _DEFAULT_TERMINATORS) with a pre-compiled terminator_re: re.Pattern[str] attribute. Exposes add_terminator_char(ch) to extend the set; the regex is rebuilt eagerly only when a genuinely new character is added, keeping zero per-call overhead in the hot path.

  • rules_inline/text.py: Drops the module-level _TerminatorChars set and @functools.cache-decorated factory. The text rule now reads state.md.inline.terminator_re directly.

  • docs/contributing.md: Updates the "Why is my inline rule not executed?" FAQ to document the new API.

Usage

def gfm_autolink_plugin(md: MarkdownIt) -> None:
    md.inline.add_terminator_char("w")
    md.inline.ruler.push("gfm_autolink_www", _www_rule)

Fully backward-compatible — the default terminator set is unchanged.

Copilot AI changed the title [WIP] Allow plugins to register inline terminator characters Allow plugins to register inline terminator characters May 4, 2026
Copilot AI requested a review from chrisjsewell May 4, 2026 21:17
Comment thread markdown_it/parser_inline.py Outdated
for name, rule2 in _rules2:
self.ruler2.push(name, rule2)
# Characters that stop the text rule, allowing other inline rules to fire.
self._terminator_chars: set[str] = set(_DEFAULT_TERMINATORS)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we remove this performance degradation in the non-extended path, by making terminator_char's additive to the defaults. Then we don't need to make this set every time

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 6100aeb. The default regex is now compiled once at module level as _default_terminator_re and assigned directly in __init__ — no set() copy, no per-instance rebuild. __init__ initialises _extra_terminator_chars as an empty set, and add_terminator_char only rebuilds the regex when a character genuinely outside _DEFAULT_TERMINATORS is added.

Copilot AI requested a review from chrisjsewell May 5, 2026 03:16
Comment thread markdown_it/parser_inline.py Outdated

# Pre-compiled regex for the default terminator set. Shared across all ParserInline
# instances that have not had extra chars added, so __init__ pays no allocation cost.
_default_terminator_re: re.Pattern[str] = re.compile(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Have this as a cached function, as before. So that we can lazy compute it once

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in d0d8d46. _default_terminator_re is now a @functools.cache-decorated function — the regex is compiled at most once (on the first ParserInline instantiation) and the cached result is shared across all unextended instances.

Copilot AI requested a review from chrisjsewell May 5, 2026 03:40
@chrisjsewell chrisjsewell changed the title Allow plugins to register inline terminator characters ✨Allow plugins to register inline terminator characters May 5, 2026
@chrisjsewell chrisjsewell marked this pull request as ready for review May 5, 2026 06:52
@codecov
Copy link
Copy Markdown

codecov Bot commented May 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.81%. Comparing base (8933147) to head (d5cf7ff).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #391   +/-   ##
=======================================
  Coverage   95.80%   95.81%           
=======================================
  Files          64       64           
  Lines        3457     3463    +6     
=======================================
+ Hits         3312     3318    +6     
  Misses        145      145           
Flag Coverage Δ
pytests 95.81% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chrisjsewell
Copy link
Copy Markdown
Member

@copilot fix pre-commit

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 5, 2026

@copilot fix pre-commit

Fixed in d5cf7ff — ruff corrected the import ordering and reformatted two expressions in parser_inline.py. All pre-commit checks now pass.

Copilot AI requested a review from chrisjsewell May 5, 2026 06:58
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.

Allow plugins to register inline terminator characters

2 participants