Skip to content

Commit

Permalink
Add support for metadata.media for opengraph and featured images fr…
Browse files Browse the repository at this point in the history
…om WordPress API.
  • Loading branch information
zachleat committed Nov 25, 2024
1 parent f5ae0c1 commit 8960d04
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 50 deletions.
37 changes: 17 additions & 20 deletions src/DataSource/HostedWordPressApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,32 @@ class HostedWordPressApi extends DataSource {
];
}

async cleanEntry(entry, data) {
async cleanEntry(rawEntry, data) {
let metadata = {
categories: Object.keys(entry.categories),
tags: Object.keys(entry.tags),
categories: Object.keys(rawEntry.categories),
tags: Object.keys(rawEntry.tags),
};

let url = this.getUrlFromEntry(entry);
let status = this.cleanStatus(entry.status);
if(rawEntry.featured_image) {
metadata.media = {
featuredImage: rawEntry.featured_image,
};

if(entry.featured_image) {
// TODO opengraphImage: { width, height, src, mime }
metadata.featuredImage = await this.fetcher.fetchAsset(entry.featured_image, {
url,
status,
filePath: entry.filePath,
});
// backwards compatibility (not downloaded or optimized)
metadata.featuredImage = rawEntry.featured_image;
}

return {
uuid: this.getUniqueIdFromEntry(entry),
uuid: this.getUniqueIdFromEntry(rawEntry),
type: HostedWordPressApi.TYPE,
title: entry.title,
url,
authors: this.#getAuthorData(entry.author),
date: entry.date,
dateUpdated: entry.modified,
content: entry.content,
title: rawEntry.title,
url: this.getUrlFromEntry(rawEntry),
authors: this.#getAuthorData(rawEntry.author),
date: rawEntry.date,
dateUpdated: rawEntry.modified,
content: rawEntry.content,
contentType: "html",
status,
status: this.cleanStatus(rawEntry.status),
metadata,
}
}
Expand Down
55 changes: 26 additions & 29 deletions src/DataSource/WordPressApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,58 +137,55 @@ class WordPressApi extends DataSource {
}

// Supports: Title, Author, Published/Updated Dates
async cleanEntry(entry, data) {
let url = this.getUrlFromEntry(entry);
let status = this.cleanStatus(entry.status)
async cleanEntry(rawEntry, data) {
let url = this.getUrlFromEntry(rawEntry);
let status = this.cleanStatus(rawEntry.status)

let metadata = {};
if(entry.jetpack_featured_media_url) {
metadata.featuredImage = await this.fetcher.fetchAsset(entry.jetpack_featured_media_url, {
url,
status,
filePath: entry.filePath,
});
if(rawEntry.jetpack_featured_media_url || rawEntry.og_image) {
let media = {};
if(rawEntry.og_image) {
media.opengraphImage = rawEntry.og_image?.url;
}
if(rawEntry.jetpack_featured_media_url) {
media.featuredImage = rawEntry.jetpack_featured_media_url;

// backwards compatibility (not downloaded or optimized)
metadata.featuredImage = rawEntry.jetpack_featured_media_url;
}
metadata.media = media;
}

let categories = await this.#getCategories(entry.categories);
let categories = await this.#getCategories(rawEntry.categories);
if(categories.length) {
metadata.categories = categories;
}

let tags = await this.#getTags(entry.tags);
let tags = await this.#getTags(rawEntry.tags);
if(tags.length) {
metadata.tags = tags;
}

let obj = {
uuid: this.getUniqueIdFromEntry(entry),
let cleanEntry = {
uuid: this.getUniqueIdFromEntry(rawEntry),
type: WordPressApi.TYPE,
title: entry.title?.rendered,
title: rawEntry.title?.rendered,
url,
authors: await this.#getAuthors(entry.author),
date: entry.date_gmt,
dateUpdated: entry.modified_gmt,
content: entry.content.rendered,
authors: await this.#getAuthors(rawEntry.author),
date: rawEntry.date_gmt,
dateUpdated: rawEntry.modified_gmt,
content: rawEntry.content.rendered,
contentType: "html",
status,
metadata,
};

if(metadata.categories) {
// map WordPress categories for use in Eleventy tags (not WordPress metadata tags, which are different)
obj.tags = metadata.categories;
}

if(entry.og_image) {
obj.metadata.opengraphImage = {
width: entry.og_image?.width,
height: entry.og_image?.height,
src: entry.og_image?.url,
mime: entry.og_image?.type,
}
cleanEntry.tags = metadata.categories;
}

return obj;
return cleanEntry;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ class Fetcher {
getAssetLocation(assetUrl, assetContentType, contextEntry) {
let filename = Fetcher.getFilenameFromSrc(assetUrl, assetContentType);
let assetUrlLocation = Utils.pathJoin(this.#assetsFolder, filename);

// root /assets folder
if(!this.useRelativeAssets) {
return {
Expand All @@ -141,6 +140,7 @@ class Fetcher {

return {
url: assetUrlLocation,
// filePath: Utils.pathJoin(this.#outputFolder, contextPathname, assetUrlLocation),
filePath: Utils.pathJoin(contextPathname, assetUrlLocation),
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,19 @@ class Importer {
return entry.contentType === "html";
}

async fetchRelatedMedia(cleanEntry) {
let relatedMedia = cleanEntry?.metadata?.media;
if(!relatedMedia) {
return;
}

for(let mediaType in relatedMedia || {}) {
let localUrl = await this.fetcher.fetchAsset(relatedMedia[mediaType], cleanEntry);
// TODO parallel
cleanEntry.metadata.media[mediaType] = localUrl;
}
}

async getEntries(options = {}) {
let entries = [];
for(let source of this.sources) {
Expand All @@ -228,6 +241,8 @@ class Importer {
}

let promises = await Promise.allSettled(entries.map(async entry => {
await this.fetchRelatedMedia(entry);

if(Importer.isHtml(entry)) {
entry.content = await this.htmlTransformer.transform(entry.content, entry);
}
Expand Down

0 comments on commit 8960d04

Please sign in to comment.