-
-
Notifications
You must be signed in to change notification settings - Fork 378
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
Open
fuma-nama
wants to merge
5
commits into
shikijs:main
Choose a base branch
from
fuma-nama:breaking-transformers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import type { Element, Text } from 'hast' | ||
import type { ShikiTransformer, ShikiTransformerContext } from 'shiki' | ||
import { legacyClearEndCommentPrefix, parseComments, type ParsedComments } from './parse-comments' | ||
|
||
export function createCommentNotationTransformer( | ||
name: string, | ||
regex: RegExp, | ||
onMatch: ( | ||
this: ShikiTransformerContext, | ||
match: string[], | ||
line: Element, | ||
commentNode: Element, | ||
lines: Element[], | ||
index: number | ||
) => boolean, | ||
legacy = false, | ||
): ShikiTransformer { | ||
return { | ||
name, | ||
code(code) { | ||
const lines = code.children.filter(i => i.type === 'element') | ||
const linesToRemove: (Element | Text)[] = [] | ||
|
||
code.data ??= {} as any | ||
const data = code.data as { | ||
_shiki_notation?: ParsedComments | ||
} | ||
|
||
data._shiki_notation ??= parseComments(lines, ['jsx', 'tsx'].includes(this.options.lang), legacy) | ||
const parsed = data._shiki_notation | ||
|
||
for (const comment of parsed) { | ||
if (comment.info[1].length === 0) | ||
continue | ||
|
||
const isLineCommentOnly = comment.line.children.length === (comment.isJsxStyle ? 3 : 1) | ||
let lineIdx = lines.indexOf(comment.line) | ||
if (isLineCommentOnly && !legacy) | ||
lineIdx++ | ||
|
||
let replaced = false | ||
comment.info[1] = comment.info[1].replace(regex, (...match) => { | ||
if (onMatch.call(this, match, comment.line, comment.token, lines, lineIdx)) { | ||
replaced = true | ||
return '' | ||
} | ||
|
||
return match[0] | ||
}) | ||
|
||
if (!replaced) | ||
continue | ||
|
||
if (legacy) { | ||
comment.info[1] = legacyClearEndCommentPrefix(comment.info[1]) | ||
} | ||
|
||
const isEmpty = comment.info[1].trim().length === 0 | ||
// ignore comment node | ||
if (isEmpty) | ||
comment.info[1] = '' | ||
|
||
if (isEmpty && isLineCommentOnly) { | ||
linesToRemove.push(comment.line) | ||
} | ||
else if (isEmpty && comment.isJsxStyle) { | ||
comment.line.children.splice(comment.line.children.indexOf(comment.token) - 1, 3) | ||
} | ||
else if (isEmpty) { | ||
comment.line.children.splice(comment.line.children.indexOf(comment.token), 1) | ||
} | ||
else { | ||
const head = comment.token.children[0] | ||
|
||
if (head.type === 'text') { | ||
head.value = comment.info.join('') | ||
} | ||
} | ||
} | ||
|
||
for (const line of linesToRemove) | ||
code.children.splice(code.children.indexOf(line), 1) | ||
}, | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import type { Element, ElementContent } from 'hast' | ||
|
||
export type ParsedComments = { | ||
line: Element | ||
token: Element | ||
info: [prefix: string, content: string, suffix?: string] | ||
isJsxStyle: boolean | ||
}[] | ||
|
||
/** | ||
* some comment formats have to be located at the end of line | ||
* hence we can skip matching them for other tokens | ||
*/ | ||
const matchers: [re: RegExp, endOfLine: boolean][] = [ | ||
[/^(<!--)(.+)(-->)$/, false], | ||
[/^(\/\*)(.+)(\*\/)$/, false], | ||
[/^(\/\/|["'#]|;{1,2}|%{1,2}|--)(.*)$/, true], | ||
/** | ||
* for multi-line comments like this | ||
*/ | ||
[/^(\*)(.+)$/, true], | ||
] | ||
|
||
/** | ||
* @param lines line tokens | ||
* @param jsx enable JSX parsing | ||
* @param legacy support legacy behaviours, force to parse all tokens. | ||
*/ | ||
export function parseComments(lines: Element[], jsx: boolean, legacy = false): ParsedComments { | ||
const out: ParsedComments = [] | ||
|
||
for (const line of lines) { | ||
const elements = line.children | ||
let start = elements.length - 1 | ||
if (legacy) | ||
start = 0 | ||
else if (jsx) | ||
// one step further for JSX as comment is inside curly brackets | ||
start = elements.length - 2 | ||
|
||
for (let i = Math.max(start, 0); i < elements.length; i++) { | ||
const token = elements[i] | ||
if (token.type !== 'element') | ||
continue | ||
const head = token.children.at(0) | ||
if (head?.type !== 'text') | ||
continue | ||
|
||
const isLast = i === elements.length - 1 | ||
const match = matchToken(head.value, isLast) | ||
if (!match) | ||
continue | ||
|
||
if (jsx && !isLast && i !== 0) { | ||
out.push({ | ||
info: match, | ||
line, | ||
token, | ||
isJsxStyle: isValue(elements[i - 1], '{') && isValue(elements[i + 1], '}'), | ||
}) | ||
} | ||
else { | ||
out.push({ | ||
info: match, | ||
line, | ||
token, | ||
isJsxStyle: false, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return out | ||
} | ||
|
||
function isValue(element: ElementContent, value: string): boolean { | ||
if (element.type !== 'element') | ||
return false | ||
const text = element.children[0] | ||
if (text.type !== 'text') | ||
return false | ||
|
||
return text.value.trim() === value | ||
} | ||
|
||
/** | ||
* @param text text value of comment node | ||
* @param isLast whether the token is located at the end of line | ||
*/ | ||
function matchToken(text: string, isLast: boolean): [prefix: string, content: string, suffix?: string] | undefined { | ||
// no leading and trailing spaces allowed for matchers | ||
// we extract the spaces | ||
let trimmed = text.trimStart() | ||
const spaceFront = text.length - trimmed.length | ||
|
||
trimmed = trimmed.trimEnd() | ||
const spaceEnd = text.length - trimmed.length - spaceFront | ||
|
||
for (const [matcher, endOfLine] of matchers) { | ||
if (endOfLine && !isLast) | ||
continue | ||
|
||
const result = matcher.exec(trimmed) | ||
if (!result) | ||
continue | ||
|
||
return [ | ||
' '.repeat(spaceFront) + result[1], | ||
result[2], | ||
result[3] ? result[3] + ' '.repeat(spaceEnd) : undefined, | ||
] | ||
} | ||
} | ||
|
||
/** | ||
* Remove empty comment prefixes at line end, e.g. `// ` | ||
*/ | ||
export function legacyClearEndCommentPrefix(text: string): string { | ||
const regex = /(?:\/\/|["'#]|;{1,2}|%{1,2}|--)(.*)$/ | ||
const result = regex.exec(text) | ||
|
||
if (result && result[1].trim().length === 0) { | ||
return text.slice(0, result.index) | ||
} | ||
|
||
return text | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
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:
so we will highlight the next line, and remove the original line when
content
became empty.