Skip to content

Commit

Permalink
fix: support array of preptime/totaltime in json-ld
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpoy committed Dec 30, 2023
1 parent a95874c commit 2829f83
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions packages/backend/src/services/json-ld.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ const getYieldFromSchema = (jsonLD) => {
if (!recipeYield) return "";

if (typeof recipeYield === "string") return recipeYield;
if (typeof recipeYield[0] === "string") return getLongestString(recipeYield);
if (Array.isArray(recipeYield) && typeof recipeYield[0] === "string") {
return getLongestString(recipeYield);
}

return "";
};
Expand All @@ -149,6 +151,8 @@ const convertToISO8601Time = (time) => {
};

const convertFromISO8601Time = (time) => {
if (!time.startsWith("PT")) return time;

return time
.replace("PT", "")
.replace("H", " Hour(s) ")
Expand All @@ -160,18 +164,28 @@ const getActiveTimeFromSchema = (jsonLD) => {
const { prepTime } = jsonLD;
if (!prepTime) return "";

if (prepTime.startsWith("PT")) return convertFromISO8601Time(prepTime);
if (typeof prepTime === "string") {
return convertFromISO8601Time(prepTime);
}
if (Array.isArray(prepTime) && prepTime[0] === "string") {
return convertFromISO8601Time(getLongestString(prepTime));
}

return prepTime;
return "";
};

const getTotalTimeFromSchema = (jsonLD) => {
const { totalTime } = jsonLD;
if (!totalTime) return "";

if (totalTime.startsWith("PT")) return convertFromISO8601Time(totalTime);
if (typeof totalTime === "string") {
return convertFromISO8601Time(totalTime);
}
if (Array.isArray(totalTime) && totalTime[0] === "string") {
return convertFromISO8601Time(getLongestString(totalTime));
}

return totalTime;
return "";
};

const getInstructionsFromSchema = (jsonLD) => {
Expand Down

0 comments on commit 2829f83

Please sign in to comment.