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');
|
2021-06-15 17:36:27 +03:00
|
|
|
const logging = require('@tryghost/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-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) {
|
2021-10-21 11:27:56 +03:00
|
|
|
const imageTransform = require('@tryghost/image-transform');
|
2020-07-02 20:03:22 +03:00
|
|
|
const {ext} = path.parse(storagePath);
|
|
|
|
|
2021-10-20 18:22:09 +03:00
|
|
|
// NOTE: the "saveRaw" check is smelly
|
2020-07-02 20:03:22 +03:00
|
|
|
return imageTransform.canTransformFiles()
|
|
|
|
&& imageTransform.canTransformFileExtension(ext)
|
2021-10-20 18:22:09 +03:00
|
|
|
&& typeof storage.getStorage('images').saveRaw === 'function';
|
2020-07-02 20:03:22 +03:00
|
|
|
}
|
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
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-05-14 13:39:26 +03:00
|
|
|
const size = isUnsplash ? await getUnsplashSize(payload.src) : await imageSize.getOriginalImageSizeFromStorageUrl(payload.src);
|
2020-06-17 15:12:32 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|