fix(writer): revista_imprensa publisher og:image before WebDAV (BIT-25590) #28

Merged
spark merged 1 commits from fix/BIT-25590-revista-publisher-image into main 2026-06-19 08:15:13 +01:00
3 changed files with 41 additions and 18 deletions

No files matched your search

@@ -11,6 +11,7 @@ import {
parseRevistaIndexText,
publishedAtFromPtDate,
pdfDateFromRevistaUrl,
publisherHomeUrlForRevistaLead,
resolvePublisherUrl,
} from './revista-imprensa.js';
@@ -58,6 +59,14 @@ test('resolvePublisherUrl maps Record and A Bola', () => {
assert.equal(resolvePublisherUrl('Bola (A)'), 'https://www.abola.pt/');
});
test('publisherHomeUrlForRevistaLead maps synthetic lead URL to publisher origin', () => {
assert.equal(
publisherHomeUrlForRevistaLead('https://www.abola.pt/revista-imprensa/18-06-2026/3'),
'https://www.abola.pt/',
);
assert.equal(publisherHomeUrlForRevistaLead('https://www.record.pt/modalidades/andebol/detalhe/x'), null);
});
test('leadUrlForRevistaEntry uses publisher host with stable path', () => {
const url = leadUrlForRevistaEntry('Record', 'https://portal.fpa.pt/media/x.pdf', {
index: 18,
+11
View File
@@ -42,6 +42,17 @@ export function resolvePublisherUrl(sourceLabel: string): string {
return 'https://portal.fpa.pt/media/';
}
/** Synthetic revista lead URLs are not real article pages — probe publisher origin for og:image (BIT-25590). */
export function publisherHomeUrlForRevistaLead(itemUrl: string): string | null {
try {
const u = new URL(itemUrl.trim());
if (!u.pathname.includes('/revista-imprensa/')) return null;
return `${u.origin}/`;
} catch {
return null;
}
}
export function leadUrlForRevistaEntry(
sourceLabel: string,
pdfCanonicalUrl: string,
+21 -18
View File
@@ -8,6 +8,7 @@ import {
resolveFeaturedFromWebDavArchive,
type WebDavArchiveConfig,
} from './webdav-archive.js';
import { publisherHomeUrlForRevistaLead } from './revista-imprensa.js';
export interface WpPublishConfig {
wpBase: string;
@@ -67,6 +68,18 @@ function allowedMediaUrl(href: string): boolean {
}
}
async function featuredImageFromPageHtml(
pageUrl: string,
creditFallback?: string,
): Promise<{ url: string; credit?: string } | null> {
const raw = await fetchArticleHtmlForMeta(pageUrl);
if (!raw) return null;
const parsed = extractFeaturedImageFromHtml(raw);
const abs = parsed.url ? absolutizeImageUrl(parsed.url, pageUrl) : '';
if (!abs || !allowedMediaUrl(abs)) return null;
return { url: abs, credit: parsed.credit || creditFallback };
}
/**
* Resolves a rights-cleared image URL for the article (ingest hints, then live HTML).
* Throws if none — drafts must not ship without a featured image (BIT-22288).
@@ -90,27 +103,17 @@ export async function resolveFeaturedImageForDraft(item: AggregatedItem): Promis
webDavEnvError = e instanceof Error ? e.message : String(e);
}
const raw = await fetchArticleHtmlForMeta(item.url);
if (!raw) {
if (webDavCfg) {
return resolveFeaturedFromWebDavArchive(webDavCfg, item);
if (item.sourceKind === 'revista_imprensa') {
const publisherHome = publisherHomeUrlForRevistaLead(item.url);
if (publisherHome) {
const fromPublisher = await featuredImageFromPageHtml(publisherHome, item.featuredImageCredit);
if (fromPublisher) return fromPublisher;
}
if (webDavEnvError) {
throw new Error(
`Mandatory featured image: cannot fetch article HTML for ${item.url}; WebDAV env invalid: ${webDavEnvError} (BIT-22292).`,
);
}
throw new Error(
`Mandatory featured image: cannot fetch article HTML for ${item.url}. Optionally set WRITER_WEBDAV_ENABLED=1 and WRITER_WEBDAV_* to use the federation photo archive (BIT-22292).`,
);
}
const parsed = extractFeaturedImageFromHtml(raw);
const abs = parsed.url ? absolutizeImageUrl(parsed.url, item.url) : '';
if (abs && allowedMediaUrl(abs)) {
const credit = parsed.credit || item.featuredImageCredit;
return { url: abs, credit };
}
const fromArticle = await featuredImageFromPageHtml(item.url, item.featuredImageCredit);
if (fromArticle) return fromArticle;
if (webDavCfg) {
return resolveFeaturedFromWebDavArchive(webDavCfg, item);
}