Skip to content

Commit

Permalink
feat(bi-link): support force relative path (#344)
Browse files Browse the repository at this point in the history
* feat(bi-link): support force relative path

* fix(bi-link): some lint error

* fix(bi-link): some lint error2
  • Loading branch information
LincZero authored Nov 26, 2024
1 parent ff44c3f commit fe2fef1
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions packages/markdown-it-bi-directional-links/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PluginSimple } from 'markdown-it'
import { basename, extname, posix, relative, sep } from 'node:path'
import { basename, dirname, extname, posix, relative, sep } from 'node:path'
import { cwd } from 'node:process'
import { cyan, gray, yellow } from 'colorette'
import _debug from 'debug'
Expand Down Expand Up @@ -204,6 +204,12 @@ export interface BiDirectionalLinksOptions {
* @default false
*/
noNoMatchedFileWarning?: boolean
/**
* Force a relative path instead of an absolute path
*
* @default false
*/
isRelativePath?: boolean
}

/**
Expand All @@ -220,6 +226,7 @@ export const BiDirectionalLinks: (options?: BiDirectionalLinksOptions) => Plugin
const includes = options?.includesPatterns ?? []
const debugOn = options?.debug ?? false
const noNoMatchedFileWarning = options?.noNoMatchedFileWarning ?? false
const isRelativePath = options?.isRelativePath ?? false

const possibleBiDirectionalLinksInCleanBaseNameOfFilePaths: Record<string, string> = {}
const possibleBiDirectionalLinksInFullFilePaths: Record<string, string> = {}
Expand Down Expand Up @@ -346,12 +353,20 @@ export const BiDirectionalLinks: (options?: BiDirectionalLinksOptions) => Plugin
matchedHref = matchedHrefSingleOrArray
}

let resolvedNewHref = posix.join(
baseDir,
relative(rootDir, matchedHref)
let resolvedNewHref: string
if (isRelativePath) {
resolvedNewHref = relative(dirname(state.env.relativePath), matchedHref)
.split(sep)
.join('/'),
)
.join('/')
}
else {
resolvedNewHref = posix.join(
baseDir,
relative(rootDir, matchedHref)
.split(sep)
.join('/'),
)
}

if (isImageRef) {
genImage(state, resolvedNewHref, text, link)
Expand Down

0 comments on commit fe2fef1

Please sign in to comment.