Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(recipe-schema): add support for recipe schema in structured data #561

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@quintype/seo",
"version": "1.46.3",
"version": "1.46.4-recipe-schema.3",
"description": "SEO Modules for Quintype",
"main": "dist/index.cjs.js",
"repository": "https://github.com/quintype/quintype-node-seo",
Expand Down
28 changes: 27 additions & 1 deletion src/structured-data/schema.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { get } from "lodash";
import { getTitle } from "../generate-common-seo";
import { stripMillisecondsFromTime } from "../utils";
import { extractTextFromHtmlString, getCardAttributes, stripMillisecondsFromTime } from "../utils";
export const getSchemaContext = { "@context": "http://schema.org" };

export function getSchemaType(type) {
Expand Down Expand Up @@ -151,3 +151,29 @@ export function generateAuthorPageSchema(publisherConfig, data, url) {
},
};
}

export function generateRecipePageSchema(story) {
const { headline, url, "author-name": authorName } = story;

const cardsWithAttributes = story.cards.filter((card) => getCardAttributes(card, "cardtype"));
const cardWithIngredients = cardsWithAttributes.filter((card) =>
getCardAttributes(card, "cardtype").includes("ingredients ")
);
const ingredientsRichText = cardWithIngredients[0]["story-elements"][0].text;
const ingredients = extractTextFromHtmlString(ingredientsRichText);
const instructions = [];

return {
"@context": "https://schema.org/",
"@type": "Recipe",
name: headline,
url: url,
author: {
"@type": "Person",
name: authorName,
},
description: story.description,
recipeIngredient: ingredients,
recipeInstructions: instructions,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't instructions needed?

Copy link
Contributor

@ags1773 ags1773 Jul 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussed offline, let's remove instructions. We can add this later if needed as an enhancement. Right now it's not adding any value since we're passing empty array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

};
}
27 changes: 22 additions & 5 deletions src/structured-data/structured-data-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getQueryParams, stripMillisecondsFromTime } from "../utils";
import { generateTagsForEntity } from "./entity";
import {
generateAuthorPageSchema,
generateRecipePageSchema,
getSchemaBlogPosting,
getSchemaBreadcrumbList,
getSchemaContext,
Expand Down Expand Up @@ -265,11 +266,11 @@ function generateLiveBlogPostingData(structuredData = {}, story = {}, publisherC
function getEmbedUrl(cards) {
const playerUrlMapping = {
"dailymotion-embed-script": "dailymotion-url",
"instagram": "instagram-url",
instagram: "instagram-url",
"facebook-video": "facebook-url",
"tweet": "tweet-url",
tweet: "tweet-url",
"vimeo-video": "vimeo-url",
"brightcove-video": "player-url"
"brightcove-video": "player-url",
};

for (const card of cards) {
Expand All @@ -280,7 +281,7 @@ function getEmbedUrl(cards) {
if (elem.metadata && elem.metadata[playerUrlField]) {
return elem.metadata[playerUrlField];
}
};
}
if (elem.type === "youtube-video" && elem.subtype === null) {
if (elem.url) {
return elem.url;
Expand Down Expand Up @@ -446,7 +447,6 @@ export function StructuredDataTags({ structuredData = {} }, config, pageType, re
const isStructuredDataEmpty = Object.keys(structuredData).length === 0;
const enableBreadcrumbList = get(structuredData, ["enableBreadcrumbList"], true);
const structuredDataTags = get(structuredData, ["structuredDataTags"], []);

let articleData = {};

if (!isStructuredDataEmpty) {
Expand Down Expand Up @@ -475,6 +475,23 @@ export function StructuredDataTags({ structuredData = {} }, config, pageType, re
if (!isStructuredDataEmpty && pageType === "story-page") {
const newsArticleTags = generateNewsArticleTags();
newsArticleTags ? tags.push(storyTags(), newsArticleTags) : tags.push(storyTags());
if (story["story-template"] === "recipe") {
const recipeTags = generateRecipePageSchema(story);
recipeTags.image = Object.assign(
{
"@type": "ImageObject",
},
generateArticleImageData(story["hero-image-s3-key"], publisherConfig)
);
recipeTags.video = Object.assign(
{
"@type": "VideoObject",
},
generateVideoArticleData(structuredData, story, publisherConfig, timezone)
);

tags.push(ldJson("Recipe", recipeTags));
}
}

if (!isStructuredDataEmpty && pageType === "story-page-amp") {
Expand Down
16 changes: 16 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ export function objectToTags(object) {
.map(([key, value]) => ({ [getPropertyName(key)]: key, content: value }));
}

export function extractTextFromHtmlString(html) {
const regex = /<p>(.*?)<\/p>/g;
const textContents = [];

let match;
while ((match = regex.exec(html)) !== null) {
textContents.push(match[1]);
}

return textContents;
}

export const getCardAttributes = (card, type) => {
return card.metadata.attributes[type];
};

function getPropertyName(key) {
return key.startsWith("fb:") || key.startsWith("og:") ? "property" : "name";
}
Expand Down