@@ -19,77 +19,55 @@ interface ImageOpts extends MarkdownItPluginOpts {
1919
2020function replaceImageSrc (
2121 state : StateCore ,
22- currentPath : string ,
22+ from : string ,
2323 path : string ,
2424 imgSrc : string ,
25- { assetsPublicPath = sep , root = '' , log} : ImageOpts ,
25+ { assetsPublicPath = '/' , root = '' , log} : ImageOpts ,
2626) {
2727 if ( isFileExists ( path ) ) {
2828 state . md . assets ?. push ( imgSrc ) ;
2929 } else {
30- log . error ( `Asset not found: ${ bold ( imgSrc ) } in ${ bold ( currentPath ) } ` ) ;
30+ log . error ( `Asset not found: ${ bold ( imgSrc ) } in ${ bold ( from ) } ` ) ;
3131 }
3232
3333 const relativeToRoot = path . replace ( root + sep , '' ) ;
34- const publicSrc = join ( assetsPublicPath , relativeToRoot ) ;
3534
36- return publicSrc ;
35+ return join ( assetsPublicPath , relativeToRoot ) ;
36+ }
37+
38+ interface InlineOptions {
39+ enabled : boolean ;
40+ maxFileSize : number ;
3741}
3842
3943interface SVGOpts extends MarkdownItPluginOpts {
4044 notFoundCb : ( s : string ) => void ;
4145 imageOpts : ImageOptions ;
46+ svgInline : InlineOptions ;
4247}
4348
4449function getSvgContent (
45- state : StateCore ,
46- path : string ,
47- { rawContent , path : optsPath , notFoundCb, log, root} : Opts ,
50+ file : string ,
51+ from : string ,
52+ { getContent , notFoundCb, log, root = '' } : Opts ,
4853) {
49- const rootFile = state . env . path || optsPath ;
5054 try {
51- const raw = rawContent ( path ) ;
52- return typeof raw === 'string' ? raw : null ;
55+ return getContent ( file ) ;
5356 } catch ( e : unknown ) {
54- log . error ( `SVG ${ path } from ${ rootFile } not found` ) ;
57+ const path = file . replace ( root , '' ) ;
58+ log . error ( `SVG ${ path } from ${ from } not found` ) ;
5559
5660 if ( notFoundCb ) {
57- notFoundCb ( path . replace ( root , '' ) ) ;
61+ notFoundCb ( path ) ;
5862 }
5963
6064 return null ;
6165 }
6266}
63- function convertSvg (
64- raw : string | null ,
65- state : StateCore ,
66- {
67- file : path ,
68- log,
69- forceInlineSvg,
70- imageOpts,
71- svgInline : { maxFileSize} ,
72- } : Opts & { forceInlineSvg : boolean } ,
73- ) {
74- if ( raw === null ) {
75- return null ;
76- }
77- if ( raw . length > maxFileSize ) {
78- log . info ( `Svg size more than ${ maxFileSize } : ${ bold ( path ) } ` ) ;
79- if ( ! forceInlineSvg ) {
80- return null ;
81- }
82- }
83- const content = raw === '' ? '' : replaceSvgContent ( raw , imageOpts ) ;
84- const svgToken = new state . Token ( 'image_svg' , '' , 0 ) ;
85- svgToken . attrSet ( 'content' , content ) ;
86-
87- return svgToken ;
88- }
8967
9068type Opts = SVGOpts &
9169 ImageOpts & {
92- rawContent : ( path : string ) => string | boolean ;
70+ getContent : ( path : string ) => string ;
9371 calcPath : ( root : string , path : string ) => string ;
9472 replaceImageSrc : (
9573 state : StateCore ,
@@ -99,22 +77,18 @@ type Opts = SVGOpts &
9977 opts : ImageOpts ,
10078 ) => string ;
10179 file : string ;
102- svgInline : {
103- enabled : boolean ;
104- maxFileSize : number ;
105- } ;
10680 } ;
10781
108- function shouldBeInlined ( token : Token , opts : Opts ) {
82+ function shouldBeInlined ( token : Token , opts : InlineOptions ) {
10983 if ( ! token . attrGet ( 'src' ) ?. endsWith ( '.svg' ) ) {
11084 return false ;
11185 }
11286
11387 const forceInlineSvg = token . attrGet ( 'inline' ) === 'true' ;
114- const shouldInlineSvg =
115- forceInlineSvg || ( token . attrGet ( 'inline' ) !== 'false' && opts . svgInline . enabled !== false ) ;
88+ const forceSplitSvg = token . attrGet ( 'inline' ) === 'false' ;
89+ const inliningIsEnabled = opts . enabled !== false ;
11690
117- return shouldInlineSvg ;
91+ return forceInlineSvg || ( ! forceSplitSvg && inliningIsEnabled ) ;
11892}
11993
12094const getRawFile = ( path : string ) => {
@@ -123,7 +97,7 @@ const getRawFile = (path: string) => {
12397
12498const index : MarkdownItPluginCb < Opts > = ( md , opts ) => {
12599 const {
126- rawContent = getRawFile ,
100+ getContent = getRawFile ,
127101 calcPath = resolveRelativePath ,
128102 replaceImageSrc : replaceImage = replaceImageSrc ,
129103 } = opts ;
@@ -158,34 +132,32 @@ const index: MarkdownItPluginCb<Opts> = (md, opts) => {
158132 return ;
159133 }
160134
161- const forceInlineSvg = image . attrGet ( 'inline' ) === 'true' ;
162- const shouldInlineSvg = shouldBeInlined ( image , opts ) ;
135+ const shouldInlineSvg = shouldBeInlined ( image , opts . svgInline ) ;
163136 const imageOpts = {
164137 width : image . attrGet ( 'width' ) ,
165138 height : image . attrGet ( 'height' ) ,
166139 } ;
167140
168- const root = state . env . path || opts . path ;
169- const file = calcPath ( root , imgSrc ) ;
141+ const from = state . env . path || opts . path ;
142+ const file = calcPath ( from , imgSrc ) ;
170143
171144 if ( shouldInlineSvg ) {
172- const svgContent = getSvgContent ( state , file , {
173- ...opts ,
174- rawContent,
175- } ) ;
176- const svgToken = convertSvg ( svgContent , state , {
145+ const rawContent = getSvgContent ( file , from , {
177146 ...opts ,
178- forceInlineSvg,
179- imageOpts,
180- file,
147+ getContent,
181148 } ) ;
182- if ( svgToken ) {
149+
150+ if ( rawContent !== null ) {
151+ const content = replaceSvgContent ( rawContent , imageOpts ) ;
152+ const svgToken = new state . Token ( 'image_svg' , '' , 0 ) ;
153+ svgToken . attrSet ( 'content' , content ) ;
154+
183155 childrenTokens [ index ] = svgToken ;
184156 }
185157 }
186158
187159 if ( childrenTokens [ index ] . type === 'image' ) {
188- image . attrSet ( 'src' , replaceImage ( state , root , file , imgSrc , opts ) ) ;
160+ image . attrSet ( 'src' , replaceImage ( state , from , file , imgSrc , opts ) ) ;
189161 image . attrSet ( 'yfm_patched' , '1' ) ;
190162 }
191163 } ) ;
@@ -206,6 +178,10 @@ const index: MarkdownItPluginCb<Opts> = (md, opts) => {
206178} ;
207179
208180function replaceSvgContent ( content : string , options : ImageOptions ) {
181+ if ( ! content ) {
182+ return content ;
183+ }
184+
209185 // monoline
210186 content = content . replace ( / > \r ? \n < / g, '><' ) . replace ( / \r ? \n / g, ' ' ) ;
211187
0 commit comments