Skip to content

Commit e1973f5

Browse files
authored
Add Cairo links when multiple components are imported (#427)
1 parent 84f1b3a commit e1973f5

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
import { contractsVersionTag } from "@openzeppelin/wizard-cairo/src";
22

33
export function injectHyperlinks(code: string) {
4-
const importRegex = /use<\/span> (openzeppelin)::([^\s]*);/g
4+
const importRegex = /use<\/span> (openzeppelin)::([^A-Z]*)(::[a-zA-Z0-9]+|::{)/g
55
let result = code;
66
let match = importRegex.exec(code);
77
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) {
1010
const githubPrefix = `https://github.com/OpenZeppelin/cairo-contracts/blob/${contractsVersionTag}/packages/`;
1111

1212
let libraryPathSegments = libraryPath.split('::');
1313
libraryPathSegments.splice(1, 0, 'src');
1414

15-
removeComponentName(libraryPathSegments);
16-
1715
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);
2030
}
2131
}
2232
match = importRegex.exec(code);
@@ -28,13 +38,3 @@ const componentMappings: { [key: string]: string } = {
2838
'AccountComponent': 'account',
2939
'UpgradeableComponent': 'upgradeable',
3040
} 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

Comments
 (0)