janus-parse 1.0.0 → 1.0.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.
- package/README.md +126 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# janus-parse
|
|
2
|
+
|
|
3
|
+
A lightweight, high-performance, isomorphic HTML text extractor and sanitizer built entirely in TypeScript. It safely strips out targeted layout elements or malicious scripts and returns clean, whitespace-normalized text.
|
|
4
|
+
|
|
5
|
+
`janus-parse` provides dedicated entry points optimized for both Node.js environments and client-side browsers, ensuring minimal browser bundle overhead.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Isomorphic Architecture:** Native support for both Node.js servers and Web Browsers.
|
|
12
|
+
- **Smart Tree Shaking:** Heavy server-side dependencies are dynamically imported, keeping your browser footprint tiny.
|
|
13
|
+
- **Strict Validation:** Automatically ensures inputs are safe and structurally sound before parsing.
|
|
14
|
+
- **Built-in TypeScript Types:** Ship code confidently with full type safety and explicit configuration interfaces.
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
Install the package via your favorite package manager:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
# Using pnpm
|
|
24
|
+
pnpm install janus-parse
|
|
25
|
+
|
|
26
|
+
# Using npm
|
|
27
|
+
npm install janus-parse
|
|
28
|
+
|
|
29
|
+
# Using yarn
|
|
30
|
+
yarn install janus-parse
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
### 1. Server-Side Execution (Node.js)
|
|
38
|
+
|
|
39
|
+
The `janusServer` function is asynchronous. It dynamically imports [node-html-parser](https://www.npmjs.com/package/node-html-parser) to handle complex document object trees smoothly on the backend.
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { janusServer } from "janus-parse";
|
|
43
|
+
|
|
44
|
+
const rawHtml = `
|
|
45
|
+
<main>
|
|
46
|
+
<h1>Hello Universe</h1>
|
|
47
|
+
<script type="text/javascript">alert('unwanted execution')</script>
|
|
48
|
+
<p>This is a highly spaced sentence.</p>
|
|
49
|
+
</main>
|
|
50
|
+
`;
|
|
51
|
+
|
|
52
|
+
const cleanText = await janusServer(rawHtml);
|
|
53
|
+
console.log(cleanText);
|
|
54
|
+
// Output: "Hello Universe This is a highly spaced sentence."
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### 2. Client-Side Execution (Browser)
|
|
58
|
+
|
|
59
|
+
The `janusClient` function is synchronous. It ignores server node environments and hooks directly into the browser's native, hardware-optimized `DOMParser` engine.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { janusClient } from "janus-parse";
|
|
63
|
+
|
|
64
|
+
const webMarkup =
|
|
65
|
+
"<div> Dynamic Web App <style>body { display: none; }</style></div>";
|
|
66
|
+
|
|
67
|
+
const textOnly = janusClient(webMarkup);
|
|
68
|
+
console.log(textOnly);
|
|
69
|
+
// Output: " Dynamic Web App"
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 3. Custom Configurations
|
|
73
|
+
|
|
74
|
+
You can modify which HTML tags are targeted for destruction by passing an optional configuration object.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
const structuralConfig = {
|
|
78
|
+
addBlacklistTags: ["span", "section"], // Purge extra elements
|
|
79
|
+
removeBlacklistTags: ["script"], // Keep scripts if you are building an isolated sandbox
|
|
80
|
+
};
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { janusServer } from "janus-parse";
|
|
85
|
+
|
|
86
|
+
const processedTextServer = await janusServer(htmlSource, structuralConfig);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { janusClient } from 'janus-parse';
|
|
91
|
+
|
|
92
|
+
const processedTextClient = janusClienthtmlSource, structuralConfig);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## API Reference
|
|
98
|
+
|
|
99
|
+
### `Config`
|
|
100
|
+
|
|
101
|
+
TypeScript interface passed to fine-tune tag removal behaviors.
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
interface Config {
|
|
105
|
+
addBlacklistTags?: string[];
|
|
106
|
+
removeBlacklistTags?: string[];
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### `janusServer(text: string, config?: Config): Promise<string>`
|
|
111
|
+
|
|
112
|
+
- **`text`**: The input raw HTML string.
|
|
113
|
+
- **`config`**: Optional rule blocks to override standard cleaning lists.
|
|
114
|
+
- **Returns**: A promise that resolves to a stripped, whitespace-normalized single-line string.
|
|
115
|
+
|
|
116
|
+
### `janusClient(text: string, config?: Config): string`
|
|
117
|
+
|
|
118
|
+
- **`text`**: The input raw HTML string.
|
|
119
|
+
- **`config`**: Optional rule blocks to override standard cleaning lists.
|
|
120
|
+
- **Returns**: A clean string containing target inner-text nodes parsed from the browser context.
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
Distributed under the MIT License. See [LICENSE](LICENSE) for more information.
|