Skip to content

Commit

Permalink
chore(x): better resolvedUrls encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jun 30, 2024
1 parent b421313 commit 0a82722
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/metascraper-x/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Set to `true` if you want to resolve `t.co` URLs into the final URL.

Type: `function`

A decorator to be called after `t.co` is resolved. It doesn't do anything by default.
The function implementation that resolves `t.co` into a the final URL.

## License

Expand Down
13 changes: 1 addition & 12 deletions packages/metascraper-x/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const reachableUrl = require('reachable-url')
const {
getUrls,
author,
Expand Down Expand Up @@ -45,18 +44,8 @@ module.exports = ({
toDescription(async $ => {
let description = $('meta[property="og:description"]').attr('content')
if (!resolveUrls) return description

const urls = getUrls(description)

const resolvedUrls = await Promise.all(
urls.map(async url => {
const response = await reachableUrl(url, gotOpts)
if (reachableUrl.isReachable(response)) {
return resolveUrl(response.url)
}
return url
})
)
const resolvedUrls = await Promise.all(urls.map(resolveUrl))

for (const [index, url] of resolvedUrls.entries()) {
const original = urls[index]
Expand Down
3 changes: 2 additions & 1 deletion packages/metascraper-x/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ test('from a X profile resolving URLs', async t => {
const url = 'https://x.com/Kikobeats'
const html = await readFile(resolve(__dirname, 'fixtures/profile.html'))

const resolveUrl = url => {
const resolveUrl = async shortUrl => {
const { url } = await fetch(shortUrl, { method: 'HEAD' })
const urlObj = new URL(url)
urlObj.search = ''
return urlObj.toString().replace('https://', '').replace('/', '')
Expand Down

0 comments on commit 0a82722

Please sign in to comment.