Skip to content

Commit

Permalink
fix(bi-link): Invalidation of title reference (#326)
Browse files Browse the repository at this point in the history
* fix(bi-link): Invalidation of title reference
* feat(bi-link): support link to self
* chore: delete debug print
* fix(bi-link): CI Lint Error
* fix(bi-link): CI Lint Error2
  • Loading branch information
LincZero authored Nov 11, 2024
1 parent 9dbd06c commit 0145f42
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
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

0 comments on commit 0145f42

Please sign in to comment.