Skip to content

Commit 4104ccf

Browse files
authored
Merge pull request #216 from HarperDB/fix-root-and-more-links
Fixing root index migration and more link fixes
2 parents 1cfe5fa + 18512c3 commit 4104ccf

File tree

2 files changed

+164
-16
lines changed

2 files changed

+164
-16
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)) {

site/scripts/migrate-branches-to-versions.js

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,78 @@ function gitExec(command, options = {}) {
5858

5959
// Save current branch and changes
6060
function saveCurrentState() {
61-
const currentBranch = gitExec('rev-parse --abbrev-ref HEAD');
61+
let currentBranch = gitExec('rev-parse --abbrev-ref HEAD');
62+
const currentCommit = gitExec('rev-parse HEAD');
6263
const hasChanges = gitExec('status --porcelain');
6364

65+
// Check if we're in detached HEAD state (common in CI)
66+
if (currentBranch === 'HEAD') {
67+
console.log('Detected detached HEAD state (common in CI)');
68+
console.log(`Current commit: ${currentCommit}`);
69+
70+
// Try to find which branch we're on by checking which branches contain this commit
71+
try {
72+
const branches = gitExec(`branch -r --contains ${currentCommit}`);
73+
console.log('Branches containing current commit:', branches);
74+
75+
// In CI, we might want to use the commit hash directly
76+
currentBranch = currentCommit;
77+
console.log(`Will restore to commit: ${currentCommit}`);
78+
} catch (e) {
79+
console.log('Could not determine branch from commit');
80+
}
81+
} else {
82+
console.log(`Starting from branch: ${currentBranch}`);
83+
}
84+
85+
console.log(`Current working directory: ${process.cwd()}`);
86+
6487
if (hasChanges) {
6588
console.log('Stashing current changes...');
6689
gitExec('stash push -m "migrate-branches-to-versions temporary stash"');
6790
}
6891

69-
return { currentBranch, hasChanges: !!hasChanges };
92+
return {
93+
currentBranch,
94+
currentCommit,
95+
isDetachedHead: currentBranch === currentCommit,
96+
hasChanges: !!hasChanges,
97+
startingDir: process.cwd()
98+
};
7099
}
71100

72101
// Restore original state
73102
function restoreState(state) {
74-
console.log(`Switching back to ${state.currentBranch}...`);
75-
gitExec(`checkout ${state.currentBranch}`);
103+
console.log(`\nRestoring original state...`);
104+
const currentLocation = gitExec('rev-parse --abbrev-ref HEAD');
105+
console.log(`Current location: ${currentLocation}`);
106+
console.log(`Current directory: ${process.cwd()}`);
107+
108+
if (state.isDetachedHead) {
109+
// In CI with detached HEAD, checkout the specific commit
110+
console.log(`Restoring to commit: ${state.currentCommit}`);
111+
gitExec(`checkout ${state.currentCommit}`);
112+
113+
// Verify we're at the right commit
114+
const actualCommit = gitExec('rev-parse HEAD');
115+
if (actualCommit !== state.currentCommit) {
116+
console.error(`Warning: Expected commit ${state.currentCommit} but at ${actualCommit}`);
117+
} else {
118+
console.log(`✓ Successfully restored to commit ${state.currentCommit}`);
119+
}
120+
} else {
121+
// Normal branch checkout
122+
console.log(`Switching back to branch: ${state.currentBranch}...`);
123+
gitExec(`checkout ${state.currentBranch}`);
124+
125+
// Verify we're on the right branch
126+
const actualBranch = gitExec('rev-parse --abbrev-ref HEAD');
127+
if (actualBranch !== state.currentBranch) {
128+
console.error(`Warning: Expected to be on ${state.currentBranch} but actually on ${actualBranch}`);
129+
} else {
130+
console.log(`✓ Successfully restored to branch ${state.currentBranch}`);
131+
}
132+
}
76133

77134
if (state.hasChanges) {
78135
console.log('Restoring stashed changes...');
@@ -517,6 +574,35 @@ async function migrate() {
517574
// Restore original state
518575
restoreState(originalState);
519576

577+
// After switching back, ensure the site directory exists
578+
// In CI, switching branches might have removed it
579+
console.log(`\nChecking if site directory exists at: ${SITE_DIR}`);
580+
if (!fs.existsSync(SITE_DIR)) {
581+
console.error('\n⚠️ Warning: Site directory was removed during branch switching.');
582+
console.error(`Expected site directory at: ${SITE_DIR}`);
583+
console.error('This can happen in CI when switching to older branches.');
584+
console.error('The site directory should be restored by Git, but it may not be immediate.');
585+
586+
// Try to force Git to restore the directory
587+
console.log('Attempting to restore site directory from Git...');
588+
try {
589+
gitExec('checkout HEAD -- site');
590+
console.log('✓ Restored site directory from Git');
591+
592+
// Verify it was restored
593+
if (fs.existsSync(SITE_DIR)) {
594+
console.log('✓ Site directory now exists');
595+
} else {
596+
console.error('✗ Site directory still missing after restore attempt');
597+
}
598+
} catch (e) {
599+
console.error('Could not restore site directory:', e.message);
600+
console.error('Git may not have the site directory in the current branch');
601+
}
602+
} else {
603+
console.log('✓ Site directory exists');
604+
}
605+
520606
// Update and write docusaurus config after returning to original branch
521607
if (docusaurusConfig && fs.existsSync(DOCUSAURUS_CONFIG_PATH)) {
522608
const updatedConfig = updateDocusaurusConfig(docusaurusConfig);

0 commit comments

Comments
 (0)