Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bi-link): support force relative path #344

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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