Attempt to set publishedDate if readability fails to parse it

This can happen if JSONLD fails to load. The test page here has
an encoding issue that causes the oembed jsonld to fail to load
and then readability fails to parse the date.
This commit is contained in:
Jackson Harper 2022-03-13 21:23:47 -07:00
parent cf55f3152f
commit 5983cfe2a6
3 changed files with 632 additions and 2 deletions

View file

@ -280,6 +280,20 @@ export const parsePreparedContent = async (
const jsonLdLinkMetadata = await getJSONLdLinkMetadata(window.document)
logRecord.JSONLdParsed = jsonLdLinkMetadata
// Attempt to set the published date if readability didn't find it
if (article && !article?.publishedDate) {
try {
const publishedTime = window.document
.querySelector('meta[property="article:published_time"]')
?.getAttribute('content')
if (publishedTime) {
article.publishedDate = new Date(publishedTime)
}
} catch (error) {
logger.error('Error getting article:published_time', error)
}
}
Object.assign(article, {
content: clean,
title: article?.title || jsonLdLinkMetadata.title,

File diff suppressed because one or more lines are too long

View file

@ -3,7 +3,7 @@ import { expect } from 'chai'
import 'chai/register-should'
import { JSDOM } from 'jsdom'
import fs from 'fs'
import { findNewsletterUrl, isProbablyNewsletter, parsePageMetadata } from '../../src/utils/parser'
import { findNewsletterUrl, isProbablyNewsletter, parsePageMetadata, parsePreparedContent } from '../../src/utils/parser'
const load = (path: string): string => {
return fs.readFileSync(path, 'utf8')
@ -46,6 +46,19 @@ describe('parseMetadata', async () => {
expect(metadata?.title).to.deep.equal('Code Block Syntax Highlighting')
expect(metadata?.previewImage).to.deep.equal('https://cdn.substack.com/image/fetch/w_1200,h_600,c_fill,f_jpg,q_auto:good,fl_progressive:steep,g_auto/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F2ab1f7e8-2ca7-4011-8ccb-43d0b3bd244f_1490x2020.png')
expect(metadata?.description).to.deep.equal('Highlighted <code> in Omnivore')
})
})
describe('parsePreparedContent', async () => {
it('gets published date when JSONLD fails to load', async () => {
const html = load('./test/utils/data/stratechery-blog-post.html')
const result = await parsePreparedContent(
'https://example.com/',
{
document: html,
pageInfo: { }
},
)
expect(result.parsedContent?.publishedDate?.getTime()).to.equal(new Date('2016-04-05T15:27:51+00:00').getTime())
})
})