-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadd_links.ts
100 lines (82 loc) · 3.2 KB
/
add_links.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { promises as fs } from 'fs';
import path from 'path';
const summariesDir = 'summaries';
const linkifiersFile = 'linkifiers.csv';
const repoToPackageFile = './repository_to_package.json';
interface Linkifier {
term: string;
packageId: string;
}
interface RepoToPackage {
[key: string]: string;
}
async function addLinks() {
console.log('Starting addLinks process...');
// Load linkifiers
console.log('Loading linkifiers...');
const linkifiersContent = await fs.readFile(linkifiersFile, 'utf-8');
const linkifiers: Linkifier[] = linkifiersContent.split('\n')
.filter(line => line.trim() !== '')
.map(line => {
const [term, packageId] = line.split(',');
return { term, packageId };
});
// Sort linkifiers by term length (descending) to prioritize longer matches
linkifiers.sort((a, b) => b.term.length - a.term.length);
console.log(`Loaded ${linkifiers.length} linkifiers`);
// Load repository to package mapping
console.log('Loading repository to package mapping...');
const repoToPackageContent = await fs.readFile(repoToPackageFile, 'utf-8');
const repoToPackage: RepoToPackage = JSON.parse(repoToPackageContent);
const packageToRepo = Object.entries(repoToPackage).reduce((acc, [repo, pkg]) => {
acc[pkg] = repo;
return acc;
}, {} as Record<string, string>);
// Process each summary file
const files = await fs.readdir(summariesDir);
console.log(`Found ${files.length} files in summaries directory`);
for (const file of files) {
if (path.extname(file) === '.md') {
console.log(`Processing ${file}...`);
const filePath = path.join(summariesDir, file);
let content = await fs.readFile(filePath, 'utf-8');
// Extract the current IG name from the file name
const currentIGName = path.basename(file, '.md');
// Remove existing links
content = content.replace(/\[([^\]]+)\]\(https:\/\/build\.fhir\.org\/ig\/HL7\/[^)]+\)/g, '$1');
let linkCount = 0;
const insertedUrls = new Set<string>();
for (const linkifier of linkifiers) {
if (linkifier.packageId !== currentIGName) {
const repoName = packageToRepo[linkifier.packageId] || linkifier.packageId;
const targetUrl = `https://build.fhir.org/ig/HL7/${repoName}`;
if (!insertedUrls.has(targetUrl)) {
const regex = new RegExp(`\\b${escapeRegExp(linkifier.term)}\\b(?![^\\[]*\\])`, 'g');
let hasReplaced = false;
content = content.replace(regex, (match) => {
if (!hasReplaced) {
hasReplaced = true;
linkCount++;
insertedUrls.add(targetUrl);
return `[${match}](${targetUrl})`;
}
return match;
});
}
}
}
// Write the processed content back to the file
await fs.writeFile(filePath, content);
console.log(`Processed ${file} - Added ${linkCount} links`);
}
}
console.log('Finished processing all files');
}
function escapeRegExp(string: string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
// Run the addLinks function
addLinks().catch(error => {
console.error('An error occurred:', error);
process.exit(1);
});