Merge pull request #251 from omnivore-app/fix/jsonld-encoding

Use html decoding when getting values from fetched oembed
This commit is contained in:
Jackson Harper 2022-03-17 16:53:21 -07:00 committed by GitHub
commit 7910565dbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 6 deletions

View file

@ -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",

View file

@ -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) {

View file

@ -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())
})
})
})
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 = `<html>
<head>
<link rel="alternate" type="application/json+oembed" href="https://oembeddata">
</link
</head>
<body>body</body>
</html>`
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')
})
})