Prevent inline math input rule from replacing previous character#7481
Prevent inline math input rule from replacing previous character#7481n-siddarth wants to merge 3 commits intoueberdosis:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 3d3962f The changes in this PR will be included in the next version bump. This PR includes changesets to release 72 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for tiptap-embed ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in the InlineMath extension where the input rule for $$latex$$ syntax was consuming the character immediately before the opening $$. The fix changes the regular expression from using a capturing group (^|[^$]) to a negative lookbehind assertion (?<!$).
Changes:
- Modified the InlineMath input rule regex to use negative lookbehind instead of a capturing group
- Added a changeset documenting the fix
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/extension-mathematics/src/extensions/InlineMath.ts | Updated regex pattern in addInputRules() from capturing group to negative lookbehind |
| .changeset/yellow-bags-pump.md | Added changeset for the bug fix with minor version bump |
Comments suppressed due to low confidence (1)
packages/extension-mathematics/src/extensions/InlineMath.ts:233
- This bug fix changes user-visible behavior and should include a test to prevent regression. Other extensions in this codebase have unit tests (e.g.,
packages/extension-bold/__tests__/bold.spec.ts). Consider adding a test that verifies the input rule correctly matches$$latex$$without consuming the preceding character, for example by testing thatHello $$\sigma$$produces the expected result with proper spacing preserved.
new InputRule({
find: /(?<!$)(\$\$([^$\n]+?)\$\$)(?!\$)/,
handler: ({ state, range, match }) => {
const latex = match[3]
const { tr } = state
const start = range.from
const end = range.to
tr.replaceWith(start, end, this.type.create({ latex }))
},
}),
]
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
Is there a related issue for this? |
|
One thing that worries me is a negative lookup. While Safari catched up and supports them since 2023 I wonder if we should keep this information on the docs. Not sure though. |
Sorry, I did not create one due to the simplicity of the change. Would you like me to create a corresponding issue? |
Hmm, good point. We of course do not need to use negative lookbehinds. We could capture the previous character and insert content that re-adds the character in a generic |
Changes Overview
Changed the InlineMath extension's input rule to prevent it from consuming the preceding character to a
$$latex$$expression.For example, the string
Hello $$\sigma$$would be transformed into →Helloσ(with the space deleted).Implementation Approach
We can utilize a fixed-width negative lookbehind that validates the preceding character is not a $-sign. This mimics the negative lookahead that's used at the end of the regular expression.
Testing Done
Change is small enough to not require tests.
Verification Steps
Build the editor. Utilize the mathematics extension and verify that
$$somelatex$$does not replace the preceding character.Additional Notes
Checklist
(N/A here)Related Issues