Skip to content

Node.js based forum software built for the modern web

fix: closes #14533, json.stringify and escape breadcrumbs structured data

Barış Soner Uşaklı433c76801259
diff --git a/install/package.json b/install/package.json
index dd4680de7e..bed612deef 100644
--- a/install/package.json
+++ b/install/package.json
@@ -112,7 +112,7 @@
"nodebb-plugin-spam-be-gone": "2.3.4",
"nodebb-plugin-web-push": "0.8.1",
"nodebb-rewards-essentials": "1.0.3",
- "nodebb-theme-harmony": "3.1.9",
+ "nodebb-theme-harmony": "3.1.11",
"nodebb-theme-lavender": "8.0.6",
"nodebb-theme-peace": "3.0.6",
"nodebb-theme-persona": "15.1.5",
diff --git a/public/src/modules/helpers.common.js b/public/src/modules/helpers.common.js
index 9413153680..289384bc03 100644
--- a/public/src/modules/helpers.common.js
+++ b/public/src/modules/helpers.common.js
@@ -13,6 +13,7 @@ module.exports = function (utils, Benchpress, tx, relative_path) {
tx: _tx,
buildMetaTag,
buildLinkTag,
+ breadCrumbsToJSONLD,
stringify,
displayMenuItem,
stripTags,
@@ -113,6 +114,30 @@ module.exports = function (utils, Benchpress, tx, relative_path) {
return '<link ' + link + rel + as + type + sizes + title + href + hreflang + crossorigin + '/>\n\t';
}
+ function breadCrumbsToJSONLD(breadcrumbs) {
+ const self = this;
+ const breadcrumbsJSONLD = {
+ '@context': 'https://schema.org',
+ '@type': 'BreadcrumbList',
+ 'itemListElement': breadcrumbs.filter(Boolean).map((crumb, index) => {
+ const item = {
+ '@type': 'ListItem',
+ position: index + 1,
+ name: _tx.call(self, crumb.text),
+ };
+ if (index !== breadcrumbs.length - 1) {
+ item.item = crumb.url;
+ }
+ return item;
+ }),
+ };
+
+ return JSON.stringify(breadcrumbsJSONLD)
+ .replace(/</g, '\\u003C')
+ .replace(/>/g, '\\u003E')
+ .replace(/&/g, '\\u0026');
+ }
+
function stringify(obj) {
// Turns the incoming object into a JSON string
return JSON.stringify(obj).replace(/&/gm, '&amp;').replace(/</gm, '&lt;').replace(/>/gm, '&gt;')
diff --git a/src/views/partials/breadcrumbs-json-ld.tpl b/src/views/partials/breadcrumbs-json-ld.tpl
new file mode 100644
index 0000000000..1f3eae913f
--- /dev/null
+++ b/src/views/partials/breadcrumbs-json-ld.tpl
@@ -0,0 +1 @@
+<script type="application/ld+json">{{breadCrumbsToJSONLD(breadcrumbs)}}</script>
\ No newline at end of file