2020-06-17 15:12:32 +03:00
|
|
|
const path = require('path');
|
2020-04-30 22:26:12 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-05-28 21:30:23 +03:00
|
|
|
const logging = require('../../shared/logging');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../shared/config');
|
2020-06-17 15:12:32 +03:00
|
|
|
const storage = require('../adapters/storage');
|
2020-07-02 20:03:22 +03:00
|
|
|
const imageTransform = require('@tryghost/image-transform');
|
2020-03-25 22:53:11 +03:00
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
let cardFactory;
|
|
|
|
let cards;
|
|
|
|
let mobiledocHtmlRenderer;
|
2017-12-14 14:09:54 +03:00
|
|
|
|
2020-03-19 15:18:54 +03:00
|
|
|
module.exports = {
|
2020-04-08 18:42:55 +03:00
|
|
|
get blankDocument() {
|
|
|
|
return {
|
|
|
|
version: '0.3.1',
|
2021-02-15 21:10:51 +03:00
|
|
|
ghostVersion: '4.0',
|
2020-04-08 18:42:55 +03:00
|
|
|
markups: [],
|
|
|
|
atoms: [],
|
|
|
|
cards: [],
|
|
|
|
sections: [
|
|
|
|
[1, 'p', [
|
|
|
|
[0, [], 0, '']
|
|
|
|
]]
|
|
|
|
]
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-12-14 14:09:54 +03:00
|
|
|
get cards() {
|
2020-04-08 20:21:15 +03:00
|
|
|
if (!cards) {
|
|
|
|
const CardFactory = require('@tryghost/kg-card-factory');
|
|
|
|
const defaultCards = require('@tryghost/kg-default-cards');
|
2020-03-25 22:53:11 +03:00
|
|
|
|
2020-04-08 20:21:15 +03:00
|
|
|
cardFactory = new CardFactory({
|
2020-06-11 15:27:56 +03:00
|
|
|
siteUrl: config.get('url'),
|
2020-10-19 19:05:57 +03:00
|
|
|
imageOptimization: config.get('imageOptimization'),
|
2020-07-02 20:03:22 +03:00
|
|
|
canTransformImage(storagePath) {
|
|
|
|
const {ext} = path.parse(storagePath);
|
|
|
|
|
|
|
|
return imageTransform.canTransformFiles()
|
|
|
|
&& imageTransform.canTransformFileExtension(ext)
|
|
|
|
&& typeof storage.getStorage().saveRaw === 'function';
|
|
|
|
}
|
2020-04-08 20:21:15 +03:00
|
|
|
});
|
2020-03-25 22:53:11 +03:00
|
|
|
|
2020-04-08 20:21:15 +03:00
|
|
|
cards = defaultCards.map((card) => {
|
|
|
|
return cardFactory.createCard(card);
|
|
|
|
});
|
|
|
|
}
|
2020-03-25 22:53:11 +03:00
|
|
|
|
|
|
|
return cards;
|
2017-12-14 14:09:54 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
get atoms() {
|
2020-04-08 16:49:14 +03:00
|
|
|
return require('@tryghost/kg-default-atoms');
|
2017-12-14 14:09:54 +03:00
|
|
|
},
|
|
|
|
|
2020-04-08 20:21:15 +03:00
|
|
|
get mobiledocHtmlRenderer() {
|
|
|
|
if (!mobiledocHtmlRenderer) {
|
|
|
|
const MobiledocHtmlRenderer = require('@tryghost/kg-mobiledoc-html-renderer');
|
|
|
|
|
|
|
|
mobiledocHtmlRenderer = new MobiledocHtmlRenderer({
|
|
|
|
cards: this.cards,
|
|
|
|
atoms: this.atoms,
|
|
|
|
unknownCardHandler(args) {
|
2020-04-30 22:26:12 +03:00
|
|
|
logging.error(new errors.InternalServerError({
|
2020-04-08 20:21:15 +03:00
|
|
|
message: 'Mobiledoc card \'' + args.env.name + '\' not found.'
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return mobiledocHtmlRenderer;
|
2020-03-19 15:18:54 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
get htmlToMobiledocConverter() {
|
|
|
|
try {
|
|
|
|
return require('@tryghost/html-to-mobiledoc').toMobiledoc;
|
|
|
|
} catch (err) {
|
|
|
|
return () => {
|
2020-04-30 22:26:12 +03:00
|
|
|
throw new errors.InternalServerError({
|
2020-03-19 15:18:54 +03:00
|
|
|
message: 'Unable to convert from source HTML to Mobiledoc',
|
|
|
|
context: 'The html-to-mobiledoc package was not installed',
|
|
|
|
help: 'Please review any errors from the install process by checking the Ghost logs',
|
|
|
|
code: 'HTML_TO_MOBILEDOC_INSTALLATION',
|
|
|
|
err: err
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2020-06-15 18:45:36 +03:00
|
|
|
},
|
|
|
|
|
2020-06-17 15:12:32 +03:00
|
|
|
// used when force-rerendering post content to ensure that old image card
|
|
|
|
// payloads contain width/height values to be used when generating srcsets
|
|
|
|
populateImageSizes: async function (mobiledocJson) {
|
|
|
|
// do not require image-size until it's requested to avoid circular dependencies
|
|
|
|
// shared/url-utils > server/lib/mobiledoc > server/lib/image/image-size > server/adapters/storage/utils
|
2020-12-09 15:19:22 +03:00
|
|
|
const {imageSize} = require('./image');
|
2020-06-17 15:12:32 +03:00
|
|
|
const urlUtils = require('../../shared/url-utils');
|
|
|
|
const storageInstance = storage.getStorage();
|
|
|
|
|
|
|
|
async function getUnsplashSize(url) {
|
|
|
|
const parsedUrl = new URL(url);
|
|
|
|
parsedUrl.searchParams.delete('w');
|
|
|
|
parsedUrl.searchParams.delete('fit');
|
|
|
|
parsedUrl.searchParams.delete('crop');
|
|
|
|
parsedUrl.searchParams.delete('dpr');
|
|
|
|
|
|
|
|
return await imageSize.getImageSizeFromUrl(parsedUrl.href);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getLocalSize(url) {
|
|
|
|
// local storage adapter's .exists() expects image paths without any prefixes
|
2020-06-23 14:47:20 +03:00
|
|
|
const subdirRegex = new RegExp(`^${urlUtils.getSubdir()}`);
|
|
|
|
const contentRegex = new RegExp(`^/${urlUtils.STATIC_IMAGE_URL_PREFIX}`);
|
|
|
|
const storagePath = url.replace(subdirRegex, '').replace(contentRegex, '');
|
2020-06-17 15:12:32 +03:00
|
|
|
|
|
|
|
const {dir, name, ext} = path.parse(storagePath);
|
|
|
|
const [imageNameMatched, imageName, imageNumber] = name.match(/^(.+?)(-\d+)?$/) || [null];
|
|
|
|
|
2020-07-02 20:03:22 +03:00
|
|
|
if (!imageNameMatched || !(await storageInstance.exists(storagePath))) {
|
2020-06-17 15:12:32 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the original/unoptimized image if it exists as that will have
|
|
|
|
// the maximum dimensions that srcset/handle-image-sizes can use
|
|
|
|
const originalImagePath = path.join(dir, `${imageName}_o${imageNumber || ''}${ext}`);
|
|
|
|
const imagePath = await storageInstance.exists(originalImagePath) ? originalImagePath : storagePath;
|
|
|
|
|
|
|
|
return await imageSize.getImageSizeFromStoragePath(imagePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
const mobiledoc = JSON.parse(mobiledocJson);
|
|
|
|
|
|
|
|
const sizePromises = mobiledoc.cards.map(async (card) => {
|
|
|
|
const [cardName, payload] = card;
|
|
|
|
|
2020-06-18 17:32:42 +03:00
|
|
|
const needsFilling = cardName === 'image' && payload && payload.src && (!payload.width || !payload.height);
|
2020-06-17 15:12:32 +03:00
|
|
|
if (!needsFilling) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isUnsplash = payload.src.match(/images\.unsplash\.com/);
|
|
|
|
try {
|
|
|
|
const size = isUnsplash ? await getUnsplashSize(payload.src) : await getLocalSize(payload.src);
|
|
|
|
|
|
|
|
if (size && size.width && size.height) {
|
|
|
|
payload.width = size.width;
|
|
|
|
payload.height = size.height;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// TODO: use debug instead?
|
|
|
|
logging.error(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(sizePromises);
|
|
|
|
|
|
|
|
return JSON.stringify(mobiledoc);
|
|
|
|
},
|
|
|
|
|
2020-06-15 18:45:36 +03:00
|
|
|
// allow config changes to be picked up - useful in tests
|
|
|
|
reload() {
|
|
|
|
cardFactory = null;
|
|
|
|
cards = null;
|
|
|
|
mobiledocHtmlRenderer = null;
|
2017-03-14 21:07:33 +03:00
|
|
|
}
|
|
|
|
};
|