Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
isahers1 committed Aug 29, 2024
1 parent 0b89c62 commit c1a468c
Show file tree
Hide file tree
Showing 3 changed files with 523 additions and 30 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"cheminfo-types": "^1.4.0",
"d3": "^7.9.0",
"esm-hook": "^0.1.4",
"linkinator": "^4.0.5",
"markdown-link-check": "^3.10.3",
"readline": "^1.3.0",
"release-it": "^17.6.0",
Expand Down
55 changes: 35 additions & 20 deletions scripts/check-links.cjs
Original file line number Diff line number Diff line change
@@ -1,45 +1,60 @@
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const { LinkChecker } = require('linkinator');

// Function to find all .ipynb files in the given directory
function findIpynbFiles(dir) {
const ignorePatterns = [
'https://(api|web)\\.smith\\.langchain\\.com/.*',
'https://x\\.com/.*'
];

async function findIpynbFiles(dir) {
const files = await fs.promises.readdir(dir);
let results = [];
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
const stat = await fs.promises.stat(filePath);
if (stat.isDirectory()) {
results = results.concat(findIpynbFiles(filePath));
results = results.concat(await findIpynbFiles(filePath));
} else if (path.extname(file) === '.ipynb') {
results.push(filePath);
}
}
return results;
}

// Main function to check links
function checkLinks() {
const ignorePatterns = [
'https://(api|web)\\.smith\\.langchain\\.com/.*',
'https://x\\.com/.*'
];

const ipynbFiles = findIpynbFiles('.');
async function checkLinks() {
const ipynbFiles = await findIpynbFiles('.');
console.log('Found .ipynb files:', ipynbFiles);

const checker = new LinkChecker();

checker.on('link', (result) => {
console.log(`${result.status} ${result.url}`);
});

for (const file of ipynbFiles) {
console.log(`Checking links in ${file}`);
try {
execSync(`yarn run linkinator ${file} ${ignorePatterns.map(pattern => `--skip "${pattern}"`).join(' ')}`, { stdio: 'inherit' });
} catch (error) {
if (error.status === 5) {
console.log('Broken links found, but continuing...');
const result = await checker.check({
path: file,
recurse: false,
linksToSkip: ignorePatterns,
});

if (result.passed) {
console.log(`All links in ${file} are valid.`);
} else {
throw error;
console.error(`Broken links found in ${file}.`);
process.exitCode = 1;
}
} catch (error) {
console.error(`Error checking links in ${file}:`, error);
process.exitCode = 1;
}
}
}

checkLinks();
checkLinks().catch(error => {
console.error('An error occurred:', error);
process.exitCode = 1;
});
Loading

0 comments on commit c1a468c

Please sign in to comment.