janus-parse 2.0.2 → 2.0.3
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 +19 -44
- package/package.json +2 -2
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,7 +57,7 @@ const rawHtml = `
|
|
|
52
57
|
const cleanText = await janusServer(rawHtml); // Output: "Hello Universe This is a highly spaced sentence."
|
|
53
58
|
```
|
|
54
59
|
|
|
55
|
-
###
|
|
60
|
+
### Client-Side Execution (Browser)
|
|
56
61
|
|
|
57
62
|
The `janusClient` function is synchronous. It ignores server node environments and hooks directly into the browser's native, hardware-optimized `DOMParser` engine.
|
|
58
63
|
|
|
@@ -65,36 +70,13 @@ const webMarkup =
|
|
|
65
70
|
const textOnly = janusClient(webMarkup); // Output: " Dynamic Web App"
|
|
66
71
|
```
|
|
67
72
|
|
|
68
|
-
### 3. Custom Configurations
|
|
69
|
-
|
|
70
|
-
You can modify which HTML tags are targeted for destruction by passing an optional configuration object.
|
|
71
|
-
|
|
72
|
-
```typescript
|
|
73
|
-
const structuralConfig = {
|
|
74
|
-
tagsToRemove: ["span", "section"] // Adds additonal blacklist candidates,
|
|
75
|
-
tagsToPreserve: ["script"] // Preserves script tag in output,
|
|
76
|
-
};
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
```typescript
|
|
80
|
-
import { janusServer } from "janus-parse";
|
|
81
|
-
|
|
82
|
-
const processedTextServer = await janusServer(htmlSource, structuralConfig);
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
```typescript
|
|
86
|
-
import { janusClient } from "janus-parse";
|
|
87
|
-
|
|
88
|
-
const processedTextClient = janusClient(htmlSource, structuralConfig);
|
|
89
|
-
```
|
|
90
|
-
|
|
91
73
|
---
|
|
92
74
|
|
|
93
75
|
## API Reference
|
|
94
76
|
|
|
95
|
-
|
|
77
|
+
#### `Config`
|
|
96
78
|
|
|
97
|
-
|
|
79
|
+
Optionanl second function parameter to fine-tune tag removal behaviors.
|
|
98
80
|
|
|
99
81
|
```typescript
|
|
100
82
|
type Config {
|
|
@@ -103,16 +85,9 @@ type Config {
|
|
|
103
85
|
}
|
|
104
86
|
```
|
|
105
87
|
|
|
106
|
-
#### `janusServer(text: string, config?: Config): Promise<string>`
|
|
107
|
-
|
|
108
|
-
- **`text`**: The input raw HTML string.
|
|
109
|
-
- **`config`**: Optional rule blocks to override standard cleaning lists.
|
|
110
|
-
- **Returns**: A promise that resolves to a stripped, whitespace-normalized single-line string.
|
|
111
|
-
|
|
112
88
|
---
|
|
113
89
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
-
|
|
117
|
-
-
|
|
118
|
-
- **Returns**: A clean string containing target inner-text nodes parsed from the browser context.
|
|
90
|
+
- `rawHtml`: The input HTML string.
|
|
91
|
+
- `config`: Optional rule blocks to override standard cleaning lists.
|
|
92
|
+
- `janusServer(rawHtml, config)`: Returns a promise that resolves to a stripped, whitespace-normalized single-line string
|
|
93
|
+
- `janusClient(rawHtml, config)`: Returns a stripped, whitespace-normalized single-line string
|