@@ -3,6 +3,7 @@ import { RemoteContent } from 'nextra/data'
3
3
import { buildDynamicMDX } from ' nextra/remote'
4
4
import { listFiles } from ' ./_meta.js'
5
5
import { getPageMap } from ' @/src/getPageMap'
6
+ import path from ' node:path'
6
7
7
8
export async function getStaticPaths() {
8
9
const files = await listFiles ()
@@ -37,8 +38,13 @@ export async function getStaticProps({ params: { slug = ['README'] } }) {
37
38
)
38
39
// remove gitbook {% ... %} elements
39
40
.replaceAll (/ {%. *? %}/ g , ' ' )
40
- // close img tags only if he doesn't point to .gitbook
41
- .replaceAll (/ <img(. *? )>/ g , (... m ) => (m [1 ].includes (' src=".gitbook' ) ? ' ' : ` <img${m [1 ]}/> ` ))
41
+ // close unclosed img tags
42
+ .replaceAll (/ <img((?:(?!\/ >)[^ >] )*? )>/ g , (... m ) => ` <img${m [1 ]}/> ` )
43
+ // fix gitbook image srcs
44
+ .replaceAll (/ src="[^ >"] * \. gitbook\/ (. * )"/ g , (... m ) => {
45
+ console .log ({ m })
46
+ return ` src="${baseURL }.gitbook/${m [1 ]}" `
47
+ })
42
48
const mdx = await buildDynamicMDX (data , {
43
49
mdxOptions: {
44
50
// change-log contains `{variable}` text that is thrown an error by MDX2 parser since he treat it as variable injection, to fix it we parse chang-log with the markdown parser
@@ -47,15 +53,12 @@ export async function getStaticProps({ params: { slug = ['README'] } }) {
47
53
() => (tree , _file , done ) => {
48
54
// enhance links
49
55
visit (tree , ' link' , (node ) => {
50
- if (node .url .startsWith (' ./' )) {
51
- node .url = node .url .slice (2 )
52
- }
53
56
if (node .url .startsWith (' /' )) {
54
57
// (foo)[/foo/bar]
55
58
node .url = node .url .replace (' /' , ' /substreams/' )
56
- } else if ( ! node . url . includes ( ' / ' ) && node . url . endsWith ( ' .md ' )) {
57
- // (foo)[foo.md]
58
- node .url = [ ... slug . slice ( 0 , - 1 ), node .url ]. join ( ' / ' )
59
+ } else {
60
+ // (foo)[foo.md] or (foo)[./foo.md] or (foo)[../foo.md]
61
+ node .url = path . join ( path . dirname ( fileURL ), node .url )
59
62
}
60
63
})
61
64
done ()
0 commit comments