Skip to content

Commit 31a9949

Browse files
authored
Fixing Edit on GitHub Links (#627)
1 parent 4dacaae commit 31a9949

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ package-lock.json
3131
# LLM documentation (generated)
3232
/public/llms/
3333
/public/llms.txt
34+
35+
# Edit on GitHub index routes (generated by scripts/generate-github-routes.mjs)
36+
/src/lib/edit-on-github-routes.js

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"version": "1.0.0",
44
"private": true,
55
"scripts": {
6-
"dev": "npm run gen:llm && next dev --webpack",
7-
"build": "npm run gen:llm && next build --webpack",
6+
"dev": "npm run gen:llm && npm run gen:edit-routes && next dev --webpack",
7+
"build": "npm run gen:llm && npm run gen:edit-routes && next build --webpack",
8+
"gen:edit-routes": "node scripts/generate-github-routes.mjs",
89
"gen": "swagger-codegen generate -i https://raw.githubusercontent.com/netbirdio/netbird/main/management/server/http/api/openapi.yml -l openapi -o generator/openapi && npx ts-node generator/index.ts gen --input generator/openapi/openapi.json --output src/pages/ipa/resources",
910
"gen:llm": "node scripts/generate-llm-docs.mjs",
1011
"start": "next start",

scripts/generate-github-routes.mjs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Generates src/lib/edit-on-github-routes.js by scanning src/pages for index.mdx.
4+
* Used by Layout.jsx so "Edit on GitHub" links point to .../index.mdx for those routes.
5+
* Run automatically with dev and build.
6+
*/
7+
8+
import fs from 'fs'
9+
import path from 'path'
10+
import { fileURLToPath } from 'url'
11+
12+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
13+
const ROOT = path.join(__dirname, '..')
14+
const PAGES_DIR = path.join(ROOT, 'src/pages')
15+
const OUT_PATH = path.join(ROOT, 'src/lib/edit-on-github-routes.js')
16+
17+
function findIndexMdxRoutes(dir, basePath = '') {
18+
const entries = fs.readdirSync(dir, { withFileTypes: true })
19+
const routes = []
20+
for (const e of entries) {
21+
const rel = basePath ? `${basePath}/${e.name}` : e.name
22+
if (e.isDirectory()) {
23+
routes.push(...findIndexMdxRoutes(path.join(dir, e.name), rel))
24+
} else if (e.name === 'index.mdx') {
25+
routes.push('/' + basePath)
26+
}
27+
}
28+
return routes
29+
}
30+
31+
const routes = findIndexMdxRoutes(PAGES_DIR)
32+
.filter((r) => r !== '') // ignore root index if any
33+
.sort()
34+
35+
const content = `// Auto-generated by scripts/generate-github-routes.mjs – do not edit
36+
/** Pathnames served by src/pages/.../index.mdx (Edit on GitHub must link to /index.mdx). */
37+
export const EDIT_ON_GITHUB_INDEX_ROUTES = new Set(${JSON.stringify(routes)});
38+
`
39+
40+
fs.mkdirSync(path.dirname(OUT_PATH), { recursive: true })
41+
fs.writeFileSync(OUT_PATH, content, 'utf8')
42+
console.log('Generated', OUT_PATH, 'with', routes.length, 'index routes')

src/components/Layout.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { faGithub } from '@fortawesome/free-brands-svg-icons';
1717
import {toast} from "react-toastify";
1818
import {AnnouncementBanner} from "@/components/announcement-banner/AnnouncementBanner";
1919
import {useAnnouncements} from "@/components/announcement-banner/AnnouncementBannerProvider";
20+
import { EDIT_ON_GITHUB_INDEX_ROUTES } from '@/lib/edit-on-github-routes'
2021

2122
const navigation = [
2223
{
@@ -228,7 +229,7 @@ export function Layout({ children, title, tableOfContents }) {
228229
</li>
229230
<li key="edit-on-github">
230231
<Link
231-
href={"https://github.com/netbirdio/docs/tree/main/src/pages" + router.pathname + ".mdx"}
232+
href={"https://github.com/netbirdio/docs/tree/main/src/pages" + (EDIT_ON_GITHUB_INDEX_ROUTES.has(router.pathname) ? router.pathname + "/index.mdx" : router.pathname + ".mdx")}
232233
className="dark:hover:text-slate-300 dark:text-slate-400 text-slate-500 hover:text-slate-700 font-normal'"
233234
style={{display: "flex", alignItems: 'center'}}
234235
>

0 commit comments

Comments
 (0)