janus-parse 2.0.2 → 2.1.0
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.
- package/README.md +61 -38
- package/dist/client/client.d.ts +3 -0
- package/dist/client/client.d.ts.map +1 -0
- package/dist/{client.js → client/client.js} +1 -1
- package/dist/client/stream.d.ts +10 -0
- package/dist/client/stream.d.ts.map +1 -0
- package/dist/client/stream.js +26 -0
- package/dist/{server.d.ts → server/server.d.ts} +1 -1
- package/dist/server/server.d.ts.map +1 -0
- package/dist/{server.js → server/server.js} +1 -1
- package/dist/server/stream.d.ts +16 -0
- package/dist/server/stream.d.ts.map +1 -0
- package/dist/server/stream.js +32 -0
- package/package.json +24 -9
- package/dist/client.d.ts +0 -3
- package/dist/client.d.ts.map +0 -1
- package/dist/server.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -6,12 +6,17 @@ A lightweight, high-performance, isomorphic HTML text extractor and sanitizer bu
|
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
### What janus-parse does
|
|
10
10
|
|
|
11
|
-
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
`janus-parse` is a utility that cleans up HTML strings and extracts the raw text content.
|
|
12
|
+
|
|
13
|
+
Here is exactly what it handles:
|
|
14
|
+
|
|
15
|
+
1. **Completely removes blocked tags**: If a tag is blacklisted (like `script` or `style`), it doesn't just empty the content—it deletes the entire element so no empty HTML tags are left behind in your final string.
|
|
16
|
+
2. **Keeps specific HTML intact**: You can pass tags into `tagsToPreserve` (like `<code>` or `<iframe>`). It will keep their full HTML layout exactly as it is while stripping out generic structural tags like `<div>` or `<span>`.
|
|
17
|
+
3. **Cleans up messy whitespace**: It automatically strips out layout code indentations, tabs, and random line breaks, squashing them down into uniform, single spaces.
|
|
18
|
+
|
|
19
|
+
> **Note on implementation**: While the underlying `node-html-parser` library is excellent at reading and building HTML trees, it doesn't handle this type of conditional tag removal, selective HTML preservation, or text formatting out of the box. `janus-parse` bridges that gap by adding this specific data-cleaning layer on top of the parser.
|
|
15
20
|
|
|
16
21
|
---
|
|
17
22
|
|
|
@@ -34,9 +39,9 @@ yarn install janus-parse
|
|
|
34
39
|
|
|
35
40
|
## Usage
|
|
36
41
|
|
|
37
|
-
###
|
|
42
|
+
### Server-Side Execution (Node.js)
|
|
38
43
|
|
|
39
|
-
The `janusServer` function is asynchronous. It
|
|
44
|
+
The `janusServer` function is asynchronous. It imports [node-html-parser](https://www.npmjs.com/package/node-html-parser) to handle complex document object trees smoothly on the backend.
|
|
40
45
|
|
|
41
46
|
```typescript
|
|
42
47
|
import { janusServer } from "janus-parse/server";
|
|
@@ -52,67 +57,85 @@ const rawHtml = `
|
|
|
52
57
|
const cleanText = await janusServer(rawHtml); // Output: "Hello Universe This is a highly spaced sentence."
|
|
53
58
|
```
|
|
54
59
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
The `janusClient` function is synchronous. It ignores server node environments and hooks directly into the browser's native, hardware-optimized `DOMParser` engine.
|
|
60
|
+
The `janusServerStream` function is asynchronous. It supports streaming local or from a url.
|
|
58
61
|
|
|
59
62
|
```typescript
|
|
60
|
-
import {
|
|
63
|
+
import { janusServerStream } from "janus-parse/server/stream";
|
|
61
64
|
|
|
62
|
-
const
|
|
63
|
-
|
|
65
|
+
const cleanText = await janusServerStream({ filePath: "./sample.html" });
|
|
66
|
+
```
|
|
64
67
|
|
|
65
|
-
|
|
68
|
+
```typescript
|
|
69
|
+
import { janusServerStream } from "janus-parse/server/stream";
|
|
70
|
+
|
|
71
|
+
const cleanText = await janusServerStream({ url: "https://url.com" });
|
|
66
72
|
```
|
|
67
73
|
|
|
68
|
-
###
|
|
74
|
+
### Client-Side Execution (Browser)
|
|
69
75
|
|
|
70
|
-
|
|
76
|
+
The `janusClient` function is synchronous. It ignores server node environments and hooks directly into the browser's native, hardware-optimized `DOMParser` engine.
|
|
71
77
|
|
|
72
78
|
```typescript
|
|
73
|
-
|
|
74
|
-
tagsToRemove: ["span", "section"] // Adds additonal blacklist candidates,
|
|
75
|
-
tagsToPreserve: ["script"] // Preserves script tag in output,
|
|
76
|
-
};
|
|
77
|
-
```
|
|
79
|
+
import { janusClient } from "janus-parse/client";
|
|
78
80
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
+
const webMarkup =
|
|
82
|
+
"<div> Dynamic Web App <style>body { display: none; }</style></div>";
|
|
81
83
|
|
|
82
|
-
const
|
|
84
|
+
const textOnly = janusClient(webMarkup); // Output: " Dynamic Web App"
|
|
83
85
|
```
|
|
84
86
|
|
|
87
|
+
---
|
|
88
|
+
|
|
85
89
|
```typescript
|
|
86
|
-
import {
|
|
90
|
+
import { janusClientStream } from "janus-parse/client/stream";
|
|
87
91
|
|
|
88
|
-
const
|
|
92
|
+
const cleanText = await janusClientStream({ url: "https://url.com" });
|
|
89
93
|
```
|
|
90
94
|
|
|
91
95
|
---
|
|
92
96
|
|
|
93
97
|
## API Reference
|
|
94
98
|
|
|
95
|
-
|
|
99
|
+
#### `Config`
|
|
96
100
|
|
|
97
|
-
|
|
101
|
+
Optionanl second function parameter to fine-tune tag removal behaviors.
|
|
98
102
|
|
|
99
103
|
```typescript
|
|
100
104
|
type Config {
|
|
101
105
|
tagsToRemove?: string[];
|
|
102
106
|
tagsToPreserve?: string[];
|
|
103
|
-
}
|
|
104
|
-
```
|
|
107
|
+
};
|
|
105
108
|
|
|
106
|
-
|
|
109
|
+
type JanusClientStreamConfig = {
|
|
110
|
+
url: string;
|
|
111
|
+
config?: Config;
|
|
112
|
+
encoding?: BufferEncoding;
|
|
113
|
+
fetchOptions?: RequestInit;
|
|
114
|
+
};
|
|
107
115
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
116
|
+
type JanusServerStreamConfig = {
|
|
117
|
+
url?: string;
|
|
118
|
+
config?: Config;
|
|
119
|
+
encoding?: BufferEncoding;
|
|
120
|
+
fetchOptions?: RequestInit;
|
|
121
|
+
filePath?: string;
|
|
122
|
+
};
|
|
123
|
+
```
|
|
111
124
|
|
|
112
125
|
---
|
|
113
126
|
|
|
114
|
-
|
|
127
|
+
- `rawHtml`: The input HTML string.
|
|
128
|
+
- `config`: Optional rule blocks to override standard cleaning lists.
|
|
129
|
+
- `janusServer(rawHtml, config)`: Returns a promise that resolves to a stripped, whitespace-normalized single-line string
|
|
130
|
+
- `janusClient(rawHtml, config)`: Returns a stripped, whitespace-normalized single-line string
|
|
131
|
+
|
|
132
|
+
## 📦 Bundle Size
|
|
133
|
+
|
|
134
|
+
This package is split into targeted modules. Modern bundlers will only bundle the exact module you import, keeping your builds incredibly lightweight.
|
|
115
135
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
136
|
+
| Import Path | Target Environment | Size (Minified + Gzipped) |
|
|
137
|
+
| :-------------------------- | :----------------- | :------------------------ |
|
|
138
|
+
| `janus-parse/server` | Node.js | **~47.6 KB** |
|
|
139
|
+
| `janus-parse/server:stream` | Node.js | **~49 KB** |
|
|
140
|
+
| `janus-parse/client` | Browser | **~700 B** |
|
|
141
|
+
| `janus-parse/client:stream` | Browser | **~1 KB** |
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../lib/client/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAa7C,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,GAAE,MAAsB,UASvE"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Config } from "../utils/index.js";
|
|
2
|
+
type JanusClientStreamConfig = {
|
|
3
|
+
url: string;
|
|
4
|
+
config?: Config;
|
|
5
|
+
encoding?: BufferEncoding;
|
|
6
|
+
fetchOptions?: RequestInit;
|
|
7
|
+
};
|
|
8
|
+
export declare function janusClientStream({ url, config, encoding, fetchOptions, }: JanusClientStreamConfig): Promise<string>;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../lib/client/stream.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD,KAAK,uBAAuB,GAAG;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,YAAY,CAAC,EAAE,WAAW,CAAC;CAC5B,CAAC;AAEF,wBAAsB,iBAAiB,CAAC,EACtC,GAAG,EACH,MAAM,EACN,QAAQ,EACR,YAAY,GACb,EAAE,uBAAuB,GAAG,OAAO,CAAC,MAAM,CAAC,CAS3C"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { janusClient } from "./client.js";
|
|
2
|
+
export async function janusClientStream({ url, config, encoding, fetchOptions, }) {
|
|
3
|
+
const response = await fetch(url, fetchOptions);
|
|
4
|
+
if (!response.ok || !response.body) {
|
|
5
|
+
throw new Error(`Failed downloading stream.\n====\nURL: ${url}\n====\n
|
|
6
|
+
Status:${response.status}`);
|
|
7
|
+
}
|
|
8
|
+
return await _janusClient(response.body, config, encoding);
|
|
9
|
+
}
|
|
10
|
+
async function _janusClient(inputStream, config, encoding = "utf8") {
|
|
11
|
+
const decodedStream = inputStream.pipeThrough(new TextDecoderStream(encoding));
|
|
12
|
+
const reader = decodedStream.getReader();
|
|
13
|
+
let text = "";
|
|
14
|
+
try {
|
|
15
|
+
while (true) {
|
|
16
|
+
const { done, value } = await reader.read();
|
|
17
|
+
if (done)
|
|
18
|
+
break;
|
|
19
|
+
text += value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
finally {
|
|
23
|
+
reader.releaseLock();
|
|
24
|
+
}
|
|
25
|
+
return janusClient(text, config);
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../lib/server/server.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAa7C,wBAAsB,WAAW,CAC/B,IAAI,EAAE,MAAM,EACZ,MAAM,GAAE,MAAsB,mBAS/B"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parse } from "node-html-parser";
|
|
2
|
-
import { validateText, getTags, normalizeWhitespace, serialize, } from "
|
|
2
|
+
import { validateText, getTags, normalizeWhitespace, serialize, } from "../utils/index.js";
|
|
3
3
|
const defaultConfig = {
|
|
4
4
|
tagsToRemove: [],
|
|
5
5
|
tagsToPreserve: [],
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Config } from "../utils/index.js";
|
|
2
|
+
type ConfigOptions = {
|
|
3
|
+
encoding?: BufferEncoding;
|
|
4
|
+
fetchOptions?: RequestInit;
|
|
5
|
+
config?: Config;
|
|
6
|
+
};
|
|
7
|
+
type JanusFileConfig = Omit<ConfigOptions, "fetchOptions"> & {
|
|
8
|
+
filePath: string;
|
|
9
|
+
};
|
|
10
|
+
type JanusServerStreamConfig = ConfigOptions & {
|
|
11
|
+
url: string;
|
|
12
|
+
};
|
|
13
|
+
export declare function janusServerStream({ config, filePath, encoding, }: JanusFileConfig): Promise<string>;
|
|
14
|
+
export declare function janusServerStream({ url, encoding, fetchOptions, config, }: JanusServerStreamConfig): Promise<string>;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../lib/server/stream.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD,KAAK,aAAa,GAAG;IACnB,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,YAAY,CAAC,EAAE,WAAW,CAAC;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAMF,KAAK,eAAe,GAAG,IAAI,CAAC,aAAa,EAAE,cAAc,CAAC,GAAG;IAC3D,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AACF,KAAK,uBAAuB,GAAG,aAAa,GAAG;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAE/D,wBAAsB,iBAAiB,CAAC,EACtC,MAAM,EACN,QAAQ,EACR,QAAQ,GACT,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AACrC,wBAAsB,iBAAiB,CAAC,EACtC,GAAG,EACH,QAAQ,EACR,YAAY,EACZ,MAAM,GACP,EAAE,uBAAuB,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { open } from "node:fs/promises";
|
|
2
|
+
import { Readable } from "node:stream";
|
|
3
|
+
import { buffer } from "node:stream/consumers";
|
|
4
|
+
import { janusServer } from "./server.js";
|
|
5
|
+
export async function janusServerStream({ encoding = "utf8", filePath = "", fetchOptions = {}, config = {}, url = "", }) {
|
|
6
|
+
if (url.trim().length > 0) {
|
|
7
|
+
const response = await fetch(url, fetchOptions);
|
|
8
|
+
if (!response.ok || !response.body) {
|
|
9
|
+
throw new Error(`Failed downloading stream.\n====\nURL: ${url}\n====\n
|
|
10
|
+
Status:${response.status}`);
|
|
11
|
+
}
|
|
12
|
+
const webStream = Readable.from(response.body);
|
|
13
|
+
return await _janusServer(webStream, config, encoding);
|
|
14
|
+
}
|
|
15
|
+
let handle;
|
|
16
|
+
try {
|
|
17
|
+
handle = await open(filePath, "r"); // Read mode.
|
|
18
|
+
const file = handle.createReadStream();
|
|
19
|
+
return await _janusServer(file, config, encoding);
|
|
20
|
+
}
|
|
21
|
+
finally {
|
|
22
|
+
if (handle) {
|
|
23
|
+
await handle.close();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function _janusServer(inputStream, config, encoding = "utf8") {
|
|
28
|
+
const bytesBuffer = await buffer(inputStream);
|
|
29
|
+
const decoder = new TextDecoder(encoding);
|
|
30
|
+
const text = decoder.decode(bytesBuffer);
|
|
31
|
+
return await janusServer(text, config);
|
|
32
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "janus-parse",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"version": "2.0
|
|
5
|
-
"packageManager": "pnpm@11.
|
|
4
|
+
"version": "2.1.0",
|
|
5
|
+
"packageManager": "pnpm@11.17.0",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"html-parser",
|
|
8
8
|
"text-extractor",
|
|
@@ -16,13 +16,25 @@
|
|
|
16
16
|
"THIRD-PARTY-NOTICES.md"
|
|
17
17
|
],
|
|
18
18
|
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/server/server.d.js",
|
|
21
|
+
"import": "./dist/server/server.js"
|
|
22
|
+
},
|
|
23
|
+
"./server/stream": {
|
|
24
|
+
"types": "./dist/server/stream.d.ts",
|
|
25
|
+
"import": "./dist/server/stream.js"
|
|
26
|
+
},
|
|
27
|
+
"./client/stream": {
|
|
28
|
+
"types": "./dist/client/stream.d.ts",
|
|
29
|
+
"import": "./dist/client/stream.js"
|
|
30
|
+
},
|
|
19
31
|
"./server": {
|
|
20
|
-
"types": "./dist/server.d.ts",
|
|
21
|
-
"import": "./dist/server.js"
|
|
32
|
+
"types": "./dist/server/server.d.ts",
|
|
33
|
+
"import": "./dist/server/server.js"
|
|
22
34
|
},
|
|
23
35
|
"./client": {
|
|
24
|
-
"types": "./dist/client.d.ts",
|
|
25
|
-
"import": "./dist/client.js"
|
|
36
|
+
"types": "./dist/client/client.d.ts",
|
|
37
|
+
"import": "./dist/client/client.js"
|
|
26
38
|
}
|
|
27
39
|
},
|
|
28
40
|
"release": {
|
|
@@ -55,11 +67,13 @@
|
|
|
55
67
|
"code:lint": "pnpm eslint --flag unstable_native_nodejs_ts_config --cache .",
|
|
56
68
|
"fmt": "prettier --write .",
|
|
57
69
|
"type:check": "tsc --noEmit",
|
|
58
|
-
"build": "tsc",
|
|
59
|
-
"
|
|
70
|
+
"build": "rm -rf ./dist && tsc --project tsconfig.lib.json",
|
|
71
|
+
"build:lib": "tsc --project tsconfig.lib.json",
|
|
72
|
+
"prepublishOnly": "pnpm build",
|
|
73
|
+
"bundle:size": "tsx scripts/bundle-size.ts"
|
|
60
74
|
},
|
|
61
75
|
"dependencies": {
|
|
62
|
-
"node-html-parser": "9.0.
|
|
76
|
+
"node-html-parser": "9.0.1"
|
|
63
77
|
},
|
|
64
78
|
"devDependencies": {
|
|
65
79
|
"@eslint/js": "^10.0.1",
|
|
@@ -68,6 +82,7 @@
|
|
|
68
82
|
"@semantic-release/npm": "^13.1.5",
|
|
69
83
|
"@semantic-release/release-notes-generator": "^14.1.1",
|
|
70
84
|
"@types/node": "^26.1.1",
|
|
85
|
+
"esbuild": "^0.28.1",
|
|
71
86
|
"eslint": "^10.7.0",
|
|
72
87
|
"eslint-import-resolver-typescript": "^4.4.5",
|
|
73
88
|
"eslint-plugin-import-x": "^4.17.1",
|
package/dist/client.d.ts
DELETED
package/dist/client.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../lib/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAa5C,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,GAAE,MAAsB,UASvE"}
|
package/dist/server.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../lib/server.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAa5C,wBAAsB,WAAW,CAC/B,IAAI,EAAE,MAAM,EACZ,MAAM,GAAE,MAAsB,mBAS/B"}
|