Skip to content

Commit cd566cb

Browse files
jhroemerserhalp
andauthored
fix: package name not detected in code view (#1711)
Co-authored-by: Philippe Serhal <philippe.serhal@gmail.com>
1 parent 85dd9fd commit cd566cb

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

server/utils/code-highlight.ts

Lines changed: 3 additions & 3 deletions
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 linkifyImports(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 => {
@@ -196,7 +196,7 @@ function linkifyImports(html: string, options?: LinkifyOptions): string {
196196
// Match: from keyword span followed by string span containing module specifier
197197
// Pattern: <span style="...">from</span><span style="..."> 'module'</span>
198198
let result = html.replace(
199-
/(<span[^>]*>from<\/span>)(<span[^>]*>) (['"][^'"]+['"])<\/span>/g,
199+
/(<span[^>]*> ?from<\/span>)(<span[^>]*>) (['"][^'"]+['"])<\/span>/g,
200200
(match, fromSpan, stringSpanOpen, moduleSpecifier) => {
201201
const href = getHref(moduleSpecifier)
202202
if (!href) return match
@@ -285,7 +285,7 @@ export async function highlightCode(
285285

286286
// Make import statements clickable for JS/TS languages
287287
if (IMPORT_LANGUAGES.has(language)) {
288-
html = linkifyImports(html, {
288+
html = linkifyModuleSpecifiers(html, {
289289
dependencies: options?.dependencies,
290290
resolveRelative: options?.resolveRelative,
291291
})
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)