diff --git a/src/markdown-helpers.ts b/src/markdown-helpers.ts index b15afc9..2a6ac56 100644 --- a/src/markdown-helpers.ts +++ b/src/markdown-helpers.ts @@ -707,6 +707,11 @@ export const safelyJoinTokens = (tokens: Token[], options: JoinTokenOptions = {} joinedContent += safelyJoinTokens(tokenToCheck.children, options); continue; } + if (tokenToCheck.type === 'image') { + const altText = tokenToCheck.content || 'No Description'; + joinedContent += `[Image: ${altText}]`; + continue; + } expect(tokenToCheck.children).to.equal( null, 'There should be no nested children in the joinable tokens', @@ -800,14 +805,26 @@ export const safelyJoinTokens = (tokens: Token[], options: JoinTokenOptions = {} break; case 'paragraph_open': case 'blockquote_close': - case 'html_block': break; case 'html_inline': // Replace
elements with a newline if (tokenToCheck.content.match(//)) { joinedContent += '\n'; } + // Intentional fallthrough to next case + case 'html_block': { + // Replace tags with [Image: ] + const imgMatch = tokenToCheck.content.match(/]*>/i); + if (imgMatch) { + const altMatch = imgMatch[0].match(/alt=["']([^"']*)["']/i); + const altText = altMatch && altMatch[1] ? altMatch[1] : 'No Description'; + joinedContent += `[Image: ${altText}]`; + if (tokenToCheck.type === 'html_block') { + joinedContent += '\n\n'; + } + } break; + } case 'fence': if (options.parseCodeFences) { joinedContent += `\`\`\`\n${tokenToCheck.content}\`\`\`\n\n`; diff --git a/tests/__snapshots__/markdown-helpers.spec.ts.snap b/tests/__snapshots__/markdown-helpers.spec.ts.snap index 3a92b26..c552056 100644 --- a/tests/__snapshots__/markdown-helpers.spec.ts.snap +++ b/tests/__snapshots__/markdown-helpers.spec.ts.snap @@ -352,6 +352,10 @@ Process: Main _This class is not exported from the \`'electron'\` module. It is only available as a return value of other methods in the Electron API._" `; +exports[`markdown-helpers > safelyJoinTokens > snapshots > should be correct for paragraph-with-image 1`] = `"This paragraph has an image [Image: screenshot] embedded in it."`; + +exports[`markdown-helpers > safelyJoinTokens > snapshots > should be correct for paragraph-with-image-no-alt 1`] = `"This paragraph has an image with no alt text [Image: No Description] in it."`; + exports[`markdown-helpers > safelyJoinTokens > snapshots > should be correct for paragraph-with-inline-code 1`] = `"This is a \`inline code\` block that is \`code\` tagged."`; exports[`markdown-helpers > safelyJoinTokens > snapshots > should be correct for paragraph-with-links 1`] = `"This paragraph has a bunch of stuff. Also some links Foo, these links should be stripped."`; diff --git a/tests/fixtures/paragraph-with-image-no-alt.md b/tests/fixtures/paragraph-with-image-no-alt.md new file mode 100644 index 0000000..4c7eca3 --- /dev/null +++ b/tests/fixtures/paragraph-with-image-no-alt.md @@ -0,0 +1 @@ +This paragraph has an image with no alt text ![](https://example.com/screenshot.png) in it. diff --git a/tests/fixtures/paragraph-with-image.md b/tests/fixtures/paragraph-with-image.md new file mode 100644 index 0000000..a828a18 --- /dev/null +++ b/tests/fixtures/paragraph-with-image.md @@ -0,0 +1 @@ +This paragraph has an image ![screenshot](https://example.com/screenshot.png) embedded in it. diff --git a/tests/markdown-helpers.spec.ts b/tests/markdown-helpers.spec.ts index 9b0fa84..0384a0a 100644 --- a/tests/markdown-helpers.spec.ts +++ b/tests/markdown-helpers.spec.ts @@ -110,6 +110,46 @@ def fn(): expect(safelyJoinTokens(tokens, { parseCodeFences: true })).toMatchSnapshot(); }); }); + + describe('images', () => { + it('should transform markdown image links with alt text', () => { + const tokens = getTokens('Hello ![screenshot](https://example.com/screenshot.png) world'); + expect(safelyJoinTokens(tokens)).toBe('Hello [Image: screenshot] world'); + }); + + it('should use "No Description" for markdown images without alt text', () => { + const tokens = getTokens('Hello ![](https://example.com/screenshot.png) world'); + expect(safelyJoinTokens(tokens)).toBe('Hello [Image: No Description] world'); + }); + + it('should handle raw tags with alt text', () => { + const tokens = getTokens( + 'Hello my image world', + ); + expect(safelyJoinTokens(tokens)).toBe('Hello [Image: my image] world'); + }); + + it('should handle raw tags without alt text', () => { + const tokens = getTokens('Hello world'); + expect(safelyJoinTokens(tokens)).toBe('Hello [Image: No Description] world'); + }); + + it('should handle standalone block tags with alt text', () => { + const tokens = getTokens( + 'Before:\n\nImage Before Adjustment\n\nAfter:\n\nImage After Adjustment', + ); + expect(safelyJoinTokens(tokens)).toBe( + 'Before:\n\n[Image: Image Before Adjustment]\n\nAfter:\n\n[Image: Image After Adjustment]', + ); + }); + + it('should handle standalone block tags without alt text', () => { + const tokens = getTokens( + 'Text\n\n\n\nMore text', + ); + expect(safelyJoinTokens(tokens)).toBe('Text\n\n[Image: No Description]\n\nMore text'); + }); + }); }); describe('extractStringEnum()', () => {