Skip to content

Commit fe2fef1

Browse files
authored
feat(bi-link): support force relative path (#344)
* feat(bi-link): support force relative path * fix(bi-link): some lint error * fix(bi-link): some lint error2
1 parent ff44c3f commit fe2fef1

File tree

1 file changed

+21
-6
lines changed
  • packages/markdown-it-bi-directional-links/src

1 file changed

+21
-6
lines changed

packages/markdown-it-bi-directional-links/src/index.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 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'
33
import { cwd } from 'node:process'
44
import { cyan, gray, yellow } from 'colorette'
55
import _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

Comments
 (0)