Skip to content

Commit 3dd4f34

Browse files
committed
test: add test coverage for linkifyImports
1 parent fdc2435 commit 3dd4f34

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

server/utils/code-highlight.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ interface LinkifyOptions {
167167
* @param html - The HTML to process
168168
* @param options - Dependencies map and optional relative import resolver
169169
*/
170-
function linkifyModuleSpecifiers(html: string, options?: LinkifyOptions): string {
170+
export function linkifyModuleSpecifiers(html: string, options?: LinkifyOptions): string {
171171
const { dependencies, resolveRelative } = options ?? {}
172172

173173
const getHref = (moduleSpecifier: string): string | null => {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { linkifyModuleSpecifiers } from '../../../../server/utils/code-highlight'
3+
4+
describe('linkifyModuleSpecifiers', () => {
5+
const dependencies = {
6+
'vue': { version: '3.4.0' },
7+
'@unocss/webpack': { version: '0.65.3' },
8+
}
9+
10+
it('should linkify import ... from "package"', () => {
11+
// Shiki output for: import { ref } from "vue"
12+
const html =
13+
'<span class="line">' +
14+
'<span style="color:#F97583">import</span>' +
15+
'<span style="color:#E1E4E8"> { ref }</span>' +
16+
'<span style="color:#F97583">from</span>' +
17+
'<span style="color:#9ECBFF"> "vue"</span>' +
18+
'</span>'
19+
20+
const result = linkifyModuleSpecifiers(html, { dependencies })
21+
expect(result).toContain('<a href="/package-code/vue/v/3.4.0" class="import-link">')
22+
})
23+
24+
it('should linkify export * from "package"', () => {
25+
// Shiki output for: export * from "@unocss/webpack"
26+
// Note: Shiki puts a leading space before "from" in the same span
27+
const html =
28+
'<span class="line">' +
29+
'<span style="color:#F97583">export</span>' +
30+
'<span style="color:#E1E4E8"> *</span>' +
31+
'<span style="color:#F97583"> from</span>' +
32+
'<span style="color:#9ECBFF"> "@unocss/webpack"</span>' +
33+
'<span style="color:#E1E4E8">;</span>' +
34+
'</span>'
35+
36+
const result = linkifyModuleSpecifiers(html, { dependencies })
37+
expect(result).toContain(
38+
'<a href="/package-code/@unocss/webpack/v/0.65.3" class="import-link">',
39+
)
40+
})
41+
})

0 commit comments

Comments
 (0)