@@ -28,48 +28,72 @@ export function remarkRewriteAssets(args: {
2828
2929 return function plugin ( ) {
3030 return function transform ( tree ) {
31- // @ts -expect-error Types Should be correct here
32- visitParents < Image > ( tree , 'image' , ( node , ancestors : Array < Parent > ) => {
33- let url
34- const originalUrl = node . url
31+ visitParents < Image > (
32+ // @ts -expect-error Types Should be correct here
33+ tree ,
34+ [ 'image' , 'definition' ] ,
35+ // @ts -expect-error Types Should be correct here
36+ ( node , ancestors : Array < Parent > ) => {
37+ let url
38+ const originalUrl = node . url
3539
36- if ( isInUDR ) {
37- let domain
40+ /**
41+ * The Definition node could be used by an image or link reference
42+ * This regex checks if the path starts with /img or /public/img. While
43+ * a brittle check, it prevents links references from breaking.
44+ *
45+ * We need to follow with a more comprehensive, long-term solution
46+ * to selectively delineate image from link.
47+ */
48+ const regex = / ^ \/ i m g | \/ p u b l i c \/ i m g / // Ensures non-image links aren't rewritten
49+ const isImagePath = regex . test ( originalUrl )
3850
39- if ( process . env . NODE_ENV === 'development' ) {
40- domain = `http://localhost:${ process . env . UNIFIED_DOCS_PORT } `
41- } else {
42- domain = process . env . UNIFIED_DOCS_API
43- }
51+ if ( isImagePath ) {
52+ if ( isInUDR ) {
53+ let domain
4454
45- url = new URL ( `${ domain } /api/assets/${ product } /${ version } ${ node . url } ` )
46- } else {
47- const asset = path . posix . join ( ...getAssetPathParts ( originalUrl ) )
55+ if ( process . env . NODE_ENV === 'development' ) {
56+ domain = `http://localhost:${ process . env . UNIFIED_DOCS_PORT } `
57+ } else {
58+ domain = process . env . UNIFIED_DOCS_API
59+ }
4860
49- url = new URL ( `${ process . env . MKTG_CONTENT_DOCS_API } /api/assets` )
50- url . searchParams . append ( 'asset' , asset )
51- url . searchParams . append ( 'version' , version )
52- url . searchParams . append ( 'product' , product )
53- }
61+ url = new URL (
62+ `${ domain } /api/assets/${ product } /${ version } ${ node . url } `
63+ )
64+ } else {
65+ const asset = path . posix . join ( ...getAssetPathParts ( originalUrl ) )
66+
67+ url = new URL ( `${ process . env . MKTG_CONTENT_DOCS_API } /api/assets` )
68+ url . searchParams . append ( 'asset' , asset )
69+ url . searchParams . append ( 'version' , version )
70+ url . searchParams . append ( 'product' , product )
71+ }
5472
55- node . url = url . toString ( )
56- logOnce (
57- node . url ,
58- `Rewriting asset url for local preview:
73+ node . url = url . toString ( )
74+ logOnce (
75+ node . url ,
76+ `Rewriting asset url for local preview:
5977- Found: ${ originalUrl }
6078- Replaced with: ${ node . url }
6179
6280If this is a net-new asset, you'll need to commit and push it to GitHub.\n`
63- )
81+ )
6482
65- // if the image is wrapped in a link, and shares the same url as the original image, then update the link's url to the new asset url
66- if ( isInUDR ) {
67- const parent = ancestors [ ancestors . length - 1 ]
68- if ( parent && parent . type === 'link' && parent . url === originalUrl ) {
69- parent . url = url . toString ( )
83+ // if the image is wrapped in a link, and shares the same url as the original image, then update the link's url to the new asset url
84+ if ( isInUDR ) {
85+ const parent = ancestors [ ancestors . length - 1 ]
86+ if (
87+ parent &&
88+ parent . type === 'link' &&
89+ parent . url === originalUrl
90+ ) {
91+ parent . url = url . toString ( )
92+ }
93+ }
7094 }
7195 }
72- } )
96+ )
7397 }
7498 }
7599}
0 commit comments