Skip to content

Commit

Permalink
adding support for JSON LD
Browse files Browse the repository at this point in the history
  • Loading branch information
jshemas committed Mar 3, 2024
1 parent fa42962 commit ce77f3e
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dist/lib/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ function extractMetaTags(body, options) {
// if onlyGetOpenGraphInfo isn't set, run the open graph fallbacks
if (!options.onlyGetOpenGraphInfo) {
ogObject = (0, fallback_1.default)(ogObject, options, $, body);
$('script').each((index, script) => {
if (script.attribs.type && script.attribs.type === 'application/ld+json') {
ogObject.jsonLD = JSON.parse($(script).text());
}
});
}
return ogObject;
}
Expand Down
1 change: 1 addition & 0 deletions dist/lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export type OgObjectInteral = {
errorDetails?: Error;
favicon?: string;
fbAppId?: string;
jsonLD?: object;
modifiedTime?: string;
musicAlbum?: string;
musicAlbumDisc?: string;
Expand Down
6 changes: 6 additions & 0 deletions lib/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export default function extractMetaTags(body: string, options: OpenGraphScraperO
// if onlyGetOpenGraphInfo isn't set, run the open graph fallbacks
if (!options.onlyGetOpenGraphInfo) {
ogObject = fallback(ogObject, options, $, body);

$('script').each((index, script) => {
if (script.attribs.type && script.attribs.type === 'application/ld+json') {
ogObject.jsonLD = JSON.parse($(script).text());
}
});
}

return ogObject;
Expand Down
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export type OgObjectInteral = {
errorDetails?: Error;
favicon?: string;
fbAppId?: string;
jsonLD?: object;
modifiedTime?: string;
musicAlbum?: string;
musicAlbumDisc?: string;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"keywords": [
"app links",
"dublin core",
"json ld",
"meta tags",
"metadata",
"og",
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ describe('basic', function () {
expect(result.ogTitle).to.be.eql('Buy a domain name - Register cheap domain names from $0.99 - Namecheap');
expect(result.ogImage).to.be.an('array').and.to.not.be.empty;
expect(result.requestUrl).to.be.eql('https://www.namecheap.com/');
expect(result.jsonLD).to.be.an('object').and.to.not.be.empty;
expect(result.charset).to.be.eql('utf-8');
expect(result.success).to.be.eql(true);
expect(result).to.have.all.keys(
'jsonLD',
'favicon',
'ogTitle',
'ogDescription',
Expand Down Expand Up @@ -282,6 +284,7 @@ describe('basic', function () {
expect(result).to.have.all.keys(
'favicon',
'fbAppId',
'jsonLD',
'alAndroidAppName',
'alAndroidPackage',
'alAndroidUrl',
Expand Down Expand Up @@ -372,10 +375,12 @@ describe('basic', function () {
expect(result.ogTitle).to.be.eql('Facebook - log in or sign up');
expect(result.ogDescription).to.be.eql('Log into Facebook to start sharing and connecting with your friends, family, and people you know.');
expect(result.requestUrl).to.be.eql('https://www.facebook.com/');
expect(result.jsonLD).to.be.an('object').and.to.not.be.empty;
expect(result.charset).to.be.eql('utf-8');
expect(result.success).to.be.eql(true);
expect(result).to.have.all.keys(
'favicon',
'jsonLD',
'ogDescription',
'ogImage',
'ogLocale',
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/encoding.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ describe('encoding', function () {
url: 'https://r.r10s.jp/com/img/home/top/ogp.png',
}]);
expect(result.requestUrl).to.be.eql('https://jshemas.github.io/openGraphScraperPages/rakuten');
expect(result.jsonLD).to.be.an('object').and.to.not.be.empty;
expect(result.fbAppId).to.be.eql('194149240717705');
expect(result.charset).to.be.eql('euc-jp');
expect(result.success).to.be.eql(true);
expect(result).to.have.all.keys(
'favicon',
'jsonLD',
'ogDescription',
'ogImage',
'ogSiteName',
Expand Down Expand Up @@ -211,9 +213,11 @@ describe('encoding', function () {
expect(result.ogUrl).to.be.eql('https://blog.csdn.net/qq449245884/article/details/107096460');
expect(result.charset).to.be.eql('UTF-8');
expect(result.requestUrl).to.be.eql('https://jshemas.github.io/openGraphScraperPages/csdn');
expect(result.jsonLD).to.be.an('object').and.to.not.be.empty;
expect(result.success).to.be.eql(true);
expect(result).to.have.all.keys(
'favicon',
'jsonLD',
'charset',
'ogDescription',
'ogImage',
Expand Down Expand Up @@ -304,9 +308,11 @@ describe('encoding', function () {
expect(result.requestUrl).to.be.eql('https://jshemas.github.io/openGraphScraperPages/okezone');
expect(result.success).to.be.eql(true);
expect(result.fbAppId).to.be.eql('213195442194472');
expect(result.jsonLD).to.be.an('object').and.to.not.be.empty;
expect(result).to.have.all.keys(
'favicon',
'fbAppId',
'jsonLD',
'articleAuthor',
'articlePublishedTime',
'articlePublisher',
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ describe('image', function () {
expect(result.requestUrl).to.be.eql('https://jshemas.github.io/openGraphScraperPages/flickr');
expect(result.charset).to.be.eql('UTF-8');
expect(result.fbAppId).to.be.eql('137206539707334');
expect(result.jsonLD).to.be.an('object').and.to.not.be.empty;
expect(result.success).to.be.eql(true);
expect(result).to.have.all.keys(
'favicon',
'jsonLD',
'fbAppId',
'alIosAppName',
'alIosAppStoreId',
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/redirect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ describe('redirect', function () {
expect(result.requestUrl).to.be.eql('https://www.forbes.com/sites/kenkam/2017/09/28/3-stocks-like-apple-was-10-years-ago-tesla-nvidia-and-alibaba/#2636f6c2f0fa');
expect(result.charset).to.be.eql('utf-8');
expect(result.fbAppId).to.be.eql('123694841080850');
expect(result.jsonLD).to.be.an('object').and.to.not.be.empty;
expect(result.success).to.be.eql(true);
expect(result).to.have.all.keys(
'jsonLD',
'favicon',
'fbAppId',
'ogDate',
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/spotify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ describe('spotify', function () {
expect(result.requestUrl).to.be.eql('https://open.spotify.com/album/5EBGCvO6upi3GNknMVe9x9');
expect(result.charset).to.be.eql('utf-8');
expect(result.fbAppId).to.be.eql('174829003346');
expect(result.jsonLD).to.be.an('object').and.to.not.be.empty;
expect(result.success).to.be.eql(true);
expect(result).to.have.all.keys(
'favicon',
'fbAppId',
'jsonLD',
'alAndroidAppName',
'alAndroidPackage',
'alAndroidUrl',
Expand Down Expand Up @@ -134,10 +136,12 @@ describe('spotify', function () {
expect(result.requestUrl).to.be.eql('https://open.spotify.com/artist/5K4W6rqBFWDnAN6FQUkS6x');
expect(result.charset).to.be.eql('utf-8');
expect(result.fbAppId).to.be.eql('174829003346');
expect(result.jsonLD).to.be.an('object').and.to.not.be.empty;
expect(result.success).to.be.eql(true);
expect(result).to.have.all.keys(
'favicon',
'fbAppId',
'jsonLD',
'alAndroidAppName',
'alAndroidPackage',
'alAndroidUrl',
Expand Down Expand Up @@ -202,10 +206,12 @@ describe('spotify', function () {
expect(result.requestUrl).to.be.eql('https://open.spotify.com/track/3p6fkbeZDIVqapfdgQe6fm');
expect(result.charset).to.be.eql('utf-8');
expect(result.fbAppId).to.be.eql('174829003346');
expect(result.jsonLD).to.be.an('object').and.to.not.be.empty;
expect(result.success).to.be.eql(true);
expect(result).to.have.all.keys(
'favicon',
'fbAppId',
'jsonLD',
'alAndroidAppName',
'alAndroidPackage',
'alAndroidUrl',
Expand Down Expand Up @@ -317,10 +323,12 @@ describe('spotify', function () {
expect(result.requestUrl).to.be.eql('https://jshemas.github.io/openGraphScraperPages/spotifyPlayList');
expect(result.charset).to.be.eql('UTF-8');
expect(result.fbAppId).to.be.eql('174829003346');
expect(result.jsonLD).to.be.an('object').and.to.not.be.empty;
expect(result.success).to.be.eql(true);
expect(result).to.have.all.keys(
'favicon',
'fbAppId',
'jsonLD',
'alAndroidAppName',
'alAndroidPackage',
'alAndroidUrl',
Expand Down
Loading

0 comments on commit ce77f3e

Please sign in to comment.