I am building a recipe website because of this I need to markup my site with the correct JSON schema. This will allow me to appear in the enhanced elements of the SERP.
I have a working solution however it requires me to extract content that would normally be in the markdown into a JSON file. From there I can then use this content when building the schema as well as when building the page.
File Containing content
[
{
"question": "Can I use different vegetables?",
"answer": "Yes! In this recipe, we use chopped carrots, but great contenders for this recipe are:\\n - Broccoli\\n - Zucchini\\n - Mushrooms\\nHowever, keep in mind that that delicate vegetable should be cooked separately to avoid them becoming too mushy."
},
{
"question": "How much chili should I use?",
"answer": "1 teaspoon crushed chilies 🌶🌶🌶\\n½ teaspoon crushed chilies 🌶🌶\\n¼ teaspoon crushed chilies 🌶\\nThis dish also doesn’t need spice to taste great! Skip the crushed chilies altogether if you’d prefer a milder meal."
}
]
Single.html
{{- $faq := (index .Site.Data (replace .File.Dir "/" "")).faq -}}
{{- range $faq -}}
<h2 class="recipe__content-question">{{.question}}</h3>
<p class="recipe__content-answer">{{(replace (.answer | markdownify) "\\n" "<br />" ) | safeHTML }}</p>
{{- end -}}
I am then able to generate the correct schema format like this
{{- $faq := (index .Site.Data (replace .File.Dir "/" "")).faq -}}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{{- range $i, $faqItem := $faq -}}
{
"@type": "Question",
"name": "{{$faqItem.question}}",
"acceptedAnswer": {
"@type": "Answer",
"text": "{{(replace ($faqItem.answer | markdownify) "\\n" "<br/>" ) | safeHTML }}"
}
}
{{- if ne (add $i 1) (len $faq) -}},{{- end -}}
{{- end -}}
]
}
My problem with this approach is that it’s really horrible to write content in this JSON format, even things like new lines that are easy in markdown become quite unreadable.
My current idea is that I would like to be able to write all of my content in the markdown and use shortcodes to tag key areas. I would then like to be able to access an array of all FAQs on the page.
{{<FAQ>}}
{{<Question>}}
## Test question
{{</Question>}}
{{<Answer>}}
Test answer
{{</Answer>}}
{{</FAQ>}}
Has anyone come up with a better solution to this problem? or can they see a better way to improve this in Hugo?
Schema is a really important part of building a competitive webpage these days so it would be great to find a way to improve the support of it in Hugo.