Skip to content

Commit 522b1bc

Browse files
authored
Merge pull request #740 from microlinkhq/instagram
fix(instagram): handle empty cases
2 parents f9968be + 5ee96fd commit 522b1bc

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

packages/metascraper-instagram/src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ module.exports = () => {
2727
const rules = {
2828
author: ({ htmlDom: $ }) => {
2929
const title = $('meta[property="og:title"]').attr('content')
30-
const value = title.split(' on Instagram')[0]
30+
const value = title?.split(' on Instagram')[0]
3131
return author(value)
3232
},
3333
date: ({ htmlDom: $, url }) => {
3434
const description = getDescription(url, $)
35-
const dateMatch = description.match(/on ([^,]+, \d{4})/)
36-
if (dateMatch === null) return
35+
const dateMatch = description?.match(/on ([^,]+, \d{4})/)
36+
if (dateMatch === null || dateMatch === undefined) return
3737
const dateString = `${dateMatch[1]} GMT`
3838
return date(new Date(dateString))
3939
},
4040
lang: ({ htmlDom: $, url }) => {
4141
const description = getDescription(url, $)
42-
const input = description.split(': ').pop().split(' - ').pop()
42+
const input = description?.split(': ').pop()?.split(' - ').pop()
4343
return detectLang(input)
4444
},
4545
title: ({ htmlDom: $ }) =>

packages/metascraper-instagram/test/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ const metascraper = require('metascraper')([
1818
require('metascraper-url')()
1919
])
2020

21+
test('code is resilient', async t => {
22+
const url = 'https://www.instagram.com/p/CPeC-Eenc8l/'
23+
const html = ''
24+
const metadata = await metascraper({ url, html })
25+
t.snapshot(metadata)
26+
})
27+
2128
test('from photo post', async t => {
2229
const url = 'https://www.instagram.com/p/CPeC-Eenc8l/'
2330
const html = await readFile(

0 commit comments

Comments
 (0)