-
Notifications
You must be signed in to change notification settings - Fork 19
fix: replace Markdown image links and raw <img> tags with alt text #187
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,13 +805,23 @@ export const safelyJoinTokens = (tokens: Token[], options: JoinTokenOptions = {} | |
| break; | ||
| case 'paragraph_open': | ||
| case 'blockquote_close': | ||
| case 'html_block': | ||
| break; | ||
| case 'html_inline': | ||
| // Replace <br> elements with a newline | ||
| if (tokenToCheck.content.match(/<br\s*\/?>/)) { | ||
| joinedContent += '\n'; | ||
| } | ||
| case 'html_block': | ||
| // Replace <img> and <image> tags with [Image: <alt text>] | ||
| const imgMatch = tokenToCheck.content.match(/<(?:img|image)\b[^>]*>/i); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is Regardless can we parse this as XML or something? HTML typically can't be safely parsed with regex (with the exception of inline self-closed tags like
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Claude special, not sure why it decided
We can make this more robust, but I think "Good enough" might suffice here? If the regex approaches misses some cases they'll just be excluded from the output all together, which seems like an acceptable failure mode. Is there a main issue you want to avoid with more robust parsing?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For context, we currently have three usages of If we have any larger concerns around
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Looks like we could convert the one in |
||
| 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) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| This paragraph has an image with no alt text  in it. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| This paragraph has an image  embedded in it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lost a
break:?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intentional fallthrough as
<img>can be bothhtml_inlineandhtml_block. Will add a comment about the intentional fallthrough.