1- import React , { useEffect , useState } from 'react' ;
2- import useDocusaurusContext from '@docusaurus/useDocusaurusContext ' ;
1+ import React from 'react' ;
2+ import { useDoc } from '@docusaurus/plugin-content-docs/client ' ;
33import ContentActions from '@site/src/components/ContentActions' ;
44
55export default function DocsActions ( ) : JSX . Element | null {
6- const { siteConfig } = useDocusaurusContext ( ) ;
7- const [ pathname , setPathname ] = useState < string > ( '' ) ;
8-
9- useEffect ( ( ) => {
10- setPathname ( window . location . pathname ) ;
11- } , [ ] ) ;
12-
13- // Compute values unconditionally to keep hooks order stable
14- const isDocsPage = pathname . startsWith ( '/docs/' ) && pathname !== '/docs/' && ! pathname . includes ( '/index' ) ;
15-
16- const relativePath = pathname . replace ( '/docs/' , '' ) ;
17- const editUrl = ` https://gitlab.com/postgres-ai/docs/-/edit/master/docs/${ relativePath } .md` ;
18- const rawUrl = `/raw/docs/ ${ relativePath } .md` ;
19-
20- if ( ! isDocsPage ) {
6+ const { metadata } = useDoc ( ) ;
7+
8+ // metadata.editUrl is correctly generated by Docusaurus with the actual filename
9+ // e.g., "https://gitlab.com/postgres-ai/docs/-/edit/master/docs/monitoring/dashboards/04-wait-events.md"
10+ const editUrl = metadata . editUrl ;
11+
12+ if ( ! editUrl ) {
13+ return null ;
14+ }
15+
16+ // Extract the relative path from the editUrl to construct the raw URL
17+ // editUrl format: https://gitlab.com/postgres-ai/docs/-/edit/master/docs/path/to/file .md
18+ // We need to extract "path/to/file .md" from after "/master/docs/"
19+ const match = editUrl . match ( / \/ m a s t e r \/ d o c s \/ ( . + \. m d ) $ / ) ;
20+ if ( ! match ) {
2121 return null ;
2222 }
23+
24+ const relativePath = match [ 1 ] ;
25+ const rawUrl = `/raw/docs/${ relativePath } ` ;
26+
27+ // Intentionally use blob (view) URL instead of edit URL.
28+ // This lets users see the file first and choose their preferred editing method:
29+ // Web IDE, single file editor, clone & edit locally, etc.
30+ const viewUrl = editUrl . replace ( '/-/edit/' , '/-/blob/' ) ;
2331
24- return < ContentActions rawUrl = { rawUrl } editUrl = { editUrl } /> ;
25- }
32+ return < ContentActions rawUrl = { rawUrl } editUrl = { viewUrl } /> ;
33+ }
0 commit comments