-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze-links.mjs
More file actions
62 lines (50 loc) · 1.72 KB
/
analyze-links.mjs
File metadata and controls
62 lines (50 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import fs from 'fs/promises';
import path from 'path';
async function getMarkdownFiles(dir) {
const files = [];
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
files.push(...(await getMarkdownFiles(fullPath)));
} else if (entry.name.endsWith('.md')) {
files.push(fullPath);
}
}
return files;
}
async function analyzeAllLinks() {
const CONTENT_DIR = './content/posts';
const markdownFiles = await getMarkdownFiles(CONTENT_DIR);
let blogcardCount = 0;
const otherHttpLinks = [];
for (const filePath of markdownFiles) {
const content = await fs.readFile(filePath, 'utf-8');
const linkPattern = /\[([^\]]+)\]\(([^)]+)\)/g;
let match;
while ((match = linkPattern.exec(content)) !== null) {
const text = match[1];
const url = match[2];
if (url.startsWith('http')) {
const normalizedText = text.replace(/^https?:\/\//, '').replace(/\/$/, '');
const normalizedUrl = url.replace(/^https?:\/\//, '').replace(/\/$/, '');
if (normalizedText === normalizedUrl) {
blogcardCount++;
} else {
otherHttpLinks.push({text, url, file: filePath});
}
}
}
}
console.log('BLOGCARD URLs (detected): ' + blogcardCount);
console.log('OTHER HTTP LINKS: ' + otherHttpLinks.length);
console.log('');
console.log('=== SAMPLE OTHER LINKS ===');
otherHttpLinks.slice(0, 20).forEach(item => {
console.log('TEXT: ' + item.text);
console.log('URL: ' + item.url);
console.log('FILE: ' + item.file);
console.log('');
});
}
analyzeAllLinks().catch(console.error);