fix: use alt -> title -> filename
This commit is contained in:
@@ -201,10 +201,13 @@ export class ImportAnalysisEngine {
|
||||
// - Otherwise, use the original image src
|
||||
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();
|
||||
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 filename = urlPath.split('/').pop() || '';
|
||||
altText = filename;
|
||||
|
||||
@@ -111,10 +111,13 @@ export class ImportExecutionEngine extends EventEmitter {
|
||||
// - Otherwise, use the original image src
|
||||
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();
|
||||
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 filename = urlPath.split('/').pop() || '';
|
||||
altText = filename;
|
||||
|
||||
@@ -312,7 +312,9 @@ with multiple lines</pre>]]></content:encoded>
|
||||
<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>
|
||||
<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>
|
||||
<wp:post_id>107</wp:post_id>
|
||||
<wp:post_date>2024-01-07 10:00:00</wp:post_date>
|
||||
|
||||
@@ -432,6 +432,9 @@ describe('ImportExecutionEngine E2E Tests', () => {
|
||||
// Image with proper alt inside link should preserve the alt text
|
||||
expect(content).toContain('');
|
||||
|
||||
// Image with title but empty alt should use title as alt text (title takes precedence over filename)
|
||||
expect(content).toContain(';
|
||||
|
||||
// Should NOT have empty image alt text (the broken pattern we're fixing)
|
||||
expect(content).not.toMatch(/!\[\]\([^)]+\)/);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user