-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve highlighted function (#4967)
- Loading branch information
1 parent
dcf2e9f
commit 6bfdb5f
Showing
4 changed files
with
33 additions
and
19 deletions.
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,25 @@ | ||
import type { Colors } from '@vitest/utils' | ||
import { highlight } from '@vitest/utils' | ||
import { extname } from 'pathe' | ||
import c from 'picocolors' | ||
|
||
const HIGHLIGHT_SUPPORTED_EXTS = new Set(['js', 'ts'].flatMap(lang => [ | ||
`.${lang}`, | ||
`.m${lang}`, | ||
`.c${lang}`, | ||
`.${lang}x`, | ||
`.m${lang}x`, | ||
`.c${lang}x`, | ||
])) | ||
|
||
export function highlightCode( | ||
id: string, | ||
source: string, | ||
colors?: Colors, | ||
) { | ||
const ext = extname(id) | ||
if (!HIGHLIGHT_SUPPORTED_EXTS.has(ext)) | ||
return source | ||
const isJsx = ext.endsWith('x') | ||
return highlight(source, { jsx: isJsx, colors: colors || c }) | ||
} |
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