-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathcode-highlight.ts
More file actions
341 lines (304 loc) · 9.79 KB
/
code-highlight.ts
File metadata and controls
341 lines (304 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import { isBuiltin } from 'node:module'
// File extension to language mapping
const EXTENSION_MAP: Record<string, string> = {
// JavaScript/TypeScript
js: 'javascript',
mjs: 'javascript',
cjs: 'javascript',
ts: 'typescript',
mts: 'typescript',
cts: 'typescript',
jsx: 'jsx',
tsx: 'tsx',
// Web
html: 'html',
htm: 'html',
css: 'css',
scss: 'scss',
sass: 'scss',
less: 'less',
vue: 'vue',
svelte: 'svelte',
astro: 'astro',
gjs: 'glimmer-js',
gts: 'glimmer-ts',
// Data formats
json: 'json',
jsonc: 'jsonc',
json5: 'jsonc',
yaml: 'yaml',
yml: 'yaml',
toml: 'toml',
xml: 'xml',
svg: 'xml',
// Shell
sh: 'bash',
bash: 'bash',
zsh: 'bash',
fish: 'bash',
// Docs
md: 'markdown',
mdx: 'markdown',
markdown: 'markdown',
// Other languages
py: 'python',
rs: 'rust',
go: 'go',
sql: 'sql',
graphql: 'graphql',
gql: 'graphql',
diff: 'diff',
patch: 'diff',
}
// Special filenames that have specific languages
const FILENAME_MAP: Record<string, string> = {
'.gitignore': 'bash',
'.npmignore': 'bash',
'.editorconfig': 'toml',
'.prettierrc': 'json',
'.eslintrc': 'json',
'tsconfig.json': 'jsonc',
'jsconfig.json': 'jsonc',
'package.json': 'json',
'package-lock.json': 'json',
'pnpm-lock.yaml': 'yaml',
'yarn.lock': 'yaml',
'Makefile': 'bash',
'Dockerfile': 'bash',
'LICENSE': 'text',
'CHANGELOG': 'markdown',
'CHANGELOG.md': 'markdown',
'README': 'markdown',
'README.md': 'markdown',
'README.markdown': 'markdown',
}
/**
* Determine the language for syntax highlighting based on file path
*/
export function getLanguageFromPath(filePath: string): string {
const filename = filePath.split('/').pop() || ''
// Check for exact filename match first
if (FILENAME_MAP[filename]) {
return FILENAME_MAP[filename]
}
// Then check extension
const ext = filename.split('.').pop()?.toLowerCase() || ''
return EXTENSION_MAP[ext] || 'text'
}
/**
* Check if a module specifier is an npm package (not a relative/absolute path or Node built-in)
*/
function isNpmPackage(specifier: string): boolean {
// Remove quotes
const pkg = specifier.replace(/^['"]|['"]$/g, '').trim()
// Relative or absolute paths
if (pkg.startsWith('.') || pkg.startsWith('/')) return false
// Node built-ins with node: prefix
if (pkg.startsWith('node:')) return false
// Node built-ins without prefix
if (isBuiltin(pkg)) return false
// Empty
if (!pkg) return false
return true
}
/**
* Extract the package name from a module specifier (handles scoped packages and subpaths)
*/
function getPackageName(specifier: string): string {
const pkg = specifier.replace(/^['"]|['"]$/g, '').trim()
// Scoped package: @scope/name or @scope/name/subpath
if (pkg.startsWith('@')) {
const parts = pkg.split('/')
if (parts[0] && parts[1]) {
return `${parts[0]}/${parts[1]}`
}
}
// Regular package: name or name/subpath
const firstSlash = pkg.indexOf('/')
if (firstSlash > 0) {
return pkg.substring(0, firstSlash)
}
return pkg
}
/**
* Resolved dependency info for linking imports to specific versions
*/
export interface ResolvedDependency {
version: string
}
/**
* Map of package name to resolved version for import linking
*/
export type DependencyVersions = Record<string, ResolvedDependency>
/**
* Function to resolve relative imports to URLs
*/
export type RelativeImportResolver = (specifier: string) => string | null
interface LinkifyOptions {
dependencies?: DependencyVersions
resolveRelative?: RelativeImportResolver
}
/**
* Make import/export module specifiers clickable links to package code browser.
* Handles:
* - import ... from 'package'
* - export ... from 'package'
* - import 'package' (side-effect imports)
* - require('package')
* - import('package') - dynamic imports
* - Relative imports (./foo, ../bar) when resolver is provided
*
* @param html - The HTML to process
* @param options - Dependencies map and optional relative import resolver
*/
export function linkifyModuleSpecifiers(html: string, options?: LinkifyOptions): string {
const { dependencies, resolveRelative } = options ?? {}
const getHref = (moduleSpecifier: string): string | null => {
const cleanSpec = moduleSpecifier.replace(/^['"]|['"]$/g, '').trim()
// Try relative import resolution first
if (cleanSpec.startsWith('.') && resolveRelative) {
return resolveRelative(moduleSpecifier)
}
if (
(cleanSpec.startsWith('#') || cleanSpec.startsWith('~') || cleanSpec.startsWith('@/')) &&
resolveRelative
) {
return resolveRelative(moduleSpecifier)
}
// Not a relative import - check if it's an npm package
if (!isNpmPackage(moduleSpecifier)) {
return null
}
const packageName = getPackageName(moduleSpecifier)
const dep = dependencies?.[packageName]
if (dep) {
// Link to code browser with resolved version
return `/package-code/${packageName}/v/${dep.version}`
}
// Fall back to package page if not a known dependency
return `/package/${packageName}`
}
// Match: from keyword span followed by string span containing module specifier
// Pattern: <span style="...">from</span><span style="..."> 'module'</span>
let result = html.replace(
/(<span[^>]*> ?from<\/span>)(<span[^>]*>) (['"][^'"]+['"])<\/span>/g,
(match, fromSpan, stringSpanOpen, moduleSpecifier) => {
const href = getHref(moduleSpecifier)
if (!href) return match
return `${fromSpan}${stringSpanOpen} <a href="${href}" class="import-link">${moduleSpecifier}</a></span>`
},
)
// Match: side-effect imports like `import 'package'`
// Pattern: <span>import</span><span> 'module'</span>
// But NOT: import ... from, import(, or import {
result = result.replace(
/(<span[^>]*>import<\/span>)(<span[^>]*>) (['"][^'"]+['"])<\/span>/g,
(match, importSpan, stringSpanOpen, moduleSpecifier) => {
const href = getHref(moduleSpecifier)
if (!href) return match
return `${importSpan}${stringSpanOpen} <a href="${href}" class="import-link">${moduleSpecifier}</a></span>`
},
)
// Match: require( or import( followed by string
// Pattern: <span> require</span><span>(</span><span>'module'</span>
// or: <span>import</span><span>(</span><span>'module'</span>
// Note: require often has a leading space in the span from Shiki
result = result.replace(
/(<span[^>]*>)(\s*)(require|import)(<\/span>)(<span[^>]*>\(<\/span>)(<span[^>]*>)(['"][^'"]+['"])<\/span>/g,
(
match,
spanOpen,
whitespace,
keyword,
spanClose,
parenSpan,
stringSpanOpen,
moduleSpecifier,
) => {
const href = getHref(moduleSpecifier)
if (!href) return match
return `${spanOpen}${whitespace}${keyword}${spanClose}${parenSpan}${stringSpanOpen}<a href="${href}" class="import-link">${moduleSpecifier}</a></span>`
},
)
return result
}
// Languages that support import/export statements
const IMPORT_LANGUAGES = new Set([
'javascript',
'typescript',
'jsx',
'tsx',
'vue',
'svelte',
'astro',
])
export interface HighlightOptions {
/** Map of dependency names to resolved versions for import linking */
dependencies?: DependencyVersions
/** Resolver function for relative imports (./foo, ../bar) */
resolveRelative?: RelativeImportResolver
}
/**
* Highlight code using Shiki with line-by-line output for line highlighting.
* Each line is wrapped in a span.line for individual line highlighting.
*/
export async function highlightCode(
code: string,
language: string,
options?: HighlightOptions,
): Promise<string> {
const shiki = await getShikiHighlighter()
const loadedLangs = shiki.getLoadedLanguages()
// Use Shiki if language is loaded
if (loadedLangs.includes(language as never)) {
try {
let html = shiki.codeToHtml(code, {
lang: language,
themes: { light: 'github-light', dark: 'github-dark' },
defaultColor: 'dark',
})
// Shiki doesn't encode > in text content (e.g., arrow functions)
html = escapeRawGt(html)
// Make import statements clickable for JS/TS languages
if (IMPORT_LANGUAGES.has(language)) {
html = linkifyModuleSpecifiers(html, {
dependencies: options?.dependencies,
resolveRelative: options?.resolveRelative,
})
}
// Check if Shiki already outputs .line spans (newer versions do)
if (html.includes('<span class="line">')) {
// Shiki already wraps lines, but they're separated by newlines
// We need to remove the newlines since display:block handles line breaks
// Replace newlines between </span> and <span class="line"> with nothing
return html.replace(/<\/span>\n<span class="line">/g, '</span><span class="line">')
}
// Older Shiki without .line spans - wrap manually
const codeMatch = html.match(/<code[^>]*>([\s\S]*)<\/code>/)
if (codeMatch?.[1]) {
const codeContent = codeMatch[1]
const lines = codeContent.split('\n')
const wrappedLines = lines
.map((line: string, i: number) => {
if (i === lines.length - 1 && line === '') return null
return `<span class="line">${line}</span>`
})
.filter((line: string | null): line is string => line !== null)
.join('')
return html.replace(codeMatch[1], wrappedLines)
}
return html
} catch {
// Fall back to plain
}
}
// Plain code for unknown languages - also wrap lines
const lines = code.split('\n')
const wrappedLines = lines
.map(line => {
const escaped = line.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
return `<span class="line">${escaped}</span>`
})
.join('') // No newlines - display:block handles it
return `<pre class="shiki github-dark"><code>${wrappedLines}</code></pre>`
}