metascraper-readability 5.46.0-beta.1 → 5.46.0-beta.2

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 +13 -0
  2. package/package.json +4 -4
  3. package/src/index.js +31 -21
package/README.md CHANGED
@@ -14,6 +14,19 @@
14
14
  $ npm install metascraper-readability --save
15
15
  ```
16
16
 
17
+ ## API
18
+
19
+ ### metascraper-readability([options])
20
+
21
+ #### options
22
+
23
+ ##### getDocument
24
+
25
+ Type: `function`<br>
26
+ Default: [source code](https://github.com/microlinkhq/metascraper/blob/master/packages/metascraper-readability/src/index.js#L14-L20)
27
+
28
+ The function to be called to serialized html into a DOM document.
29
+
17
30
  ## License
18
31
 
19
32
  **metascraper-readability** © [Microlink](https://microlink.io), released under the [MIT](https://github.com/microlinkhq/metascraper/blob/master/LICENSE.md) License.<br>
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "metascraper-readability",
3
3
  "description": "A Readability connector for metascraper",
4
4
  "homepage": "https://github.com/microlinkhq/metascraper/packages/metascraper-readability",
5
- "version": "5.46.0-beta.1",
5
+ "version": "5.46.0-beta.2",
6
6
  "types": "src/index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "author": {
@@ -23,9 +23,9 @@
23
23
  "url"
24
24
  ],
25
25
  "dependencies": {
26
- "@metascraper/helpers": "5.46.0-beta.1",
26
+ "@metascraper/helpers": "5.46.0-beta.2",
27
27
  "@mozilla/readability": "~0.5.0",
28
- "jsdom": "~25.0.1"
28
+ "happy-dom": "~16.5.3"
29
29
  },
30
30
  "devDependencies": {
31
31
  "ava": "5"
@@ -40,5 +40,5 @@
40
40
  "test": "NODE_PATH=.. TZ=UTC ava --timeout 15s"
41
41
  },
42
42
  "license": "MIT",
43
- "gitHead": "e2310394e475e90e59061967414f1a84b8978068"
43
+ "gitHead": "2938f144fde59e245281fa21d62ede6c15d57084"
44
44
  }
package/src/index.js CHANGED
@@ -1,9 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const { memoizeOne, composeRule } = require('@metascraper/helpers')
4
-
5
4
  const { Readability } = require('@mozilla/readability')
6
- const { JSDOM, VirtualConsole } = require('jsdom')
7
5
 
8
6
  const parseReader = reader => {
9
7
  try {
@@ -13,24 +11,36 @@ const parseReader = reader => {
13
11
  }
14
12
  }
15
13
 
16
- const readability = memoizeOne((url, html) => {
17
- const dom = new JSDOM(html, { url, virtualConsole: new VirtualConsole() })
18
- const reader = new Readability(dom.window.document)
19
- return parseReader(reader)
20
- }, memoizeOne.EqualityFirstArgument)
21
-
22
- const getReadbility = composeRule(($, url) => readability(url, $.html()))
23
-
24
- module.exports = () => {
25
- const rules = {
26
- author: getReadbility({ from: 'byline', to: 'author' }),
27
- description: getReadbility({ from: 'excerpt', to: 'description' }),
28
- lang: getReadbility({ from: 'lang' }),
29
- publisher: getReadbility({ from: 'siteName', to: 'publisher' }),
30
- title: getReadbility({ from: 'title' })
31
- }
32
-
33
- rules.pkgName = 'metascraper-readability'
14
+ const defaultGetDocument = ({ url, html }) => {
15
+ const { Window } = require('happy-dom')
16
+ const window = new Window({ url })
17
+ const document = window.document
18
+ document.documentElement.innerHTML = html
19
+ return document
20
+ }
34
21
 
35
- return rules
22
+ module.exports = ({ getDocument = defaultGetDocument } = {}) => {
23
+ const readability = memoizeOne((url, html, getDocument) => {
24
+ const document = getDocument({ url, html })
25
+ const reader = new Readability(document)
26
+ return parseReader(reader)
27
+ }, memoizeOne.EqualityFirstArgument)
28
+
29
+ const getReadbility = composeRule(($, url) =>
30
+ readability(url, $.html(), getDocument)
31
+ )
32
+
33
+ module.exports = () => {
34
+ const rules = {
35
+ author: getReadbility({ from: 'byline', to: 'author' }),
36
+ description: getReadbility({ from: 'excerpt', to: 'description' }),
37
+ lang: getReadbility({ from: 'lang' }),
38
+ publisher: getReadbility({ from: 'siteName', to: 'publisher' }),
39
+ title: getReadbility({ from: 'title' })
40
+ }
41
+
42
+ rules.pkgName = 'metascraper-readability'
43
+
44
+ return rules
45
+ }
36
46
  }