Skip to content

Commit 40a50e0

Browse files
authored
Merge pull request #254 from rnystrom/rn/ld-json-whitespace
fix: Trim ld+json before parsing
2 parents 211dacd + 51cd42d commit 40a50e0

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/extract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export default function extractMetaTags(body: string, options: OpenGraphScraperO
9595
$('script').each((index, script) => {
9696
if (script.attribs.type && script.attribs.type === 'application/ld+json') {
9797
if (!ogObject.jsonLD) ogObject.jsonLD = [];
98-
let scriptText = $(script).text();
98+
let scriptText = $(script).text().trim();
9999
if (scriptText) {
100100
scriptText = scriptText.replace(/(\r\n|\n|\r)/gm, ''); // remove newlines
101101
scriptText = unescapeScriptText(scriptText);

tests/unit/static.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,28 @@ describe('static check meta tags', function () {
257257
});
258258
});
259259

260+
it('jsonLD - empty with whitespace', function () {
261+
const metaHTML = `<html><head>
262+
<script type="application/ld+json">
263+
264+
265+
</script>
266+
</head></html>`;
267+
268+
mockAgent.get('http://www.test.com')
269+
.intercept({ path: '/' })
270+
.reply(200, metaHTML);
271+
272+
return ogs({ url: 'www.test.com' })
273+
.then(function (data) {
274+
expect(data.result.success).to.be.eql(true);
275+
expect(data.result.requestUrl).to.be.eql('http://www.test.com');
276+
expect(data.result.jsonLD).to.be.eql([]);
277+
expect(data.html).to.be.eql(metaHTML);
278+
expect(data.response).to.be.a('response');
279+
});
280+
});
281+
260282
it('encoding - utf-8', function () {
261283
/* eslint-disable max-len */
262284
const metaHTML = `<html><head>

0 commit comments

Comments
 (0)