Skip to content

Commit 45f74bc

Browse files
authored
Merge pull request #233 from HarperDB/editlink-upd
more accurate edit links and a conditional root redirect
2 parents 322bbfe + d8206e4 commit 45f74bc

File tree

6 files changed

+86
-47
lines changed

6 files changed

+86
-47
lines changed

site/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
# Latest (/docs/) is a build time copy of the latest version
88
/docs
99

10+
# Root index page is conditionally generated if docs is not at the root
11+
/src/pages/index.tsx
12+
1013
# Generated files
1114
.docusaurus
1215
.cache-loader

site/docusaurus.config.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ const config: Config = {
6666
sidebarPath: './sidebars.ts',
6767
// Docs are served at the configured route base path
6868
routeBasePath,
69-
editUrl: ({ docPath }) => {
70-
// Find where docs/ starts in the path and use everything from there
71-
const docsIndex = docPath.indexOf('docs/');
72-
if (docsIndex !== -1) {
73-
const cleanPath = docPath.substring(docsIndex);
74-
// TODO: When implementing versioned docs, this will need to handle version branches
75-
return `https://github.com/HarperDB/documentation/blob/main/${cleanPath}`;
69+
editUrl: ({ versionDocsDirPath, docPath }) => {
70+
// For versioned docs: versionDocsDirPath is like 'versioned_docs/version-4.6'
71+
// For current docs: versionDocsDirPath is 'docs'
72+
if (versionDocsDirPath.startsWith('versioned_docs')) {
73+
// Versioned docs are in site/versioned_docs/version-X.X/
74+
return `https://github.com/HarperDB/documentation/blob/main/site/${versionDocsDirPath}/${docPath}`;
75+
} else {
76+
// Current docs are in the root docs/ directory
77+
return `https://github.com/HarperDB/documentation/blob/main/docs/${docPath}`;
7678
}
77-
// Fallback if docs/ is not found
78-
return `https://github.com/HarperDB/documentation/blob/main/docs/${docPath}`;
7979
},
8080
lastVersion: '4.6',
8181
includeCurrentVersion: false,

site/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
"version": "0.0.0",
44
"private": true,
55
"scripts": {
6+
"prebuild": "node scripts/prebuild.js",
67
"docusaurus": "docusaurus",
7-
"start": "docusaurus start",
8-
"build": "docusaurus build",
8+
"start": "npm run prebuild && docusaurus start",
9+
"build": "npm run prebuild && docusaurus build",
910
"version": "node scripts/cut-version.js",
1011
"swizzle": "docusaurus swizzle",
1112
"deploy": "docusaurus deploy",
12-
"clear": "docusaurus clear",
13+
"clear": "npm run prebuild -- clean && docusaurus clear",
1314
"serve": "docusaurus serve",
1415
"typecheck": "tsc"
1516
},

site/scripts/prebuild.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Pre-build script for Docusaurus
5+
* Handles dynamic configuration based on environment variables
6+
*
7+
* Usage:
8+
* node prebuild.js - Setup for build/start
9+
* node prebuild.js clean - Clean generated files
10+
*/
11+
12+
const fs = require('node:fs');
13+
const path = require('node:path');
14+
15+
// Check if running in clean mode
16+
const isCleanMode = process.argv[2] === 'clean';
17+
18+
// Get the route base path from environment variable
19+
const routeBasePath = process.env.DOCUSAURUS_ROUTE_BASE_PATH || '/';
20+
21+
const indexPagePath = path.join(__dirname, '../src/pages/index.tsx');
22+
const pagesDir = path.join(__dirname, '../src/pages');
23+
24+
// Helper function to remove the index page
25+
function removeIndexPage() {
26+
if (fs.existsSync(indexPagePath)) {
27+
fs.unlinkSync(indexPagePath);
28+
console.log('Removed index.tsx');
29+
return true;
30+
}
31+
return false;
32+
}
33+
34+
if (isCleanMode) {
35+
// Clean mode - remove all generated files
36+
console.log('Cleaning generated files...');
37+
removeIndexPage();
38+
process.exit(0);
39+
}
40+
41+
console.log('Running pre-build setup...');
42+
console.log(`Route base path: ${routeBasePath}`);
43+
44+
// Setup index redirect page based on route configuration
45+
if (routeBasePath === '/') {
46+
// If docs are at root, remove the index redirect page
47+
console.log('Docs are at root (/), removing index redirect page, if it exists...');
48+
removeIndexPage();
49+
} else {
50+
// If docs are not at root, ensure the index redirect page exists
51+
console.log(`Docs are at ${routeBasePath}, creating index redirect page...`);
52+
53+
// Create pages directory if it doesn't exist
54+
if (!fs.existsSync(pagesDir)) {
55+
fs.mkdirSync(pagesDir, { recursive: true });
56+
}
57+
58+
// Create the redirect page
59+
const redirectContent = `import React from 'react';
60+
import { Redirect } from '@docusaurus/router';
61+
62+
export default function Home(): JSX.Element {
63+
// Redirect to the docs location
64+
return <Redirect to="${routeBasePath}" />;
65+
}
66+
`;
67+
68+
fs.writeFileSync(indexPagePath, redirectContent);
69+
console.log(`Created index redirect to ${routeBasePath}`);
70+
}

site/src/pages/index.module.css

Lines changed: 0 additions & 23 deletions
This file was deleted.

site/src/pages/index.tsx

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)