Skip to content

feat: include json-ld from metadata #657

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 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions src/steps/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import { h } from 'hastscript';
import { unified } from 'unified';
import rehypeParse from 'rehype-parse';
import { cleanupHeaderValue } from '@adobe/helix-shared-utils';

function appendElement($parent, $el) {
if ($el) {
Expand All @@ -34,6 +35,13 @@ function createElement(name, ...attrs) {
return h(name, properties);
}

function sanitizeJsonLd(jsonLd) {
if (jsonLd.toLowerCase().indexOf('</script>') >= 0) {
throw new Error('script tag not allowed');
}
return JSON.stringify(JSON.parse(jsonLd.trim()));
}

/**
* @type PipelineStep
* @param {PipelineState} state
Expand All @@ -59,7 +67,13 @@ export default async function render(state, req, res) {
appendElement($head, createElement('link', 'rel', 'canonical', 'href', meta.canonical));
}

let jsonLd;
for (const [name, value] of Object.entries(meta.page)) {
if (name.toLowerCase() === 'json-ld') {
jsonLd = value;
// eslint-disable-next-line no-continue
continue;
}
const attr = name.includes(':') && !name.startsWith('twitter:') ? 'property' : 'name';
if (Array.isArray(value)) {
for (const v of value) {
Expand All @@ -71,6 +85,19 @@ export default async function render(state, req, res) {
}
appendElement($head, createElement('link', 'rel', 'alternate', 'type', 'application/xml+atom', 'href', meta.feed, 'title', `${meta.title} feed`));

// inject json ld if valid
if (jsonLd) {
const props = { type: 'application/ld+json' };
try {
jsonLd = sanitizeJsonLd(jsonLd);
} catch (e) {
jsonLd = '';
props['data-error'] = `error in json-ld: ${cleanupHeaderValue(e.message)}`;
}
const script = h('script', props, jsonLd);
$head.children.push(script);
}

// inject head.html
const headHtml = state.helixConfig?.head?.data.html;
if (headHtml) {
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/content/config-all-ld.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": 1,
"metadata": {
"data": {
"/**": [
{
"key": "json-ld",
"value": "{\"@context\":\"http://schema.org\",\"@type\":\"Product\",\"sku\":\"AA-BB-GLOBAL\"}"
}
]
},
"lastModified": "Thu, 01 Jan 1970 00:00:01 GMT"
}
}
16 changes: 16 additions & 0 deletions test/fixtures/content/page-metadata-jsonld-error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<head>
<title>Home | Helix Project Boilerplate</title>
<link rel="canonical" href="https://helix-pages.com/page-metadata-jsonld-error">
<meta property="og:title" content="Home | Helix Project Boilerplate">
<meta property="og:url" content="https://helix-pages.com/page-metadata-jsonld-error">
<meta property="og:image" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<meta property="og:image:secure_url" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Home | Helix Project Boilerplate">
<meta name="twitter:image" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<script type="application/ld+json" data-error="error in json-ld: Unexpected non-whitespace character after JSON at position 10"></script>
<link id="favicon" rel="icon" type="image/svg+xml" href="/icons/spark.svg">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="/scripts.js" type="module"></script>
<link rel="stylesheet" href="/styles.css">
</head>
11 changes: 11 additions & 0 deletions test/fixtures/content/page-metadata-jsonld-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# JSON LD Test

This is great.

+----------------------------------------------------+
| Metadata |
+================+===================================+
| title | Home \| Helix Project Boilerplate |
+----------------+-----------------------------------|
| Json-Ld | "@context":"http://schema.org" |
+----------------+-----------------------------------+
16 changes: 16 additions & 0 deletions test/fixtures/content/page-metadata-jsonld-global.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<head>
<title>Global JSON LD Test</title>
<link rel="canonical" href="https://helix-pages.com/page-metadata-jsonld-global">
<meta property="og:title" content="Global JSON LD Test">
<meta property="og:url" content="https://helix-pages.com/page-metadata-jsonld-global">
<meta property="og:image" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<meta property="og:image:secure_url" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Global JSON LD Test">
<meta name="twitter:image" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<script type="application/ld+json">{"@context":"http://schema.org","@type":"Product","sku":"AA-BB-GLOBAL"}</script>
<link id="favicon" rel="icon" type="image/svg+xml" href="/icons/spark.svg">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="/scripts.js" type="module"></script>
<link rel="stylesheet" href="/styles.css">
</head>
3 changes: 3 additions & 0 deletions test/fixtures/content/page-metadata-jsonld-global.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Global JSON LD Test

This is great.
16 changes: 16 additions & 0 deletions test/fixtures/content/page-metadata-jsonld-multi.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<head>
<title>Home | Helix Project Boilerplate</title>
<link rel="canonical" href="https://helix-pages.com/page-metadata-jsonld-multi">
<meta property="og:title" content="Home | Helix Project Boilerplate">
<meta property="og:url" content="https://helix-pages.com/page-metadata-jsonld-multi">
<meta property="og:image" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<meta property="og:image:secure_url" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Home | Helix Project Boilerplate">
<meta name="twitter:image" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<script type="application/ld+json">{"@context":"http://schema.org","@type":"Product","sku":"FOO-BAR-12345"}</script>
<link id="favicon" rel="icon" type="image/svg+xml" href="/icons/spark.svg">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="/scripts.js" type="module"></script>
<link rel="stylesheet" href="/styles.css">
</head>
13 changes: 13 additions & 0 deletions test/fixtures/content/page-metadata-jsonld-multi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# JSON LD Test

This is great.

+-------------------------------------------------------------------------------------------+
| Metadata |
+================+==========================================================================+
| title | Home \| Helix Project Boilerplate |
+----------------+--------------------------------------------------------------------------|
| json-ld | {"@context":"http://schema.org","@type":"Product","sku":"BPB-CMON-TABS"} |
+----------------+--------------------------------------------------------------------------|
| json-ld | {"@context":"http://schema.org","@type":"Product","sku":"FOO-BAR-12345"} |
+----------------+--------------------------------------------------------------------------+
16 changes: 16 additions & 0 deletions test/fixtures/content/page-metadata-jsonld-xss.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<head>
<title>Home | Helix Project Boilerplate</title>
<link rel="canonical" href="https://helix-pages.com/page-metadata-jsonld-xss">
<meta property="og:title" content="Home | Helix Project Boilerplate">
<meta property="og:url" content="https://helix-pages.com/page-metadata-jsonld-xss">
<meta property="og:image" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<meta property="og:image:secure_url" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Home | Helix Project Boilerplate">
<meta name="twitter:image" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<script type="application/ld+json" data-error="error in json-ld: script tag not allowed"></script>
<link id="favicon" rel="icon" type="image/svg+xml" href="/icons/spark.svg">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="/scripts.js" type="module"></script>
<link rel="stylesheet" href="/styles.css">
</head>
11 changes: 11 additions & 0 deletions test/fixtures/content/page-metadata-jsonld-xss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# JSON LD Test

This is great.

+-------------------------------------------------------------------------+
| Metadata |
+================+========================================================+
| title | Home \| Helix Project Boilerplate |
+----------------+--------------------------------------------------------|
| Json-Ld | { "foo:": "\</script><script>alert('hello, world.')" } |
+----------------+--------------------------------------------------------+
16 changes: 16 additions & 0 deletions test/fixtures/content/page-metadata-jsonld.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<head>
<title>Home | Helix Project Boilerplate</title>
<link rel="canonical" href="https://helix-pages.com/page-metadata-jsonld">
<meta property="og:title" content="Home | Helix Project Boilerplate">
<meta property="og:url" content="https://helix-pages.com/page-metadata-jsonld">
<meta property="og:image" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<meta property="og:image:secure_url" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Home | Helix Project Boilerplate">
<meta name="twitter:image" content="https://helix-pages.com/default-meta-image.png?width=1200&#x26;format=pjpg&#x26;optimize=medium">
<script type="application/ld+json">{"@context":"http://schema.org","@type":"Product","sku":"BPB-CMON-TABS"}</script>
<link id="favicon" rel="icon" type="image/svg+xml" href="/icons/spark.svg">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="/scripts.js" type="module"></script>
<link rel="stylesheet" href="/styles.css">
</head>
11 changes: 11 additions & 0 deletions test/fixtures/content/page-metadata-jsonld.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# JSON LD Test

This is great.

+-------------------------------------------------------------------------------------------+
| Metadata |
+================+==========================================================================+
| title | Home \| Helix Project Boilerplate |
+----------------+--------------------------------------------------------------------------|
| json-ld | {"@context":"http://schema.org","@type":"Product","sku":"BPB-CMON-TABS"} |
+----------------+--------------------------------------------------------------------------+
26 changes: 26 additions & 0 deletions test/rendering.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
}
const response = await render(url, '', expStatus);
const actHtml = response.body;
console.log(actHtml);

Check warning on line 73 in test/rendering.test.js

View workflow job for this annotation

GitHub Actions / Test

Unexpected console statement
if (expStatus === 200) {
const $actMain = new JSDOM(actHtml).window.document.querySelector(domSelector);
const $expMain = new JSDOM(expHtml).window.document.querySelector(domSelector);
Expand Down Expand Up @@ -284,6 +285,31 @@
loader.status('config-all.json', 404);
await testRender('page-metadata-twitter-fallback', 'head');
});

it('injects json ld', async () => {
loader.status('config-all.json', 404);
await testRender('page-metadata-jsonld', 'head');
});

it('chooses last json-ld if multiple', async () => {
loader.rewrite('config-all.json', 'config-all-ld.json');
await testRender('page-metadata-jsonld-multi', 'head');
});

it('injects global json ld', async () => {
loader.rewrite('config-all.json', 'config-all-ld.json');
await testRender('page-metadata-jsonld-global', 'head');
});

it('detects errors in json ld', async () => {
loader.status('config-all.json', 404);
await testRender('page-metadata-jsonld-error', 'head');
});

it('prevents xss in json ld', async () => {
loader.status('config-all.json', 404);
await testRender('page-metadata-jsonld-xss', 'head');
});
});

describe('Miscellaneous', () => {
Expand Down