1
1
import { contractsVersionTag } from "@openzeppelin/wizard-cairo/src" ;
2
2
3
3
export function injectHyperlinks ( code : string ) {
4
- const importRegex = / u s e < \/ s p a n > ( o p e n z e p p e l i n ) : : ( [ ^ \s ] * ) ; / g
4
+ const importRegex = / u s e < \/ s p a n > ( o p e n z e p p e l i n ) : : ( [ ^ A - Z ] * ) ( : : [ a - z A - Z 0 - 9 ] + | : : { ) / g
5
5
let result = code ;
6
6
let match = importRegex . exec ( code ) ;
7
7
while ( match != null ) {
8
- const [ line , libraryPrefix , libraryPath ] = match ;
9
- if ( line !== undefined && libraryPrefix !== undefined && libraryPath !== undefined ) {
8
+ const [ line , libraryPrefix , libraryPath , suffix ] = match ;
9
+ if ( line !== undefined && libraryPrefix !== undefined && libraryPath !== undefined && suffix !== undefined ) {
10
10
const githubPrefix = `https://github.com/OpenZeppelin/cairo-contracts/blob/${ contractsVersionTag } /packages/` ;
11
11
12
12
let libraryPathSegments = libraryPath . split ( '::' ) ;
13
13
libraryPathSegments . splice ( 1 , 0 , 'src' ) ;
14
14
15
- removeComponentName ( libraryPathSegments ) ;
16
-
17
15
if ( libraryPathSegments !== undefined && libraryPathSegments . length > 0 ) {
18
- const replacedImportLine = `use<\/span> <a class="import-link" href='${ githubPrefix } ${ libraryPathSegments . join ( '/' ) } .cairo' target='_blank' rel='noopener noreferrer'>${ libraryPrefix } ::${ libraryPath } </a>;` ;
19
- result = result . replace ( line , replacedImportLine ) ;
16
+ let replacement ;
17
+ if ( suffix === '::{' ) {
18
+ // Multiple components are imported, so remove components and link to the parent .cairo file
19
+ replacement = `use<\/span> <a class="import-link" href='${ githubPrefix } ${ libraryPathSegments . join ( '/' ) } .cairo' target='_blank' rel='noopener noreferrer'>${ libraryPrefix } ::${ libraryPath } </a>${ suffix } ` ; // Exclude suffix from link
20
+ } else {
21
+ // Single component is imported
22
+ // If a mapping exists, link to the mapped file, otherwise remove the component and link to the parent .cairo file
23
+ const componentName = suffix . substring ( 2 , suffix . length ) ;
24
+ const mapping = componentMappings [ componentName ] ;
25
+ const urlSuffix = mapping ? `/${ mapping } .cairo` : '.cairo' ;
26
+ replacement = `use<\/span> <a class="import-link" href='${ githubPrefix } ${ libraryPathSegments . join ( '/' ) } ${ urlSuffix } ' target='_blank' rel='noopener noreferrer'>${ libraryPrefix } ::${ libraryPath } ${ suffix } </a>` ; // Include suffix (component) in link
27
+ }
28
+
29
+ result = result . replace ( line , replacement ) ;
20
30
}
21
31
}
22
32
match = importRegex . exec ( code ) ;
@@ -28,13 +38,3 @@ const componentMappings: { [key: string]: string } = {
28
38
'AccountComponent' : 'account' ,
29
39
'UpgradeableComponent' : 'upgradeable' ,
30
40
} as const ;
31
-
32
- function removeComponentName ( libraryPathSegments : Array < string > ) {
33
- const lastItem = libraryPathSegments [ libraryPathSegments . length - 1 ] ;
34
- if ( lastItem !== undefined && componentMappings [ lastItem ] !== undefined ) {
35
- // Replace component name with the name of its .cairo file
36
- libraryPathSegments . splice ( - 1 , 1 , componentMappings [ lastItem ] as string ) ;
37
- } else {
38
- libraryPathSegments . pop ( ) ;
39
- }
40
- }
0 commit comments