fix: use alt -> title -> filename

This commit is contained in:
2026-02-15 09:31:42 +01:00
parent 416a7ad5d3
commit b730f12ba0
4 changed files with 16 additions and 5 deletions

View File

@@ -201,10 +201,13 @@ export class ImportAnalysisEngine {
// - Otherwise, use the original image src // - Otherwise, use the original image src
const imageUrl = hrefIsImage ? href : imgSrc; const imageUrl = hrefIsImage ? href : imgSrc;
// Derive alt text: use image alt if not empty, otherwise extract filename from the URL // Derive alt text: prefer alt, then title, then filename
let altText = imgAlt.trim(); let altText = imgAlt.trim();
if (!altText) { if (!altText) {
// Extract filename from the image URL altText = imgTitle.trim();
}
if (!altText) {
// Extract filename from the image URL as last resort
const urlPath = imageUrl.split('?')[0]; // Remove query string const urlPath = imageUrl.split('?')[0]; // Remove query string
const filename = urlPath.split('/').pop() || ''; const filename = urlPath.split('/').pop() || '';
altText = filename; altText = filename;

View File

@@ -111,10 +111,13 @@ export class ImportExecutionEngine extends EventEmitter {
// - Otherwise, use the original image src // - Otherwise, use the original image src
const imageUrl = hrefIsImage ? href : imgSrc; const imageUrl = hrefIsImage ? href : imgSrc;
// Derive alt text: use image alt if not empty, otherwise extract filename from the URL // Derive alt text: prefer alt, then title, then filename
let altText = imgAlt.trim(); let altText = imgAlt.trim();
if (!altText) { if (!altText) {
// Extract filename from the image URL altText = imgTitle.trim();
}
if (!altText) {
// Extract filename from the image URL as last resort
const urlPath = imageUrl.split('?')[0]; // Remove query string const urlPath = imageUrl.split('?')[0]; // Remove query string
const filename = urlPath.split('/').pop() || ''; const filename = urlPath.split('/').pop() || '';
altText = filename; altText = filename;

View File

@@ -312,7 +312,9 @@ with multiple lines</pre>]]></content:encoded>
<p>Linked image where link and image src are the same:</p> <p>Linked image where link and image src are the same:</p>
<a href="http://example.com/photo.jpg"><img src="http://example.com/photo.jpg" alt="" /></a> <a href="http://example.com/photo.jpg"><img src="http://example.com/photo.jpg" alt="" /></a>
<p>For comparison, an image with proper alt inside a link should preserve the alt:</p> <p>For comparison, an image with proper alt inside a link should preserve the alt:</p>
<a href="http://example.com/about"><img src="http://example.com/logo.png" alt="Company Logo" /></a>]]></content:encoded> <a href="http://example.com/about"><img src="http://example.com/logo.png" alt="Company Logo" /></a>
<p>Image with title but empty alt should use title as alt text:</p>
<a href="http://example.com/wp-content/uploads/2020/03/dish.jpg"><img src="http://example.com/wp-content/uploads/2020/03/dish-thumb.jpg" alt="" title="Delicious Piroggen" /></a>]]></content:encoded>
<excerpt:encoded><![CDATA[Testing linked images conversion]]></excerpt:encoded> <excerpt:encoded><![CDATA[Testing linked images conversion]]></excerpt:encoded>
<wp:post_id>107</wp:post_id> <wp:post_id>107</wp:post_id>
<wp:post_date>2024-01-07 10:00:00</wp:post_date> <wp:post_date>2024-01-07 10:00:00</wp:post_date>

View File

@@ -432,6 +432,9 @@ describe('ImportExecutionEngine E2E Tests', () => {
// Image with proper alt inside link should preserve the alt text // Image with proper alt inside link should preserve the alt text
expect(content).toContain('![Company Logo](http://example.com/logo.png)'); expect(content).toContain('![Company Logo](http://example.com/logo.png)');
// Image with title but empty alt should use title as alt text (title takes precedence over filename)
expect(content).toContain('![Delicious Piroggen](http://example.com/wp-content/uploads/2020/03/dish.jpg');
// Should NOT have empty image alt text (the broken pattern we're fixing) // Should NOT have empty image alt text (the broken pattern we're fixing)
expect(content).not.toMatch(/!\[\]\([^)]+\)/); expect(content).not.toMatch(/!\[\]\([^)]+\)/);
}); });