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-directional-links): incorrect token implementation and dup handling for .png images #239

Merged
merged 1 commit into from
Jun 18, 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
32 changes: 32 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 @@ -19,6 +19,22 @@ describe('markdown-it-bi-directional-links', () => {
expect(rendered).toBe('<p><a href="/foo.md">foo</a></p>\n')
})

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

expect(rendered).toBe('<p><img src="/foo.png" alt=""></p>\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('<p><img src="/foo.png" alt=""></p>\n')
})

it('should render simple form with custom texts', () => {
const rendered = new MarkdownIt({ html: true })
.use(BiDirectionalLinks({ dir: testdataDir }))
Expand All @@ -27,6 +43,22 @@ describe('markdown-it-bi-directional-links', () => {
expect(rendered).toBe('<p><a href="/foo.md">bar</a></p>\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('<p><img src="/foo.png" alt="alt text"></p>\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('<p><img src="/foo.png" alt="alt text"></p>\n')
})

it('should render simple form with custom html texts', () => {
const inlineHTML = `<span style="color: red;">Custom HTML (Before)</span> Middle <span style="color: blue;">Custom HTML (After)</span>`

Expand Down
69 changes: 33 additions & 36 deletions packages/markdown-it-bi-directional-links/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const IMAGES_EXTENSIONS = [
'.jfif',
'.pjpeg',
'.pjp',
'.png',
'.svg',
'.webp',
'.xbm',
Expand Down Expand Up @@ -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) => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions packages/markdown-it-bi-directional-links/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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'

/**
Expand Down Expand Up @@ -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)

Expand Down
Loading