11import type { PluginSimple } from 'markdown-it'
2- import { basename , extname , posix , relative , sep } from 'node:path'
2+ import { basename , dirname , extname , posix , relative , sep } from 'node:path'
33import { cwd } from 'node:process'
44import { cyan , gray , yellow } from 'colorette'
55import _debug from 'debug'
@@ -204,6 +204,12 @@ export interface BiDirectionalLinksOptions {
204204 * @default false
205205 */
206206 noNoMatchedFileWarning ?: boolean
207+ /**
208+ * Force a relative path instead of an absolute path
209+ *
210+ * @default false
211+ */
212+ isRelativePath ?: boolean
207213}
208214
209215/**
@@ -220,6 +226,7 @@ export const BiDirectionalLinks: (options?: BiDirectionalLinksOptions) => Plugin
220226 const includes = options ?. includesPatterns ?? [ ]
221227 const debugOn = options ?. debug ?? false
222228 const noNoMatchedFileWarning = options ?. noNoMatchedFileWarning ?? false
229+ const isRelativePath = options ?. isRelativePath ?? false
223230
224231 const possibleBiDirectionalLinksInCleanBaseNameOfFilePaths : Record < string , string > = { }
225232 const possibleBiDirectionalLinksInFullFilePaths : Record < string , string > = { }
@@ -346,12 +353,20 @@ export const BiDirectionalLinks: (options?: BiDirectionalLinksOptions) => Plugin
346353 matchedHref = matchedHrefSingleOrArray
347354 }
348355
349- let resolvedNewHref = posix . join (
350- baseDir ,
351- relative ( rootDir , matchedHref )
356+ let resolvedNewHref : string
357+ if ( isRelativePath ) {
358+ resolvedNewHref = relative ( dirname ( state . env . relativePath ) , matchedHref )
352359 . split ( sep )
353- . join ( '/' ) ,
354- )
360+ . join ( '/' )
361+ }
362+ else {
363+ resolvedNewHref = posix . join (
364+ baseDir ,
365+ relative ( rootDir , matchedHref )
366+ . split ( sep )
367+ . join ( '/' ) ,
368+ )
369+ }
355370
356371 if ( isImageRef ) {
357372 genImage ( state , resolvedNewHref , text , link )
0 commit comments