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 09814ac5..39622ecb 100644 --- a/packages/markdown-it-bi-directional-links/src/index.test.ts +++ b/packages/markdown-it-bi-directional-links/src/index.test.ts @@ -19,6 +19,22 @@ describe('markdown-it-bi-directional-links', () => { expect(rendered).toBe('

foo

\n') }) + it('should render simple form for images', () => { + const rendered = new MarkdownIt({ html: true }) + .use(BiDirectionalLinks({ dir: testdataDir })) + .render('![[foo.png]]') + + expect(rendered).toBe('

\n') + }) + + it('should render simple form for images without attachment markup', () => { + const rendered = new MarkdownIt({ html: true }) + .use(BiDirectionalLinks({ dir: testdataDir })) + .render('[[foo.png]]') + + expect(rendered).toBe('

\n') + }) + it('should render simple form with custom texts', () => { const rendered = new MarkdownIt({ html: true }) .use(BiDirectionalLinks({ dir: testdataDir })) @@ -27,6 +43,22 @@ describe('markdown-it-bi-directional-links', () => { expect(rendered).toBe('

bar

\n') }) + it('should render simple form for images with custom alt', () => { + const rendered = new MarkdownIt({ html: true }) + .use(BiDirectionalLinks({ dir: testdataDir })) + .render('![[foo.png|alt text]]') + + expect(rendered).toBe('

alt text

\n') + }) + + it('should render simple form for images with custom alt without attachment markup', () => { + const rendered = new MarkdownIt({ html: true }) + .use(BiDirectionalLinks({ dir: testdataDir })) + .render('[[foo.png|alt text]]') + + expect(rendered).toBe('

alt text

\n') + }) + it('should render simple form with custom html texts', () => { const inlineHTML = `Custom HTML (Before) Middle Custom HTML (After)` diff --git a/packages/markdown-it-bi-directional-links/src/index.ts b/packages/markdown-it-bi-directional-links/src/index.ts index 2a498fca..1b2106be 100644 --- a/packages/markdown-it-bi-directional-links/src/index.ts +++ b/packages/markdown-it-bi-directional-links/src/index.ts @@ -29,7 +29,6 @@ const IMAGES_EXTENSIONS = [ '.jfif', '.pjpeg', '.pjp', - '.png', '.svg', '.webp', '.xbm', @@ -189,43 +188,41 @@ export const BiDirectionalLinks: (options?: BiDirectionalLinksOptions) => (md: M IMAGES_EXTENSIONS.forEach(ext => includes.push(`**/*${ext}`)) } - for (const include of includes) { - const files = globSync(include, { - nodir: true, - absolute: true, - cwd: rootDir, - ignore: [ - '_*', - 'dist', - 'node_modules', - ], - }) - - for (const file of files) { - const relativeFilePath = relative(rootDir, file) - const partialFilePathWithOnlyBaseName = basename(relativeFilePath) - - const existingFileName = possibleBiDirectionalLinksInCleanBaseNameOfFilePaths[partialFilePathWithOnlyBaseName] - - // when conflict - if (typeof existingFileName === 'string' && existingFileName !== '') { - // remove key from clean base name map - delete possibleBiDirectionalLinksInCleanBaseNameOfFilePaths[partialFilePathWithOnlyBaseName] - // remove key from full file path map - delete possibleBiDirectionalLinksInFullFilePaths[existingFileName] - - // add key to full file path map - possibleBiDirectionalLinksInFullFilePaths[relativeFilePath] = relativeFilePath - // recover deleted and conflicted key to full file path map - possibleBiDirectionalLinksInFullFilePaths[existingFileName] = existingFileName - - continue - } - - // otherwise, add key to both maps - possibleBiDirectionalLinksInCleanBaseNameOfFilePaths[partialFilePathWithOnlyBaseName] = relativeFilePath + const files = globSync(includes, { + nodir: true, + absolute: true, + cwd: rootDir, + ignore: [ + '_*', + 'dist', + 'node_modules', + ], + }) + + for (const file of files) { + const relativeFilePath = relative(rootDir, file) + const partialFilePathWithOnlyBaseName = basename(relativeFilePath) + + const existingFileName = possibleBiDirectionalLinksInCleanBaseNameOfFilePaths[partialFilePathWithOnlyBaseName] + + // when conflict + if (typeof existingFileName === 'string' && existingFileName !== '') { + // remove key from clean base name map + delete possibleBiDirectionalLinksInCleanBaseNameOfFilePaths[partialFilePathWithOnlyBaseName] + // remove key from full file path map + delete possibleBiDirectionalLinksInFullFilePaths[existingFileName] + + // add key to full file path map possibleBiDirectionalLinksInFullFilePaths[relativeFilePath] = relativeFilePath + // recover deleted and conflicted key to full file path map + possibleBiDirectionalLinksInFullFilePaths[existingFileName] = existingFileName + + continue } + + // otherwise, add key to both maps + possibleBiDirectionalLinksInCleanBaseNameOfFilePaths[partialFilePathWithOnlyBaseName] = relativeFilePath + possibleBiDirectionalLinksInFullFilePaths[relativeFilePath] = relativeFilePath } return (md) => { diff --git a/packages/markdown-it-bi-directional-links/src/testdata/foo.png b/packages/markdown-it-bi-directional-links/src/testdata/foo.png new file mode 100644 index 00000000..f2c312ee Binary files /dev/null and b/packages/markdown-it-bi-directional-links/src/testdata/foo.png differ diff --git a/packages/markdown-it-bi-directional-links/src/utils.ts b/packages/markdown-it-bi-directional-links/src/utils.ts index 3f25bead..05ae5d46 100644 --- a/packages/markdown-it-bi-directional-links/src/utils.ts +++ b/packages/markdown-it-bi-directional-links/src/utils.ts @@ -1,6 +1,6 @@ import { sep } from 'node:path' -import type Token from 'markdown-it/lib/token' -import type StateInline from 'markdown-it/lib/rules_inline/state_inline' +import Token from 'markdown-it/lib/token.mjs' +import type StateInline from 'markdown-it/lib/rules_inline/state_inline.mjs' import type MarkdownIt from 'markdown-it' /** @@ -87,7 +87,7 @@ export function genImage( openToken.children = [] openToken.content = text - const innerTextToken = state.push('text', '', 0) + const innerTextToken = new Token('text', '', 0) innerTextToken.content = text openToken.children.push(innerTextToken)