@@ -4,6 +4,7 @@ import {invertFileFolding} from './file-fold.js';
4
4
import { createTippy } from '../modules/tippy.js' ;
5
5
import { clippie } from 'clippie' ;
6
6
import { toAbsoluteUrl } from '../utils.js' ;
7
+ import { getFileViewContent , getFileViewFileName } from '../utils/misc.js' ;
7
8
8
9
export const singleAnchorRegex = / ^ # ( L | n ) ( [ 1 - 9 ] [ 0 - 9 ] * ) $ / ;
9
10
export const rangeAnchorRegex = / ^ # ( L [ 1 - 9 ] [ 0 - 9 ] * ) - ( L [ 1 - 9 ] [ 0 - 9 ] * ) $ / ;
@@ -120,6 +121,44 @@ function showLineButton() {
120
121
} ) ;
121
122
}
122
123
124
+ function initFilePostProcess ( ) {
125
+ const fileName = getFileViewFileName ( ) ;
126
+ if ( fileName === 'package.json' ) {
127
+ let data ;
128
+ try {
129
+ data = JSON . parse ( getFileViewContent ( ) ) ;
130
+ } catch {
131
+ return ;
132
+ }
133
+
134
+ const packages = new Set ( ) ;
135
+ for ( const key of [
136
+ 'dependencies' ,
137
+ 'devDependencies' ,
138
+ 'optionalDependencies' ,
139
+ 'peerDependencies' ,
140
+ ] ) {
141
+ for ( const packageName of Object . keys ( data ?. [ key ] || { } ) ) {
142
+ packages . add ( packageName ) ;
143
+ }
144
+ }
145
+
146
+ // match chroma NameTag token to detect JSON object keys
147
+ for ( const el of document . querySelectorAll ( '.code-inner .nt' ) ) {
148
+ const jsonKey = el . textContent . replace ( / ^ " ( .* ) " $ / , '$1' ) ;
149
+ if ( packages . has ( jsonKey ) ) {
150
+ const a = document . createElement ( 'a' ) ;
151
+ a . textContent = jsonKey ;
152
+ a . href = `https://www.npmjs.com/package/${ jsonKey } ` ;
153
+ a . target = '_blank' ;
154
+ a . rel = 'noopener noreferrer nofollow' ;
155
+ el . textContent = '' ;
156
+ el . append ( '"' , a , '"' ) ;
157
+ }
158
+ }
159
+ }
160
+ }
161
+
123
162
export function initRepoCodeView ( ) {
124
163
if ( $ ( '.code-view .lines-num' ) . length > 0 ) {
125
164
$ ( document ) . on ( 'click' , '.lines-num span' , function ( e ) {
@@ -197,4 +236,5 @@ export function initRepoCodeView() {
197
236
$ ( document ) . on ( 'click' , '.copy-line-permalink' , async ( { currentTarget} ) => {
198
237
await clippie ( toAbsoluteUrl ( currentTarget . getAttribute ( 'data-url' ) ) ) ;
199
238
} ) ;
239
+ initFilePostProcess ( )
200
240
}
0 commit comments