-
-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
breaking(transformers): redesign notation transformers #835
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for shiki-matsu ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
✅ Deploy Preview for shiki-next ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
if (comment.info[1].length === 0) | ||
continue | ||
|
||
const isLineCommentOnly = comment.line.children.length === (comment.isJsxStyle ? 3 : 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it indicates the line contains only the comment itself, like:
// [!code highlight]
console.log("test")
so we will highlight the next line, and remove the original line when content
became empty.
@@ -24,14 +24,14 @@ export function transformerNotationMap( | |||
|
|||
return createCommentNotationTransformer( | |||
name, | |||
new RegExp(`\\s*(?://|/\\*|<!--|#|--|%{1,2}|;{1,2}|"|')\\s+\\[!code (${Object.keys(classMap).map(escapeRegExp).join('|')})(:\\d+)?\\]\\s*(?:\\*/|-->)?\\s*$`), | |||
new RegExp(`\\s*\\[!code (${Object.keys(classMap).map(escapeRegExp).join('|')})(:\\d+)?\\]`), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we no longer need to provide the regex for matching comments, it is better given that the name createCommentNotationTransformer
should handles this too.
The core changes are basically done. I kept the failing unit tests for future experiments about backward compatibility, I'm thinking about a |
Description
This PR aims to redesign notation transformer utility and its usage to improve performance and make it more intuitive.
Changes
data
property ofcode
nodes.New Syntax
JSX
Breaking
It only looks for the last few tokens of line to scan comments:
It doesn't need multiple
//
to chain notations:Edge cases
for usage like:
line 2-3 will be highlighted, but the original comment is kept.
Backward Compatibility
Added a
legacy
option to force legacy behaviors:Linked Issues
#619 #770
Additional context
this PR will introduce breaking changes, I currently made a separate package to avoid the problems. Backward compatibility is possible (edit: supported), but it takes extra care and further discussion.
code implementations:
the
createCommentNotationTransformer
parses comment withparseComments
and cache the result.each parsed comment object contains related hast nodes, and
info
:remove nodes when
content
became empty.transformers using
createCommentNotationTransformer
still provides a regex, but the regex no longer need to match the comment