Skip to content

Commit

Permalink
Fixed broken referenced link (cardano-foundation#1229)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivam-1M committed May 17, 2024
1 parent fc0c48c commit 06075de
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions scripts/cip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ const path_name = path.basename(__filename);

// Download markdown resources
const processCIPContentAsync = async (cip_name: string, content: string) => {
// Finding any reference links in the content and replacing to relative links where necessary
// to fix broken links in the CIPs
const referenceLinks = identifyReferenceLinks(content);
if(referenceLinks) {
await Promise.all(
referenceLinks.map(async (link) => {
if(link.url.indexOf("./") == 0 || link.url.indexOf("../") == 0) {

console.log(`Found reference links in CIP ${cip_name}:`);
console.log(`WARNING: Reference link ${link.reference} in CIP ${cip_name} is an relative link: ${link.url}.`);

// Rewrite link to static folder
content = content.replace(
`[${link.reference}]`,
`[${link.reference}](${link.url})`
);
}
})
);
}

const cip_resource = content.match(cip_regex);
if (cip_resource) {
await Promise.all(
Expand Down Expand Up @@ -172,6 +193,23 @@ const getDocTag = (content: string, tag_name: string) => {
return content.match(new RegExp(`(?<=${tag_name}: ).*`, ""));
};

const identifyReferenceLinks = (content: string) => {
// Regular expression to match reference-style links
const referenceLinkRegex = /\[([^\]]+)\]:\s*(\S+)/g;
const matches = [];
let match;

// Loop through all matches and push them into the matches array
while ((match = referenceLinkRegex.exec(content)) !== null) {
matches.push({
reference: match[1], // The reference name
url: match[2] // The URL
});
}

return matches;
};

const main = async () => {
console.log("CIP Content Downloading...");
// Use https://raw.githubusercontent.com/cardano-foundation/CIPs/master/README.md as entry point to get URLs
Expand Down

0 comments on commit 06075de

Please sign in to comment.