Skip to content

Commit c911be2

Browse files
committed
Fixed sanity image crop downloads and added appendCroppedFilenames option
1 parent 65f018b commit c911be2

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

.changeset/big-rabbits-roll.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@bluecadet/launchpad-content": minor
3+
---
4+
5+
Fixed sanity image crop downloads. Added appendCroppedFilenames option

package-lock.json

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/content/lib/content-sources/sanity-source.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import chalk from 'chalk';
66
import jsonpath from 'jsonpath';
7+
import path from 'path';
8+
import { default as sanitizeFilename } from 'sanitize-filename';
79

810
import sanityClient from '@sanity/client';
911
import imageUrlBuilder from '@sanity/image-url';
@@ -13,6 +15,7 @@ import ContentResult, { MediaDownload } from './content-result.js';
1315
import Credentials from '../credentials.js';
1416
import { Logger } from '@bluecadet/launchpad-utils';
1517
import FileUtils from '../utils/file-utils.js';
18+
import { pathExists } from 'fs-extra';
1619

1720
/**
1821
* Options for SanitySource
@@ -30,6 +33,7 @@ export class SanityOptions extends SourceOptions {
3033
maxNumPages = -1,
3134
mergePages = false,
3235
pageNumZeroPad = 0,
36+
appendCroppedFilenames = true,
3337
...rest
3438
} = {}) {
3539
super(rest);
@@ -99,6 +103,13 @@ export class SanityOptions extends SourceOptions {
99103
* @default 0
100104
*/
101105
this.pageNumZeroPad = pageNumZeroPad;
106+
107+
/**
108+
* If an image has a crop set within Sanity, this setting will append the cropped filename to each image object as `launchpad.croppedFilename`. Set this to `false` to disable this behavior.
109+
* @type {boolean}
110+
* @default true
111+
*/
112+
this.appendCroppedFilenames = appendCroppedFilenames;
102113

103114
/**
104115
* API Token defined in your sanity project.
@@ -257,18 +268,27 @@ class SanitySource extends ContentSource {
257268
const images = jsonpath.query(content, '$..*[?(@._type=="image")]');
258269
const builder = imageUrlBuilder(this.client);
259270
for (let image of images) {
260-
if (!image._key) {
261-
// _key is only defined for derivative images. Skip if it doesn't exist
271+
if (!('crop' in image)) {
272+
// Only process images with crop properties
262273
continue;
263274
}
264275
const urlBuilder = builder.image(image);
276+
const urlStr = urlBuilder.url();
277+
const url = new URL(urlStr);
265278
const task = new MediaDownload({
266-
url: urlBuilder.url(),
279+
url: urlStr,
267280
});
268281
task.localPath = FileUtils.addFilenameSuffix(
269282
task.localPath,
270-
`_${image._key}`
283+
`_${sanitizeFilename(url.search.replace('?', ''))}`
271284
);
285+
286+
if (this.config.appendCroppedFilenames) {
287+
image.launchpad = {
288+
croppedFilename: path.basename(task.localPath)
289+
};
290+
}
291+
272292
downloads.push(task);
273293
}
274294

packages/content/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"markdown-it": "^12.2.0",
4040
"p-queue": "^7.1.0",
4141
"rimraf": "^3.0.2",
42+
"sanitize-filename": "^1.6.3",
4243
"sanitize-html": "^2.5.1",
4344
"sharp": "^0.30.7",
4445
"strip-json-comments": "^4.0.0"

0 commit comments

Comments
 (0)