diff --git a/packages/api/package.json b/packages/api/package.json index dd504e478..3434c3494 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -75,6 +75,7 @@ "graphql-middleware": "^6.0.10", "graphql-shield": "^7.5.0", "highlightjs": "^9.16.2", + "html-entities": "^2.3.2", "intercom-client": "^3.1.4", "jsdom": "^16.4.0", "jsonwebtoken": "^8.5.1", diff --git a/packages/api/src/utils/parser.ts b/packages/api/src/utils/parser.ts index d1846ad51..4f198e752 100644 --- a/packages/api/src/utils/parser.ts +++ b/packages/api/src/utils/parser.ts @@ -14,6 +14,7 @@ import { AxiosHandler } from './axios-handler' import { BloombergHandler } from './bloomberg-handler' import { GolangHandler } from './golang-handler' import * as hljs from 'highlightjs' +import { decode } from 'html-entities' const logger = buildLogger('utils.parse') @@ -333,10 +334,10 @@ const getJSONLdLinkMetadata = async ( const jsonLd = (await axios.get(jsonLdLink.href, { timeout: 5000 })).data || {} - result.byline = jsonLd['author_name'] - result.previewImage = jsonLd['thumbnail_url'] - result.siteName = jsonLd['provider_name'] - result.title = jsonLd['title'] + result.byline = decode(jsonLd['author_name']) + result.previewImage = decode(jsonLd['thumbnail_url']) + result.siteName = decode(jsonLd['provider_name']) + result.title = decode(jsonLd['title']) return result } catch (error) { diff --git a/packages/api/test/utils/parser.test.ts b/packages/api/test/utils/parser.test.ts index 96acb630a..2c877236c 100644 --- a/packages/api/test/utils/parser.test.ts +++ b/packages/api/test/utils/parser.test.ts @@ -1,9 +1,9 @@ import 'mocha' import { expect } from 'chai' import 'chai/register-should' -import { JSDOM } from 'jsdom' import fs from 'fs' import { findNewsletterUrl, isProbablyNewsletter, parsePageMetadata, parsePreparedContent } from '../../src/utils/parser' +import nock from 'nock' const load = (path: string): string => { return fs.readFileSync(path, 'utf8') @@ -70,4 +70,31 @@ describe('parsePreparedContent', async () => { ) expect(result.parsedContent?.publishedDate?.getTime()).to.equal(new Date('2016-04-05T15:27:51+00:00').getTime()) }) -}) \ No newline at end of file +}) + +describe('parsePreparedContent', async () => { + nock('https://oembeddata').get('/').reply(200, { + "version":"1.0", + "provider_name":"Hippocratic Adventures", + "provider_url":"https:\/\/www.hippocraticadventures.com", + "title":"The Ultimate Guide to Practicing Medicine in Singapore – Part 2" + }) + + it('gets metadata from external JSONLD if available', async () => { + const html = ` +
+ + + body + ` + const result = await parsePreparedContent( + 'https://example.com/', + { + document: html, + pageInfo: { } + }, + ) + expect(result.parsedContent?.title).to.equal('The Ultimate Guide to Practicing Medicine in Singapore – Part 2') + }) +})