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

fix(bi-link): Invalidation of title reference #326

Merged
merged 6 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions packages/markdown-it-bi-directional-links/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ describe('markdown-it-bi-directional-links', () => {
expect(rendered).toBe(`<p><a href="/foo bar.md#bar">foo bar</a></p>\n`)
})

it('should render when without "foo" 1', () => {
const rendered = new MarkdownIt({ html: true })
.use(BiDirectionalLinks({ dir: testdataDir }))
.render(`[[#bar]]`)

expect(rendered).toBe(`<p><a href="#bar">bar</a></p>\n`)
})

it('should render when without "foo" 2', () => {
const rendered = new MarkdownIt({ html: true })
.use(BiDirectionalLinks({ dir: testdataDir }))
.render(`[[^bar]]`)

expect(rendered).toBe(`<p><a href="#^bar">bar</a></p>\n`)
})

it('should render simple form with query strings', () => {
const rendered = new MarkdownIt({ html: true })
.use(BiDirectionalLinks({ dir: testdataDir }))
Expand Down
14 changes: 12 additions & 2 deletions packages/markdown-it-bi-directional-links/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { findBiDirectionalLinks, genAudio, genImage, genLink, genVideo } from '.

/** it will match [[file]] and [[file|text]] */
const biDirectionalLinkPattern = /!?\[\[([^|\]\n]+)(\|([^\]\n]+))?\]\](?!\()/
/** it will match [[file]] and [[file|text]] but only at the start of the text */
/** it will match [[file]] and [[file|text]], but only at the start of the text */
// eslint-disable-next-line regexp/no-unused-capturing-group
const biDirectionalLinkPatternWithStart = /^!?\[\[[^|\]\n]+(\|[^\]\n]+)?\]\](?!\()/

Expand Down Expand Up @@ -306,11 +306,21 @@ export const BiDirectionalLinks: (options?: BiDirectionalLinksOptions) => Plugin
const isAudioRef = AUDIO_EXTENSIONS.some(ext => href.endsWith(ext))

// Extract the pathname from the href
const parsedHref = new URL(href.startsWith('#') || href.startsWith('^') || href.startsWith('?') ? relative(rootDir, state.env.path) + href : href, 'https://a.com')
const parsedHref = new URL(href.startsWith('?') ? relative(rootDir, state.env.path) + href : href, 'https://a.com')
// 1. Remove the leading slash since pathname always starts with a slash and we don't want it
// 2. Decode the pathname since it is url-encoded
const parsedPathname = decodeURIComponent(parsedHref.pathname.slice(1))

// If to self, direct return, no need to find and parse
const isToSelf = href.startsWith('#') || href.startsWith('^')
if (isToSelf) {
let resolvedNewHref = ''
parsedHref.hash = href
resolvedNewHref = resolvedNewHref + parsedHref.search + parsedHref.hash
genLink(state, resolvedNewHref, href.slice(1), md, href, link)
return true
}

// Convert href to os specific path for matching and resolving
let osSpecificHref = parsedPathname.split('/').join(sep)

Expand Down