Skip to content

Commit f7bc443

Browse files
committed
Add OpenGraphScraperOptions.jsonLDOptions.throwOnJSONParseError and change default behavior to not throw on JSON-LD string parse errors
1 parent 686fcc5 commit f7bc443

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

lib/extract.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,16 @@ export default function extractMetaTags(body: string, options: OpenGraphScraperO
9999
if (scriptText) {
100100
scriptText = scriptText.replace(/(\r\n|\n|\r)/gm, ''); // remove newlines
101101
scriptText = unescapeScriptText(scriptText);
102-
ogObject.jsonLD.push(JSON.parse(scriptText));
102+
try {
103+
ogObject.jsonLD.push(JSON.parse(scriptText));
104+
} catch (error: unknown) {
105+
if (options.jsonLDOptions?.logOnJSONParseError) {
106+
console.error('Error parsing JSON-LD script tag:', error);
107+
}
108+
if (options.jsonLDOptions?.throwOnJSONParseError) {
109+
throw error;
110+
}
111+
}
103112
}
104113
}
105114
});

lib/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface OpenGraphScraperOptions {
3838
timeout?: number;
3939
url?: string;
4040
urlValidatorSettings?: ValidatorSettings;
41+
jsonLDOptions?: JSONLDOptions;
4142
}
4243

4344
/**
@@ -67,6 +68,14 @@ export interface ValidatorSettings {
6768
validate_length: boolean;
6869
}
6970

71+
/**
72+
* Options for the JSON-LD parser
73+
*/
74+
export interface JSONLDOptions {
75+
throwOnJSONParseError?: boolean;
76+
logOnJSONParseError?: boolean;
77+
}
78+
7079
/**
7180
* The type for user defined custom meta tags you want to scrape.
7281
*

tests/unit/static.spec.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,75 @@ describe('static check meta tags', function () {
279279
});
280280
});
281281

282+
it('jsonLD - invalid JSON string that cannot be parsed does not throw error', function () {
283+
const metaHTML = `<html><head>
284+
<script type="application/ld+json">
285+
{
286+
"@context": "http://schema.org",
287+
"@type": "Organization",
288+
"name": "Blah ",
289+
"sameAs": [
290+
"https:\\\\/\\\\/twitter.com\\\\/blah?lang=en"
291+
"https:\\\\/\\\\/www.facebook.com\\\\/blah\\\\/"
292+
""
293+
"https:\\\\/\\\\/www.instagram.com\\\\/blah\\\\/"
294+
""
295+
""
296+
"https:\\\\/\\\\/www.youtube.com\\\\/@blah"
297+
""
298+
],
299+
"url": "https:\\\\/\\\\/blah.com"
300+
}
301+
302+
</script>
303+
</head></html>`;
304+
305+
mockAgent.get('http://www.test.com')
306+
.intercept({ path: '/' })
307+
.reply(200, metaHTML);
308+
309+
return ogs({ url: 'www.test.com' })
310+
.then(function (data) {
311+
expect(data.result.success).to.be.eql(true);
312+
expect(data.result.requestUrl).to.be.eql('http://www.test.com');
313+
expect(data.result.jsonLD).to.be.eql([]);
314+
expect(data.html).to.be.eql(metaHTML);
315+
expect(data.response).to.be.a('response');
316+
});
317+
});
318+
319+
it('jsonLD - invalid JSON string that cannot be parsed throws error when options.jsonLDOptions.throwOnJSONParseError = true', function () {
320+
const metaHTML = `<html><head>
321+
<script type="application/ld+json">
322+
{
323+
"@context": "http://schema.org",
324+
"@type": "Organization",
325+
"name": "Blah ",
326+
"sameAs": [
327+
"https:\\\\/\\\\/twitter.com\\\\/blah?lang=en"
328+
"https:\\\\/\\\\/www.facebook.com\\\\/blah\\\\/"
329+
""
330+
"https:\\\\/\\\\/www.instagram.com\\\\/blah\\\\/"
331+
""
332+
""
333+
"https:\\\\/\\\\/www.youtube.com\\\\/@blah"
334+
""
335+
],
336+
"url": "https:\\\\/\\\\/blah.com"
337+
}
338+
339+
</script>
340+
</head></html>`;
341+
342+
mockAgent.get('http://www.test.com')
343+
.intercept({ path: '/' })
344+
.reply(200, metaHTML);
345+
346+
return ogs({ url: 'www.test.com', jsonLDOptions: {throwOnJSONParseError: true} }).catch((data) => {
347+
expect(data.result.success).to.be.eql(false);
348+
});
349+
});
350+
282351
it('encoding - utf-8', function () {
283352
/* eslint-disable max-len */
284353
const metaHTML = `<html><head>

0 commit comments

Comments
 (0)