fix: match notesAutoRuby keys longest-first and literally - #2731
Open
giaBaoJS wants to merge 1 commit into
Open
Conversation
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.
✅ Deploy Preview for slidev ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
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.
Description
notesAutoRubybuilds one alternationRegExpfrom 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:日本語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++throwsSyntaxError: 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
\b...\bword-boundary path (word-only keys are already regex-safe).renderNoteclosure intoapplyNotesAutoRubyinnode/utils.tsso it can be tested directly, and add cases to the existingnode/utils.test.ts.Keys that already worked keep their current behaviour.
Verification
pnpm verify(build, typecheck, lint, test) passes: 28 files / 246 tests.