| 1 |
( function () { |
| 2 |
document.addEventListener('DOMContentLoaded', function () { |
| 3 |
document.querySelectorAll('.bkrc2-wrap[data-schema="1"]').forEach(function (wrap) { |
| 4 |
var raw = wrap.dataset.recipe; |
| 5 |
if (!raw) return; |
| 6 |
var r; |
| 7 |
try { r = JSON.parse(raw); } catch(e) { return; } |
| 8 |
|
| 9 |
var schema = { |
| 10 |
'@context': 'https://schema.org', |
| 11 |
'@type': 'Recipe', |
| 12 |
'name': r.name || '', |
| 13 |
'description': r.description || '', |
| 14 |
'prepTime': 'PT' + (r.prepTime || '').replace(/\D+/g,'') + 'M', |
| 15 |
'cookTime': 'PT' + (r.cookTime || '').replace(/\D+/g,'') + 'M', |
| 16 |
'totalTime': 'PT' + (r.totalTime || '').replace(/\D+/g,'') + 'M', |
| 17 |
'recipeYield': r.servings || '', |
| 18 |
'recipeCuisine': r.cuisine || '', |
| 19 |
'recipeCategory': r.category || '', |
| 20 |
'recipeIngredient': r.ingredients || [], |
| 21 |
'recipeInstructions': (r.steps || []).map(function(s, i) { |
| 22 |
return { '@type': 'HowToStep', 'text': s, 'position': i + 1 }; |
| 23 |
}), |
| 24 |
'nutrition': { |
| 25 |
'@type': 'NutritionInformation', |
| 26 |
'calories': r.calories ? r.calories + ' calories' : undefined, |
| 27 |
}, |
| 28 |
'aggregateRating': r.ratingCount ? { |
| 29 |
'@type': 'AggregateRating', |
| 30 |
'ratingValue': r.rating || 5, |
| 31 |
'reviewCount': r.ratingCount, |
| 32 |
} : undefined, |
| 33 |
}; |
| 34 |
|
| 35 |
if (r.image) schema.image = r.image; |
| 36 |
|
| 37 |
// Remove undefined values |
| 38 |
schema = JSON.parse(JSON.stringify(schema)); |
| 39 |
|
| 40 |
var script = document.createElement('script'); |
| 41 |
script.type = 'application/ld+json'; |
| 42 |
script.textContent = JSON.stringify(schema); |
| 43 |
document.head.appendChild(script); |
| 44 |
}); |
| 45 |
}); |
| 46 |
}() ); |
| 47 |
|