Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cairo links when multiple components are imported #427

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions packages/ui/src/cairo/inject-hyperlinks.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import { contractsVersionTag } from "@openzeppelin/wizard-cairo/src";

export function injectHyperlinks(code: string) {
const importRegex = /use<\/span> (openzeppelin)::([^\s]*);/g
const importRegex = /use<\/span> (openzeppelin)::([^A-Z]*)(::[a-zA-Z0-9]+|::{)/g
let result = code;
let match = importRegex.exec(code);
while (match != null) {
const [line, libraryPrefix, libraryPath] = match;
if (line !== undefined && libraryPrefix !== undefined && libraryPath !== undefined) {
const [line, libraryPrefix, libraryPath, suffix] = match;
if (line !== undefined && libraryPrefix !== undefined && libraryPath !== undefined && suffix !== undefined) {
const githubPrefix = `https://github.com/OpenZeppelin/cairo-contracts/blob/${contractsVersionTag}/packages/`;

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

removeComponentName(libraryPathSegments);

if (libraryPathSegments !== undefined && libraryPathSegments.length > 0) {
const replacedImportLine = `use<\/span> <a class="import-link" href='${githubPrefix}${libraryPathSegments.join('/')}.cairo' target='_blank' rel='noopener noreferrer'>${libraryPrefix}::${libraryPath}</a>;`;
result = result.replace(line, replacedImportLine);
let replacement;
if (suffix === '::{') {
// Multiple components are imported, so remove components and link to the parent .cairo file
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
} else {
// Single component is imported
// If a mapping exists, link to the mapped file, otherwise remove the component and link to the parent .cairo file
const componentName = suffix.substring(2, suffix.length);
const mapping = componentMappings[componentName];
const urlSuffix = mapping ? `/${mapping}.cairo` : '.cairo';
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
}

result = result.replace(line, replacement);
}
}
match = importRegex.exec(code);
Expand All @@ -28,13 +38,3 @@ const componentMappings: { [key: string]: string } = {
'AccountComponent': 'account',
'UpgradeableComponent': 'upgradeable',
} as const;

function removeComponentName(libraryPathSegments: Array<string>) {
const lastItem = libraryPathSegments[libraryPathSegments.length - 1];
if (lastItem !== undefined && componentMappings[lastItem] !== undefined) {
// Replace component name with the name of its .cairo file
libraryPathSegments.splice(-1, 1, componentMappings[lastItem] as string);
} else {
libraryPathSegments.pop();
}
}
Loading