Skip to content

Commit

Permalink
remove new lines from jsonLD
Browse files Browse the repository at this point in the history
  • Loading branch information
jshemas committed Sep 17, 2024
1 parent 98e7f2b commit 12e3711
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export default function extractMetaTags(body: string, options: OpenGraphScraperO
if (!ogObject.jsonLD) ogObject.jsonLD = [];
let scriptText = $(script).text();
if (scriptText) {
scriptText = scriptText.replace(/(\r\n|\n|\r)/gm, ''); // remove newlines
scriptText = unescapeScriptText(scriptText);
ogObject.jsonLD.push(JSON.parse(scriptText));

Check warning on line 102 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (18)

Unsafe argument of type `any` assigned to a parameter of type `object`

Check warning on line 102 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (20)

Unsafe argument of type `any` assigned to a parameter of type `object`

Check warning on line 102 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (22)

Unsafe argument of type `any` assigned to a parameter of type `object`
}
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 @@ -235,6 +235,28 @@ describe('static check meta tags', function () {
});
});

it('jsonLD - with new lines', function () {
const metaHTML = `<html><head>
<script type="application/ld+json">{"foo":"
bar
"}</script>
<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([{ foo: ' bar ' }]);
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 12e3711

Please sign in to comment.