Skip to content

implementacion jSON-ld howTo #370

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

Merged
Merged
Changes from all 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
45 changes: 39 additions & 6 deletions src/templates/BlogItemDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ import MarkdownView from "react-showdown"
import Layout from "../components/layout"
import { Seo, BannerTop, CustomImage } from "../components/index.js"
import PropTypes from "prop-types"
import { Helmet } from "react-helmet" // Importa Helmet
import { Helmet } from "react-helmet"
import "./BlogItemDetail.scss"

const stripHtml = s => (s ? s.replace(/<[^>]+>/g, "").trim() : s)
const toISODuration = m => (m ? `PT${Math.max(0, Number(m))}M` : undefined)
const pickHowTo = (blocks = []) =>
blocks.find(b => {
const uid = b?.strapi_component || b?.__component
return uid && uid.includes("howto") && b?.tieneHowTo === true
})

const BlogDetail = ({ data }) => {
const { title, description, image, imagePage, author, seo, published_at, updated_at } =
const { title, description, image, imagePage, author, seo, published_at, updated_at, body } = // HOWTO: añadimos `body`
data?.allStrapiArticle?.nodes[0] || {}

const bannerTop = imagePage ? { title, imagePage } : { title, image }
const img = imagePage || image;
const imgWidth = img.width || img.localFile.childImageSharp.original.width;
const imgHeight = img.height || img.localFile.childImageSharp.original.height;

const img = imagePage || image;
const imgWidth = img.width || img.localFile.childImageSharp.original.width;
const imgHeight = img.height || img.localFile.childImageSharp.original.height;

const structuredData = {
"@context": "https://schema.org",
Expand Down Expand Up @@ -52,6 +59,23 @@ const imgHeight = img.height || img.localFile.childImageSharp.original.height;
},
}

const blocks = Array.isArray(body) ? body : []
const howto = pickHowTo(blocks)

const structuredHowTo = howto && {
"@context": "https://schema.org",
"@type": "HowTo",
name: howto.title || title,
description: stripHtml(howto.descripcion || howto.description),
totalTime: toISODuration(howto.totalMinutes),
tool: (howto.tools || []).map(t => t?.name).filter(Boolean),
step: (howto.steps || []).map(s => ({
"@type": "HowToStep",
name: s?.name,
text: stripHtml(s?.text),
})),
}

return (
<Layout>
<Seo
Expand All @@ -64,6 +88,14 @@ const imgHeight = img.height || img.localFile.childImageSharp.original.height;
{JSON.stringify(structuredData)}
</script>
</Helmet>
{structuredHowTo && (
<Helmet>
<script type="application/ld+json">
{JSON.stringify(structuredHowTo)}
</script>
</Helmet>
)}

<BannerTop banner={bannerTop} />
<div className="detail__container row">
<div className="col-lg-12">
Expand Down Expand Up @@ -161,6 +193,7 @@ export const query = graphql`
published_at
updated_at
destacado
body
seo {
pageTitle
pageDescription
Expand Down