Skip to content

Commit 14fc380

Browse files
committed
Fixing root index migration and more link fixes
1 parent ad823ce commit 14fc380

File tree

1 file changed

+74
-12
lines changed

1 file changed

+74
-12
lines changed

site/scripts/convert-gitbook-to-docusaurus.js

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ const BROKEN_LINKS = {
181181
'deployments/harper-cloud/alarms.md',
182182
'deployments/harper-cloud/instance-size-hardware-specs.md',
183183
'deployments/harper-cloud/iops-impact.md',
184-
'deployments/harper-cloud/verizon-5g-wavelength-instances.md'
184+
'deployments/harper-cloud/verizon-5g-wavelength-instances.md',
185+
'content-types.md'
185186
],
186187

187188
byVersion: {
@@ -923,16 +924,34 @@ function applyVersionSpecificFixes(content, filePath, version) {
923924
// Alarms links should work correctly with harper-studio name
924925
}
925926

926-
// Add general fixes for all versions >= 4.2
927-
if (version && parseFloat(version) >= 4.2) {
927+
// Add general fixes for all versions >= 4.1
928+
if (version && parseFloat(version) >= 4.1) {
928929
// Fix broken-reference links (these should be removed as they're GitBook artifacts)
929930
content = content.replace(/\]\(broken-reference\)/g, '');
930931

931-
// Additional comprehensive fixes for all 4.2+ versions
932-
// Fix getting-started/getting-started links (should just be getting-started/)
933-
content = content.replace(/\/getting-started\/getting-started/g, '/getting-started/');
934-
content = content.replace(/\.\.\/getting-started\/getting-started/g, '../getting-started/');
935-
content = content.replace(/\.\.\/\.\.\/getting-started\/getting-started/g, '../../getting-started/');
932+
// Fix getting-started/getting-started patterns BEFORE adding ./ prefix
933+
// These patterns should just be getting-started/
934+
// Match the pattern anywhere in the link, not just in parentheses
935+
content = content.replace(/getting-started\/getting-started\.md/g, 'getting-started/');
936+
content = content.replace(/getting-started\/getting-started(?![-\w])/g, 'getting-started/');
937+
938+
// Fix logging links in administration/logging index files BEFORE adding ./ prefix
939+
if (filePath.includes('/administration/logging/') && (filePath.endsWith('/index.md') || filePath.endsWith('/README.md'))) {
940+
content = content.replace(/\]\(logging\.md\)/g, '](standard-logging.md)');
941+
content = content.replace(/\]\(logging\)/g, '](standard-logging)');
942+
modified = true;
943+
}
944+
945+
// Fix relative paths that don't start with ./ or ../ or / or http
946+
// This ensures all relative links are properly formatted for Docusaurus
947+
content = content.replace(/\]\(([^.\/\#\)][^:)]*)\)/g, (match, path) => {
948+
// Skip if it's an external link or anchor
949+
if (path.includes('://') || path.startsWith('http')) {
950+
return match;
951+
}
952+
modified = true;
953+
return `](./${path})`;
954+
});
936955

937956
// Fix double administration paths
938957
content = content.replace(/\/administration\/administration\//g, '/administration/');
@@ -1201,6 +1220,10 @@ function fixLinks(content, filePath, version) {
12011220
return `${marker}[${linkText}](standard-logging.md)`;
12021221
});
12031222
}
1223+
// Also handle the case where .md was already removed and ./ was added
1224+
content = content.replace(/\]\(\.\/logging\)/g, '](./standard-logging)');
1225+
// And handle case where just 'logging' without .md
1226+
content = content.replace(/\]\(logging\)/g, '](standard-logging)');
12041227
}
12051228

12061229
// Fix links to logging/logging.md throughout all files (should be logging/standard-logging.md)
@@ -1212,11 +1235,28 @@ function fixLinks(content, filePath, version) {
12121235
});
12131236
}
12141237

1215-
// Remove .md extensions from internal links
1216-
content = content.replace(/(\[[^\]]+\]\()([^)]+)(\.md)([)#])/g, (match, prefix, path, ext, suffix) => {
1217-
if (!path.includes('http://') && !path.includes('https://')) {
1238+
// Remove .md extensions from internal links - comprehensive fix
1239+
// This handles all markdown link patterns with .md extensions
1240+
content = content.replace(/(\[[^\]]+\]\()([^)]+\.md)([\)#])/g, (match, prefix, pathWithExt, suffix) => {
1241+
// Only process if it's not an external link
1242+
if (!pathWithExt.includes('http://') && !pathWithExt.includes('https://')) {
12181243
modified = true;
1219-
return prefix + path + suffix;
1244+
// Remove the .md extension
1245+
const pathWithoutExt = pathWithExt.replace(/\.md$/, '');
1246+
return prefix + pathWithoutExt + suffix;
1247+
}
1248+
return match;
1249+
});
1250+
1251+
// Also handle .md extensions in HTML links within tables (GitBook specific)
1252+
// This pattern catches href attributes in <a> tags
1253+
content = content.replace(/(<a\s+[^>]*href=")([^"]+\.md)(")/g, (match, prefix, pathWithExt, suffix) => {
1254+
// Only process if it's not an external link
1255+
if (!pathWithExt.includes('http://') && !pathWithExt.includes('https://')) {
1256+
modified = true;
1257+
// Remove the .md extension
1258+
const pathWithoutExt = pathWithExt.replace(/\.md$/, '');
1259+
return prefix + pathWithoutExt + suffix;
12201260
}
12211261
return match;
12221262
});
@@ -1644,11 +1684,33 @@ function processDirectory(dirPath, targetDirPath, docsDir = dirPath, outputDir =
16441684
// Create category file if needed
16451685
createCategoryFile(dirPath, targetDirPath);
16461686

1687+
// Check if we have both index.md and README.md, and prefer README if index is blank
1688+
const hasIndex = entries.some(e => e.name === 'index.md');
1689+
const hasReadme = entries.some(e => e.name === 'README.md');
1690+
1691+
if (hasIndex && hasReadme) {
1692+
const indexPath = path.join(dirPath, 'index.md');
1693+
const indexContent = fs.readFileSync(indexPath, 'utf8');
1694+
1695+
// Check if index.md is essentially blank (contains only the comment about blank index)
1696+
if (indexContent.includes('blank index file needed to avoid "index" being added to URLs') ||
1697+
indexContent.trim().length < 50) {
1698+
// Remove the blank index.md so README.md will be used instead
1699+
fs.unlinkSync(indexPath);
1700+
console.log(` Removed blank index.md in favor of README.md in ${dirPath}`);
1701+
}
1702+
}
1703+
16471704
// Process entries
16481705
for (const entry of entries) {
16491706
let entryName = entry.name;
16501707
let actualSourcePath = path.join(dirPath, entry.name);
16511708

1709+
// Skip if this was the blank index.md we just removed
1710+
if (!fs.existsSync(actualSourcePath)) {
1711+
continue;
1712+
}
1713+
16521714
// Fix directories starting with numbers (webpack issue)
16531715
// Rename them to prefix with 'v' (e.g., '1.alby' -> 'v1-alby')
16541716
if (entry.isDirectory() && /^\d/.test(entry.name)) {

0 commit comments

Comments
 (0)