Add webp support. Provide the option to disable strict SSL to facilitate local testing.

This commit is contained in:
leon
2022-08-20 15:25:50 +01:00
committed by Will Boyd
parent a3aba85199
commit dd993b626b
4 changed files with 16 additions and 7 deletions
+2 -2
View File
@@ -113,7 +113,7 @@ function getPostCoverImageId(postData) {
function collectAttachedImages(channelData) { function collectAttachedImages(channelData) {
const images = getItemsOfType(channelData, 'attachment') const images = getItemsOfType(channelData, 'attachment')
// filter to certain image file types // filter to certain image file types
.filter(attachment => attachment.attachment_url && (/\.(gif|jpe?g|png)$/i).test(attachment.attachment_url[0])) .filter(attachment => attachment.attachment_url && (/\.(gif|jpe?g|png|webp)$/i).test(attachment.attachment_url[0]))
.map(attachment => ({ .map(attachment => ({
id: attachment.post_id[0], id: attachment.post_id[0],
postId: attachment.post_parent[0], postId: attachment.post_parent[0],
@@ -132,7 +132,7 @@ function collectScrapedImages(channelData, postTypes) {
const postContent = postData.encoded[0]; const postContent = postData.encoded[0];
const postLink = postData.link[0]; const postLink = postData.link[0];
const matches = [...postContent.matchAll(/<img[^>]*src="(.+?\.(?:gif|jpe?g|png))"[^>]*>/gi)]; const matches = [...postContent.matchAll(/<img[^>]*src="(.+?\.(?:gif|jpe?g|png|webp))"[^>]*>/gi)];
matches.forEach(match => { matches.forEach(match => {
// base the matched image URL relative to the post URL // base the matched image URL relative to the post URL
const url = new URL(match[1], postLink).href; const url = new URL(match[1], postLink).href;
+1 -1
View File
@@ -105,7 +105,7 @@ function getPostContent(postData, turndownService, config) {
if (config.saveScrapedImages) { if (config.saveScrapedImages) {
// writeImageFile() will save all content images to a relative /images // writeImageFile() will save all content images to a relative /images
// folder so update references in post content to match // folder so update references in post content to match
content = content.replace(/(<img[^>]*src=").*?([^/"]+\.(?:gif|jpe?g|png))("[^>]*>)/gi, '$1images/$2$3'); content = content.replace(/(<img[^>]*src=").*?([^/"]+\.(?:gif|jpe?g|png|webp))("[^>]*>)/gi, '$1images/$2$3');
} }
// preserve "more" separator, max one per post, optionally with custom label // preserve "more" separator, max one per post, optionally with custom label
+6
View File
@@ -74,6 +74,12 @@ const options = [
type: 'boolean', type: 'boolean',
description: 'Include custom post types and pages', description: 'Include custom post types and pages',
default: false default: false
},
{
name: 'disable-strict-ssl',
type: 'boolean',
description: 'Strict SSL prevents image retrieval from self-signed servers',
default: false
} }
]; ];
+7 -4
View File
@@ -16,7 +16,7 @@ async function processPayloadsPromise(payloads, loadFunc) {
const promises = payloads.map(payload => new Promise((resolve, reject) => { const promises = payloads.map(payload => new Promise((resolve, reject) => {
setTimeout(async () => { setTimeout(async () => {
try { try {
const data = await loadFunc(payload.item); const data = await loadFunc(payload.item, payload.strictSSL);
await writeFile(payload.destinationPath, data); await writeFile(payload.destinationPath, data);
console.log(chalk.green('[OK]') + ' ' + payload.name); console.log(chalk.green('[OK]') + ' ' + payload.name);
resolve(); resolve();
@@ -55,6 +55,7 @@ async function writeMarkdownFilesPromise(posts, config ) {
const payload = { const payload = {
item: post, item: post,
name: (config.includeOtherTypes ? post.meta.type + ' - ' : '') + post.meta.slug, name: (config.includeOtherTypes ? post.meta.type + ' - ' : '') + post.meta.slug,
strictSSL: !config.disableStrictSsl,
destinationPath, destinationPath,
delay delay
}; };
@@ -72,7 +73,7 @@ async function writeMarkdownFilesPromise(posts, config ) {
} }
} }
async function loadMarkdownFilePromise(post) { async function loadMarkdownFilePromise(post, strictSSL) {
let output = '---\n'; let output = '---\n';
Object.entries(post.frontmatter).forEach(([key, value]) => { Object.entries(post.frontmatter).forEach(([key, value]) => {
@@ -117,6 +118,7 @@ async function writeImageFilesPromise(posts, config) {
const payload = { const payload = {
item: imageUrl, item: imageUrl,
name: filename, name: filename,
strictSSL: !config.disableStrictSsl,
destinationPath, destinationPath,
delay delay
}; };
@@ -135,7 +137,7 @@ async function writeImageFilesPromise(posts, config) {
} }
} }
async function loadImageFilePromise(imageUrl) { async function loadImageFilePromise(imageUrl, strictSSL) {
// only encode the URL if it doesn't already have encoded characters // only encode the URL if it doesn't already have encoded characters
const url = (/%[\da-f]{2}/i).test(imageUrl) ? imageUrl : encodeURI(imageUrl); const url = (/%[\da-f]{2}/i).test(imageUrl) ? imageUrl : encodeURI(imageUrl);
@@ -146,7 +148,8 @@ async function loadImageFilePromise(imageUrl) {
encoding: null, // preserves binary encoding encoding: null, // preserves binary encoding
headers: { headers: {
'User-Agent': 'wordpress-export-to-markdown' 'User-Agent': 'wordpress-export-to-markdown'
} },
strictSSL: strictSSL
}); });
} catch (ex) { } catch (ex) {
if (ex.name === 'StatusCodeError') { if (ex.name === 'StatusCodeError') {