Skip to content

fix: match notesAutoRuby keys longest-first and literally - #2731

Open
giaBaoJS wants to merge 1 commit into
slidevjs:mainfrom
giaBaoJS:fix/notes-auto-ruby-key-matching
Open

fix: match notesAutoRuby keys longest-first and literally#2731
giaBaoJS wants to merge 1 commit into
slidevjs:mainfrom
giaBaoJS:fix/notes-auto-ruby-key-matching

Conversation

@giaBaoJS

Copy link
Copy Markdown

Description

notesAutoRuby builds one alternation RegExp from the configured keys. Two things go wrong while that regex is built.

1. Keys are sorted shortest-first. The comparator is (b, a) => b.length - a.length, whose parameter names are transposed, so it sorts ascending by length. A regex alternation takes the first alternative that matches at a position, so a key that is a prefix of another key always wins and the longer key can never match:

---
notesAutoRuby:
  日本: ni hon
  日本語: ni hon go
---

日本語 renders as <ruby>日本<rt>ni hon</rt></ruby>語.

2. Keys are interpolated into the pattern unescaped. Keys that do not match /^[\w-]+$/ go into the alternation verbatim, so regex metacharacters are interpreted:

  • C++ throws SyntaxError: Invalid regular expression: /(C++)/g: Nothing to repeat, which breaks note rendering for the whole deck.
  • (foo) becomes a capture group, so the literal text (foo) is never wrapped.

Changes

  • Sort keys longest-first.
  • Escape regex metacharacters in keys that do not take the \b...\b word-boundary path (word-only keys are already regex-safe).
  • Move the replacement out of the renderNote closure into applyNotesAutoRuby in node/utils.ts so it can be tested directly, and add cases to the existing node/utils.test.ts.

Keys that already worked keep their current behaviour.

Verification

pnpm verify (build, typecheck, lint, test) passes: 28 files / 246 tests.

Keys were sorted shortest-first, so a key that is a prefix of another one
always won the alternation and the longer key never matched. Keys were also
interpolated into the RegExp unescaped, which threw on keys such as `C++`
and silently mis-matched keys containing brackets or parentheses.

Extract the replacement into `applyNotesAutoRuby` and cover it with tests.
@netlify

netlify Bot commented Aug 30, 2026

Copy link
Copy Markdown

Deploy Preview for slidev ready!

Name Link
🔨 Latest commit 82dc4b8
🔍 Latest deploy log https://app.netlify.com/projects/slidev/deploys/6a9435c5168a000008c5c017
😎 Deploy Preview https://deploy-preview-2731--slidev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

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.

1 participant