Skip to content

Commit

Permalink
fix: improve error handling for the original author and translator fe…
Browse files Browse the repository at this point in the history
…ature (#938)
  • Loading branch information
scissorsneedfoodtoo authored Aug 1, 2024
1 parent 055c4a6 commit aaa1240
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
9 changes: 6 additions & 3 deletions utils/ghost/process-batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,14 @@ const processBatch = async ({
const originalPostData = await originalPostHandler(
[obj.codeinjection_head, obj.codeinjection_foot]
.filter(Boolean)
.join()
.join(),
obj.title
);

obj.original_post = originalPostData;
obj.html = originalPostData?.introHTML + obj.html;
if (originalPostData) {
obj.original_post = originalPostData;
obj.html = originalPostData.introHTML + obj.html;
}
}

// Stash original excerpt and escape for structured data.
Expand Down
10 changes: 7 additions & 3 deletions utils/hashnode/process-batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,15 @@ const processBatch = async ({
// it doesn't have to included in the final returned object.
if (obj?.seo?.description) {
const originalPostData = await originalPostHandler(
obj.seo.description
obj.seo.description,
obj.title
);

obj.original_post = originalPostData;
obj.html = originalPostData?.introHTML + obj.html;
if (originalPostData) {
obj.original_post = originalPostData;
obj.html = originalPostData.introHTML + obj.html;
}

delete obj.seo;
}
}
Expand Down
37 changes: 19 additions & 18 deletions utils/original-post-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ const { getCache, setCache } = require('./cache');
const getImageDimensions = require('./get-image-dimensions');
const translate = require('./translate');

const originalPostHandler = async str => {
const originalPostHandler = async (originalPostFlag, translatedPostTitle) => {
let hrefValue;
let linkText;
let originalPostObj = {};
const originalPostRegex =
/(const\s+)?fCCOriginalPost\s+=\s+("|')(?<url>.*)\2;?/gi;
const match = originalPostRegex.exec(str);
const match = originalPostRegex.exec(originalPostFlag);

if (match) {
let originalPostObj = {};
const originalPostURLObj = new URL(match.groups.url);
// Set hrefValue and linkText to the original post URL by default
let hrefValue = originalPostURLObj.href;
let linkText = originalPostURLObj.href;

try {
try {
if (match) {
const originalPostURLObj = new URL(match.groups.url);
// Set hrefValue and linkText to the original post URL by default
hrefValue = originalPostURLObj.href;
linkText = originalPostURLObj.href;
// Currently, postPathSegments is length 2 for English and Chinese
// ( ['news', 'slug'] ), and length 3 for other locales
// ( ['locale', 'news', 'slug'] )
Expand All @@ -40,7 +41,6 @@ const originalPostHandler = async str => {
const originalPostTitle = document
.querySelector('.post-full-title')
.textContent.trim();

// Get the original author's data
let originalAuthorObj = {};
const originalAuthorRelativePath =
Expand All @@ -50,7 +50,6 @@ const originalPostHandler = async str => {
.split('/')
.filter(Boolean)
.pop();

// Check if the original author data is cached
const cachedOriginalAuthor = getCache(originalAuthorSlug);
if (cachedOriginalAuthor) {
Expand All @@ -76,7 +75,6 @@ const originalPostHandler = async str => {
originalAuthorPageDocument
.querySelector('.author-bio')
?.textContent.trim() || null;

originalAuthorObj = {
name: originalAuthorName,
profile_image: originalAuthorImageSrc,
Expand All @@ -88,7 +86,6 @@ const originalPostHandler = async str => {
}
}
};

// Cache the original author data
setCache(originalAuthorSlug, originalAuthorObj);
}
Expand All @@ -103,18 +100,22 @@ const originalPostHandler = async str => {

// Use the title of the original post as link text
linkText = originalPostTitle;
} catch (err) {
console.warn(`
}
} catch (err) {
console.warn(`
---------------------------------------------------------------
Warning: Unable to fetch the post at
${match.groups.url}
"${match.groups.url}" in the post titled
"${translatedPostTitle}".
---------------------------------------------------------------
Please ensure that the Ghost instance is live and that the
post has not been deleted.
---------------------------------------------------------------
`);
}
}

// Intro HTML for the original post if there were no errors
if (hrefValue) {
const originalArticleDetailsMarkup = translate(
'original-author-translator.details.original-article',
{
Expand Down

0 comments on commit aaa1240

Please sign in to comment.