From 6d9ae5f944e74f3bd7ca6b0c18cac7c09da01d02 Mon Sep 17 00:00:00 2001 From: Clay Tercek <30105080+claytercek@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:30:59 -0500 Subject: [PATCH] add contentful fix --- .changeset/flat-poems-kiss.md | 5 +++++ .../content/sources/contentful-source.md | 9 +-------- .../content/src/plugins/media-downloader.ts | 2 +- .../__tests__/contentful-source.test.ts | 2 +- .../content/src/sources/contentful-source.ts | 20 ++++++++++++------- 5 files changed, 21 insertions(+), 17 deletions(-) create mode 100644 .changeset/flat-poems-kiss.md diff --git a/.changeset/flat-poems-kiss.md b/.changeset/flat-poems-kiss.md new file mode 100644 index 00000000..94d75b9c --- /dev/null +++ b/.changeset/flat-poems-kiss.md @@ -0,0 +1,5 @@ +--- +"@bluecadet/launchpad-content": patch +--- + +Improve contentful source compatibility with mediaDownloader diff --git a/docs/src/reference/content/sources/contentful-source.md b/docs/src/reference/content/sources/contentful-source.md index aba210dc..635ce654 100644 --- a/docs/src/reference/content/sources/contentful-source.md +++ b/docs/src/reference/content/sources/contentful-source.md @@ -69,13 +69,6 @@ Set to true to use the Preview API instead of the Content Delivery API. Requires Optionally limit queries to specific content types. This will also apply to linked assets. Types that link to other types will include up to 10 levels of child content. -### `locale` - -- **Type:** `string` -- **Default:** `'en-US'` - -Used to pull localized content. - ### `filename` - **Type:** `string` @@ -88,7 +81,7 @@ The filename where content (entries and assets metadata) will be stored. - **Type:** `string` - **Default:** `'https'` -The protocol to use for API requests. +This updates the asset urls for better compatibility with the mediaDownloader plugin. By default, asset urls have no protocol. ### `host` diff --git a/packages/content/src/plugins/media-downloader.ts b/packages/content/src/plugins/media-downloader.ts index d9771a48..d3cc1e28 100644 --- a/packages/content/src/plugins/media-downloader.ts +++ b/packages/content/src/plugins/media-downloader.ts @@ -12,7 +12,7 @@ import ResultAsyncQueue from "../utils/result-async-queue.js"; import { safeKy } from "../utils/safe-ky.js"; import { CacheProgressLogger, parsePluginConfig, queryOrUpdate } from "./contentPluginHelpers.js"; -const DEFAULT_MEDIA_PATTERN = /\.(jpe?g|png|webp|avi|mov|mp4|mpg|mpeg|webm)$/i; +const DEFAULT_MEDIA_PATTERN = /https?.*\.(jpe?g|png|webp|avi|mov|mp4|mpg|mpeg|webm)$/i; declare module "../content-plugin-driver.js" { interface ContentHookArgs { diff --git a/packages/content/src/sources/__tests__/contentful-source.test.ts b/packages/content/src/sources/__tests__/contentful-source.test.ts index a8bed830..a6bcde36 100644 --- a/packages/content/src/sources/__tests__/contentful-source.test.ts +++ b/packages/content/src/sources/__tests__/contentful-source.test.ts @@ -183,7 +183,7 @@ describe("contentfulSource", () => { { "fields": { "file": { - "url": "//test.com/preview.jpg", + "url": "https://test.com/preview.jpg", }, "title": "Preview Asset", }, diff --git a/packages/content/src/sources/contentful-source.ts b/packages/content/src/sources/contentful-source.ts index da79f6ec..8007fc75 100644 --- a/packages/content/src/sources/contentful-source.ts +++ b/packages/content/src/sources/contentful-source.ts @@ -28,8 +28,6 @@ const contentfulSourceSchema = z .describe("Required field to identify this source. Will be used as download path."), /** Required field to identify this source. Will be used as download path. */ space: z.string().describe("Your Contentful space ID."), - /** Used to pull localized images. */ - locale: z.string().default("en-US").describe("Used to pull localized images."), /** The filename you want to use for where all content (entries and assets metadata) will be stored. Defaults to 'content.json' */ filename: z .string() @@ -37,7 +35,7 @@ const contentfulSourceSchema = z .describe( "The filename you want to use for where all content (entries and assets metadata) will be stored.", ), - /** Optional. Defaults to 'https' */ + /** Optional. Defaults to 'https'. This updates the asset urls for better compatibility with the mediaDownloader plugin. By default, asset urls have no protocol. */ protocol: z.string().default("https").describe("Optional. Defaults to 'https'"), /** Optional. Defaults to 'cdn.contentful.com', or 'preview.contentful.com' if `usePreviewApi` is true */ host: z @@ -123,7 +121,7 @@ export default async function contentfulSource(options: z.input> { * Returns all entries from a sync() or getEntries() response object. */ // biome-ignore lint/suspicious/noExplicitAny: unknown type from CMS -function parseAssets(responseObj: any): Array { +function parseAssets(responseObj: any, protocol: string): Array { const assets = responseObj.assets || []; if (responseObj.includes) { // 'includes' is an object where the key = type, and the value = list of items for (const [key, items] of Object.entries(responseObj.includes)) { if (key === "Asset") { - assets.push(...(items as Array)); + const withMediaProtocols = (items as Array).map((asset) => { + if (asset.fields.file.url.startsWith("//")) { + asset.fields.file.url = `${protocol}:${asset.fields.file.url}`; + } + return asset; + }); + + assets.push(...withMediaProtocols); } } } @@ -186,7 +191,8 @@ function parseAssets(responseObj: any): Array { async function tryImportContentful() { try { - return await import("contentful"); + const contentful = await import("contentful"); + return contentful.default; } catch (error) { throw new Error('Could not find module "contentful". Make sure you have installed it.', { cause: error,