metascraper-x 5.49.8 → 5.49.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +1 -7
  2. package/package.json +2 -2
  3. package/src/index.js +34 -14
package/README.md CHANGED
@@ -20,17 +20,11 @@ $ npm install metascraper-x --save
20
20
 
21
21
  #### options
22
22
 
23
- ##### resolveUrls
24
-
25
- Type: `boolean`
26
-
27
- Set to `true` if you want to resolve `t.co` URLs into the final URL.
28
-
29
23
  ##### resolveUrl
30
24
 
31
25
  Type: `function`
32
26
 
33
- The function implementation that resolves `t.co` into a the final URL.
27
+ The function implementation that resolves URLs (e.g., `t.co`) into the final URL.
34
28
 
35
29
  ## License
36
30
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "metascraper-x",
3
3
  "description": "Metascraper integration with x.com",
4
4
  "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-x",
5
- "version": "5.49.8",
5
+ "version": "5.49.10",
6
6
  "types": "src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "author": {
@@ -39,5 +39,5 @@
39
39
  "test": "NODE_PATH=.. TZ=UTC ava --timeout 15s"
40
40
  },
41
41
  "license": "MIT",
42
- "gitHead": "327e130cb3825bf1e05220f19d3ffecb2e7178ed"
42
+ "gitHead": "49b73e47537a43054208b9c6d80b906fa3890395"
43
43
  }
package/src/index.js CHANGED
@@ -25,31 +25,51 @@ const test = memoizeOne(url =>
25
25
  ['twitter.com', 'x.com'].includes(parseUrl(url).domain)
26
26
  )
27
27
 
28
- module.exports = ({ resolveUrls = false, resolveUrl = url => url } = {}) => {
28
+ const getAuthorName = memoizeOne(ogTitle =>
29
+ ogTitle?.split(' on X:')[0].split(' (@')[0].trim()
30
+ )
31
+
32
+ module.exports = ({ resolveUrl = url => url } = {}) => {
29
33
  const rules = {
30
34
  author: [
31
- toAuthor($ => {
32
- const author = $('meta[property="og:title"]').attr('content')
33
- return author?.includes(' on X') ? author.split(' on X')[0] : author
34
- })
35
- ],
36
- title: [
37
- toTitle(
38
- ($, url) => `@${new URL(url).pathname.toString().split('/')[1]} on X`
35
+ toAuthor($ =>
36
+ getAuthorName($('meta[property="og:title"]').attr('content'))
39
37
  )
40
38
  ],
41
- url: [
42
- toUrl($ =>
43
- $('link[rel="canonical"]').attr('href')?.replace('twitter.com', 'x.com')
44
- )
39
+ title: [
40
+ toTitle(($, url) => {
41
+ const username = new URL(url).pathname.split('/')[1]
42
+ const authorName = getAuthorName(
43
+ $('meta[property="og:title"]').attr('content')
44
+ )
45
+ return authorName
46
+ ? `${authorName} (@${username}) on X`
47
+ : `@${username} on X`
48
+ })
45
49
  ],
50
+ url: [toUrl($ => $('link[rel="canonical"]').attr('href'))],
46
51
  description: [
47
52
  toDescription(async $ => {
48
53
  let description =
49
54
  $jsonld('mainEntity.description')($) ||
50
55
  $('meta[property="og:description"]').attr('content')
51
56
 
52
- if (!resolveUrls) return description
57
+ if (!description) {
58
+ const ogTitle = $('meta[property="og:title"]').attr('content')
59
+ if (ogTitle?.includes(' on X: "')) {
60
+ description = ogTitle.split(' on X: "')[1].split('" / X')[0]
61
+ const urls = getUrls(description)
62
+ if (urls.length > 1) {
63
+ const lastUrl = urls[urls.length - 1]
64
+ if (description.endsWith(lastUrl)) {
65
+ description = description.replace(lastUrl, '').trim()
66
+ }
67
+ }
68
+ }
69
+ }
70
+
71
+ if (!description) return
72
+
53
73
  const urls = getUrls(description)
54
74
  const resolvedUrls = await Promise.all(urls.map(resolveUrl))
55
75