You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c690933
Browse filesBrowse the repository at this point in the historyBrowse files
@@ -43,26 +42,25 @@ To find the article, it tries three strategies in order and uses the first one t
43
42
## Install
44
43
45
44
```sh
46
-
pnpm add @readstack/parser zod
45
+
pnpm add @readstack/parser
47
46
```
48
47
49
-
-**Zod 4 is a peer dependency** Install it alongside this package.
50
48
-**Node.js 24 and ESM** This package targets Node.js 24 and ships as an ES module. Load it with `import`, or with `require` on Node.js 24.
51
49
-**pnpm and `re2`** One of the parser's dependencies uses [re2](https://github.com/uhop/node-re2) to match URLs safely on untrusted pages. re2 needs to build itself when it is installed. pnpm blocks this by default, so the install ends with an error. To fix it, run `pnpm approve-builds` and pick `re2`. The parser still works if you skip this, but it is less protected against harmful pages.
52
50
53
51
## Usage
54
52
55
53
```ts
56
-
import {parseArticle} from"@readstack/parser";
54
+
import {parseArticle} from'@readstack/parser'
57
55
58
-
const url ="https://example.com/blog/my-post";
59
-
const html =await (awaitfetch(url)).text();
56
+
const url ='https://example.com/blog/my-post'
57
+
const html =await (awaitfetch(url)).text()
60
58
61
-
const article =awaitparseArticle({url, html });
59
+
const article =awaitparseArticle({url, html})
62
60
63
-
console.log(article.title);
64
-
console.log(article.markdown);
65
-
console.log(article.meta.readingTimeInSeconds);
61
+
console.log(article.title)
62
+
console.log(article.markdown)
63
+
console.log(article.meta.readingTimeInSeconds)
66
64
```
67
65
68
66
## API
@@ -80,7 +78,7 @@ Finds the article in `html` and resolves to a `ParserOutput` object.
80
78
The parser never logs to the console on its own. To inspect what it is doing, pass an `onDebug` callback:
The `ParserOutput` type only exists while TypeScript compiles your code. Once parser output is sent somewhere, such as in an HTTP response or through a queue, nothing checks its shape. Use `parserSchema` to check it at runtime at that point. The schema is strict, so any extra field fails the check.
117
-
118
-
You can use it on its own, or place it inside your own schema. The [Readstack](https://readstack.io) parser service uses this to check each response before sending it:
119
-
120
-
```ts
121
-
import { parserSchema } from"@readstack/parser";
122
-
import { z } from"zod";
123
-
124
-
const responseSchema =z.strictObject({
125
-
data: parserSchema,
126
-
});
127
-
128
-
responseSchema.parse({ data: article });
129
-
```
130
-
131
-
To place it inside your own schema, your app and this package must use the same copy of Zod. This is why Zod is a peer dependency.
132
-
133
110
### `ParseError`
134
111
135
112
Thrown when no article can be found on the page. It usually includes a `details` property, but it can be missing:
@@ -140,15 +117,15 @@ Thrown when no article can be found on the page. It usually includes a `details`
140
117
Other errors (such as an invalid URL) are thrown as standard errors. Check the error type before reading `details`:
Most websites do not use HTML in a reliable way, so the parser does not automatically trust tags like `<article>` or `<main>`.
138
+
Most websites do not use HTML in a reliable way, so the parser does not automatically trust tags like `<article>` or `<main>`.
162
139
Instead, it looks for the section of the page with the highest concentration of article content with elements like paragraphs, headings, lists, images, tables, and code blocks. It then cleans up any leftovers like duplicate titles or link-heavy lists.
163
140
164
141
## Writing a custom parser
@@ -177,23 +154,23 @@ A parser is a class in `src/parsers/` with two methods:
177
154
The simplest approach is to extend `DefaultParser`, remove the unwanted elements, and let default detection handle the rest. This example follows the same pattern as `BbcUkParser`:
0 commit comments