Skip to content

Commit

Permalink
Merge pull request #254 from rnystrom/rn/ld-json-whitespace
Browse files Browse the repository at this point in the history
fix: Trim ld+json before parsing
  • Loading branch information
jshemas authored Dec 3, 2024
2 parents 211dacd + 51cd42d commit 40a50e0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default function extractMetaTags(body: string, options: OpenGraphScraperO
$('script').each((index, script) => {
if (script.attribs.type && script.attribs.type === 'application/ld+json') {
if (!ogObject.jsonLD) ogObject.jsonLD = [];
let scriptText = $(script).text();
let scriptText = $(script).text().trim();
if (scriptText) {
scriptText = scriptText.replace(/(\r\n|\n|\r)/gm, ''); // remove newlines
scriptText = unescapeScriptText(scriptText);
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/static.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,28 @@ describe('static check meta tags', function () {
});
});

it('jsonLD - empty with whitespace', function () {
const metaHTML = `<html><head>
<script type="application/ld+json">
</script>
</head></html>`;

mockAgent.get('http://www.test.com')
.intercept({ path: '/' })
.reply(200, metaHTML);

return ogs({ url: 'www.test.com' })
.then(function (data) {
expect(data.result.success).to.be.eql(true);
expect(data.result.requestUrl).to.be.eql('http://www.test.com');
expect(data.result.jsonLD).to.be.eql([]);
expect(data.html).to.be.eql(metaHTML);
expect(data.response).to.be.a('response');
});
});

it('encoding - utf-8', function () {
/* eslint-disable max-len */
const metaHTML = `<html><head>
Expand Down

0 comments on commit 40a50e0

Please sign in to comment.