File tree Expand file tree Collapse file tree 3 files changed +26
-13
lines changed Expand file tree Collapse file tree 3 files changed +26
-13
lines changed Original file line number Diff line number Diff line change @@ -56,7 +56,7 @@ title: Authoring
56
56
<details >
57
57
<summary >Result (with double quotes)</summary >
58
58
59
- <img src = " ./ gutenberg.jpg" />
59
+ <img src = " gutenberg.jpg" />
60
60
61
61
</details >
62
62
Original file line number Diff line number Diff line change
1
+ import resolveMdxUrl from '@/utils/resolveMdxUrl'
1
2
import type { Root } from 'hast'
2
3
import { visit } from 'unist-util-visit'
3
4
5
+ import type { MdxJsxAttribute } from 'mdast-util-mdx-jsx'
6
+
4
7
// https://unifiedjs.com/learn/guide/create-a-rehype-plugin/
5
- export function rehypeImg ( ) {
6
- return ( tree : Root ) => {
8
+ export function rehypeImg (
9
+ relFilePath : Parameters < typeof resolveMdxUrl > [ 1 ] ,
10
+ MDX_BASEURL : Parameters < typeof resolveMdxUrl > [ 2 ] ,
11
+ ) {
12
+ return ( ) => ( tree : Root ) => {
7
13
visit ( tree , null , function ( node ) {
8
14
// console.log(node)
9
- if ( node . type === 'mdxJsxFlowElement' && node . name === 'img' ) node . name = 'Img' // map HTML <img> to <Img> React component
15
+ if ( node . type === 'mdxJsxFlowElement' && node . name === 'img' ) {
16
+ node . name = 'Img' // map HTML <img> to <Img> React component
17
+
18
+ //
19
+ // Resolve relative URLs
20
+ //
21
+
22
+ const srcAttr = (
23
+ node . attributes . filter ( ( { type } ) => type === 'mdxJsxAttribute' ) as MdxJsxAttribute [ ]
24
+ ) . find ( ( attr ) => attr . name === 'src' )
25
+
26
+ if ( srcAttr ) {
27
+ const src = srcAttr . value
28
+ if ( typeof src === 'string' ) srcAttr . value = resolveMdxUrl ( src , relFilePath , MDX_BASEURL )
29
+ }
30
+ }
10
31
} )
11
32
}
12
33
}
Original file line number Diff line number Diff line change @@ -142,14 +142,6 @@ async function _getDocs(
142
142
// inline images
143
143
//
144
144
145
- content = content . replace (
146
- / ( s r c = " | \( ) ( .+ ?\. (?: p n g | j p e ? g | g i f | w e b p | a v i f ) ) ( " | \) ) / g, // https://regexper.com/#%2F%28src%3D%22%7C%5C%28%29%28.%2B%3F%5C.%28%3F%3Apng%7Cjpe%3Fg%7Cgif%7Cwebp%7Cavif%29%29%28%22%7C%5C%29%29%2Fg
147
- ( _input , prefix : string , src : string , suffix : string ) => {
148
- const url = resolveMdxUrl ( src , relFilePath , MDX_BASEURL )
149
- return `${ prefix } ${ url } ${ suffix } `
150
- } ,
151
- )
152
-
153
145
const boxes : string [ ] = [ ]
154
146
const tableOfContents : DocToC [ ] = [ ]
155
147
@@ -159,7 +151,7 @@ async function _getDocs(
159
151
mdxOptions : {
160
152
remarkPlugins : [ remarkGFM ] ,
161
153
rehypePlugins : [
162
- rehypeImg ,
154
+ rehypeImg ( relFilePath , MDX_BASEURL ) ,
163
155
rehypeDetails ,
164
156
rehypeSummary ,
165
157
rehypeGha ,
You can’t perform that action at this time.
0 commit comments