diff --git a/packages/markdown-it-bi-directional-links/src/index.test.ts b/packages/markdown-it-bi-directional-links/src/index.test.ts index 149cf14a..4619da6e 100644 --- a/packages/markdown-it-bi-directional-links/src/index.test.ts +++ b/packages/markdown-it-bi-directional-links/src/index.test.ts @@ -117,6 +117,22 @@ describe('markdown-it-bi-directional-links', () => { expect(rendered).toBe(`

foo bar

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

bar

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

bar

\n`) + }) + it('should render simple form with query strings', () => { const rendered = new MarkdownIt({ html: true }) .use(BiDirectionalLinks({ dir: testdataDir })) diff --git a/packages/markdown-it-bi-directional-links/src/index.ts b/packages/markdown-it-bi-directional-links/src/index.ts index 7f5ba71a..986d6e81 100644 --- a/packages/markdown-it-bi-directional-links/src/index.ts +++ b/packages/markdown-it-bi-directional-links/src/index.ts @@ -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]+)?\]\](?!\()/ @@ -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)