janus-parse 2.2.1 → 2.4.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 +52 -8
- package/package.json +42 -50
- package/dist/client/client.d.ts +0 -3
- package/dist/client/client.d.ts.map +0 -1
- package/dist/client/client.js +0 -12
- package/dist/client/stream.d.ts +0 -10
- package/dist/client/stream.d.ts.map +0 -1
- package/dist/client/stream.js +0 -26
- package/dist/server/server.d.ts +0 -3
- package/dist/server/server.d.ts.map +0 -1
- package/dist/server/server.js +0 -12
- package/dist/server/stream.d.ts +0 -16
- package/dist/server/stream.d.ts.map +0 -1
- package/dist/server/stream.js +0 -32
- package/dist/utils/index.d.ts +0 -19
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/index.js +0 -67
package/README.md
CHANGED
|
@@ -14,7 +14,8 @@ Here is exactly what it handles:
|
|
|
14
14
|
|
|
15
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
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. **
|
|
17
|
+
3. **Preserves tags selectively by attribute**: Use `preserveTagsWithAttrs` with `tagsToPreserve` to retain only matching tags.
|
|
18
|
+
4. **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
|
|
|
19
20
|
> **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.
|
|
20
21
|
|
|
@@ -78,8 +79,7 @@ The `janusClient` function is synchronous. It ignores server node environments a
|
|
|
78
79
|
```typescript
|
|
79
80
|
import { janusClient } from "janus-parse/client";
|
|
80
81
|
|
|
81
|
-
const webMarkup =
|
|
82
|
-
"<div> Dynamic Web App <style>body { display: none; }</style></div>";
|
|
82
|
+
const webMarkup = "<div> Dynamic Web App <style>body { display: none; }</style></div>";
|
|
83
83
|
|
|
84
84
|
const textOnly = janusClient(webMarkup); // Output: " Dynamic Web App"
|
|
85
85
|
```
|
|
@@ -98,12 +98,16 @@ const cleanText = await janusClientStream({ url: "https://url.com" });
|
|
|
98
98
|
|
|
99
99
|
#### `Config`
|
|
100
100
|
|
|
101
|
-
|
|
101
|
+
Optional second function parameter to fine-tune tag removal and preservation behaviors.
|
|
102
102
|
|
|
103
103
|
```typescript
|
|
104
|
-
type Config {
|
|
104
|
+
type Config = {
|
|
105
105
|
tagsToRemove?: string[];
|
|
106
106
|
tagsToPreserve?: string[];
|
|
107
|
+
preserveTagsWithAttrs?: string[];
|
|
108
|
+
replaceTagContents?: Record<string, string>;
|
|
109
|
+
replaceTagContentsWithAttrs?: string[];
|
|
110
|
+
replaceTagContentsAsHtml?: boolean;
|
|
107
111
|
};
|
|
108
112
|
|
|
109
113
|
type JanusClientStreamConfig = {
|
|
@@ -122,6 +126,46 @@ type JanusServerStreamConfig = {
|
|
|
122
126
|
};
|
|
123
127
|
```
|
|
124
128
|
|
|
129
|
+
When `preserveTagsWithAttrs` is provided, a preserved tag is kept only if its attributes contain one of the specified strings; otherwise, the tag's markup is removed but its contents remain. Attribute matching ignores extra whitespace and treats single and double quotes as equivalent.
|
|
130
|
+
|
|
131
|
+
Use `replaceTagContents` to replace the contents of matching tag names. By default, every occurrence of those tag names is replaced; set `replaceTagContentsWithAttrs` to restrict replacement to elements whose attributes contain one of the specified strings. Replacement values are treated as plain text by default; set `replaceTagContentsAsHtml` to `true` to parse them as HTML.
|
|
132
|
+
|
|
133
|
+
All four APIs support these configuration options: `janusServer`, `janusClient`, `janusServerStream`, and `janusClientStream`.
|
|
134
|
+
|
|
135
|
+
For example, preserve only links with `data-keep="true"`:
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
const cleanText = await janusServer(
|
|
139
|
+
'<a href="/about">About</a> <a data-keep="true">Keep this link</a>',
|
|
140
|
+
{
|
|
141
|
+
tagsToPreserve: ["a"],
|
|
142
|
+
preserveTagsWithAttrs: ['data-keep="true"'],
|
|
143
|
+
},
|
|
144
|
+
);
|
|
145
|
+
// Output: 'About <a data-keep="true">Keep this link</a>'
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Replace the contents of `div` tags with `data-keep="true"`, leaving other `div` elements unchanged:
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
const cleanText = await janusServer("<div data-keep='true'>Original</div> <div>Keep this</div>", {
|
|
152
|
+
replaceTagContents: { div: "New content." },
|
|
153
|
+
replaceTagContentsWithAttrs: ["data-keep='true'"],
|
|
154
|
+
});
|
|
155
|
+
// Output: "New content. Keep this"
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
For HTML replacement content, enable `replaceTagContentsAsHtml`:
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
const cleanText = await janusServer("<div data-keep='true'>Original <span>content.</span></div>", {
|
|
162
|
+
replaceTagContents: { div: "<strong>Updated</strong>" },
|
|
163
|
+
replaceTagContentsAsHtml: true,
|
|
164
|
+
tagsToPreserve: ["strong"],
|
|
165
|
+
});
|
|
166
|
+
// Output: <strong>Updated</strong> content.
|
|
167
|
+
```
|
|
168
|
+
|
|
125
169
|
---
|
|
126
170
|
|
|
127
171
|
- `rawHtml`: The input HTML string.
|
|
@@ -131,11 +175,11 @@ type JanusServerStreamConfig = {
|
|
|
131
175
|
|
|
132
176
|
## 📦 Bundle Size
|
|
133
177
|
|
|
134
|
-
This package is split into targeted modules. Modern bundlers will only bundle the exact module you import, keeping your builds incredibly lightweight
|
|
178
|
+
This package is split into targeted modules. Modern bundlers will only bundle the exact module you import, keeping your builds incredibly lightweight `--external:node-html-parser`.
|
|
135
179
|
|
|
136
180
|
| Import Path | Target Environment | Size (Gzipped) |
|
|
137
181
|
| :-------------------------- | :----------------- | :------------- |
|
|
138
|
-
| `janus-parse/server` | Node.js | **~
|
|
139
|
-
| `janus-parse/server:stream` | Node.js | **~
|
|
182
|
+
| `janus-parse/server` | Node.js | **~1.10 KB** |
|
|
183
|
+
| `janus-parse/server:stream` | Node.js | **~1.51 KB** |
|
|
140
184
|
| `janus-parse/client` | Browser | **~700 B** |
|
|
141
185
|
| `janus-parse/client:stream` | Browser | **~1 KB** |
|
package/package.json
CHANGED
|
@@ -1,20 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "janus-parse",
|
|
3
|
-
"
|
|
4
|
-
"version": "2.2.1",
|
|
5
|
-
"packageManager": "pnpm@12.16.1",
|
|
3
|
+
"version": "2.4.0",
|
|
6
4
|
"keywords": [
|
|
5
|
+
"clean-text",
|
|
7
6
|
"html-parser",
|
|
8
|
-
"text-extractor",
|
|
9
|
-
"html-to-text",
|
|
10
7
|
"html-sanitizer",
|
|
11
|
-
"
|
|
12
|
-
"strip-tags"
|
|
8
|
+
"html-to-text",
|
|
9
|
+
"strip-tags",
|
|
10
|
+
"text-extractor"
|
|
13
11
|
],
|
|
12
|
+
"homepage": "https://github.com/Casneil/janus-parse",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/Casneil/janus-parse/issues"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/Casneil/janus-parse"
|
|
20
|
+
},
|
|
14
21
|
"files": [
|
|
15
22
|
"dist",
|
|
16
23
|
"THIRD-PARTY-NOTICES.md"
|
|
17
24
|
],
|
|
25
|
+
"type": "module",
|
|
18
26
|
"exports": {
|
|
19
27
|
".": {
|
|
20
28
|
"types": "./dist/client/client.d.js",
|
|
@@ -37,64 +45,48 @@
|
|
|
37
45
|
"import": "./dist/client/client.js"
|
|
38
46
|
}
|
|
39
47
|
},
|
|
40
|
-
"release": {
|
|
41
|
-
"branches": [
|
|
42
|
-
"main"
|
|
43
|
-
],
|
|
44
|
-
"plugins": [
|
|
45
|
-
"@semantic-release/commit-analyzer",
|
|
46
|
-
"@semantic-release/release-notes-generator",
|
|
47
|
-
"@semantic-release/npm",
|
|
48
|
-
"@semantic-release/github"
|
|
49
|
-
]
|
|
50
|
-
},
|
|
51
|
-
"repository": {
|
|
52
|
-
"type": "git",
|
|
53
|
-
"url": "https://github.com/Casneil/janus-parse"
|
|
54
|
-
},
|
|
55
|
-
"homepage": "https://github.com/Casneil/janus-parse",
|
|
56
|
-
"bugs": {
|
|
57
|
-
"url": "https://github.com/Casneil/janus-parse/issues"
|
|
58
|
-
},
|
|
59
48
|
"publishConfig": {
|
|
60
49
|
"registry": "https://registry.npmjs.org"
|
|
61
50
|
},
|
|
62
|
-
"type": "module",
|
|
63
|
-
"prepack": "pnpm build",
|
|
64
51
|
"scripts": {
|
|
65
|
-
"janus": "tsx ./lib/server.ts",
|
|
66
52
|
"test": "vitest",
|
|
67
|
-
"
|
|
68
|
-
"fmt": "
|
|
53
|
+
"lint:fix": "oxlint --fix",
|
|
54
|
+
"fmt": "oxfmt",
|
|
55
|
+
"fmt:check": "oxfmt --check",
|
|
69
56
|
"type:check": "tsc --noEmit",
|
|
70
57
|
"build": "rm -rf ./dist && tsc --project tsconfig.lib.json",
|
|
71
58
|
"build:lib": "tsc --project tsconfig.lib.json",
|
|
72
59
|
"prepublishOnly": "pnpm build",
|
|
73
|
-
"bundle:size": "
|
|
60
|
+
"bundle:size": "node ./scripts/bundle-size.ts"
|
|
74
61
|
},
|
|
75
62
|
"dependencies": {
|
|
76
|
-
"node-html-parser": "9.0.
|
|
63
|
+
"node-html-parser": "9.0.10"
|
|
77
64
|
},
|
|
78
65
|
"devDependencies": {
|
|
79
|
-
"@
|
|
80
|
-
"@semantic-release/
|
|
81
|
-
"@semantic-release/github": "12.0.10",
|
|
66
|
+
"@semantic-release/commit-analyzer": "13.0.1",
|
|
67
|
+
"@semantic-release/github": "12.0.11",
|
|
82
68
|
"@semantic-release/npm": "13.2.0",
|
|
83
|
-
"@semantic-release/release-notes-generator": "
|
|
84
|
-
"@types/node": "26.6.
|
|
85
|
-
"eslint": "10.14.0",
|
|
69
|
+
"@semantic-release/release-notes-generator": "14.1.1",
|
|
70
|
+
"@types/node": "26.6.11",
|
|
86
71
|
"esbuild": "^0.28.2",
|
|
87
|
-
"eslint-import-resolver-typescript": "^4.4.5",
|
|
88
|
-
"eslint-plugin-import-x": "^4.17.1",
|
|
89
72
|
"happy-dom": "21.0.4",
|
|
90
|
-
"
|
|
91
|
-
"
|
|
92
|
-
"semantic-release": "
|
|
93
|
-
"
|
|
94
|
-
"
|
|
95
|
-
|
|
96
|
-
|
|
73
|
+
"oxfmt": "0.81.0",
|
|
74
|
+
"oxlint": "1.96.0",
|
|
75
|
+
"semantic-release": "26.0.1",
|
|
76
|
+
"typescript": "7.0.2",
|
|
77
|
+
"vitest": "5.1.2"
|
|
78
|
+
},
|
|
79
|
+
"release": {
|
|
80
|
+
"branches": [
|
|
81
|
+
"main"
|
|
82
|
+
],
|
|
83
|
+
"plugins": [
|
|
84
|
+
"@semantic-release/commit-analyzer",
|
|
85
|
+
"@semantic-release/release-notes-generator",
|
|
86
|
+
"@semantic-release/npm",
|
|
87
|
+
"@semantic-release/github"
|
|
88
|
+
]
|
|
97
89
|
},
|
|
98
|
-
"
|
|
99
|
-
"
|
|
90
|
+
"packageManager": "pnpm@12.26.0",
|
|
91
|
+
"prepack": "pnpm build"
|
|
100
92
|
}
|
package/dist/client/client.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/dist/client/client.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { validateText, getTags, normalizeWhitespace, serialize, } from "../utils/index.js";
|
|
2
|
-
const defaultConfig = {
|
|
3
|
-
tagsToRemove: [],
|
|
4
|
-
tagsToPreserve: [],
|
|
5
|
-
};
|
|
6
|
-
export function janusClient(text, config = defaultConfig) {
|
|
7
|
-
validateText(text);
|
|
8
|
-
const parser = new DOMParser();
|
|
9
|
-
const { removedTags, preservedTags } = getTags(config);
|
|
10
|
-
const root = parser.parseFromString(text, "text/html").body;
|
|
11
|
-
return normalizeWhitespace(serialize({ node: root, removedTags, preservedTags }));
|
|
12
|
-
}
|
package/dist/client/stream.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../lib/client/stream.ts"],"names":[],"mappings":"AAAA,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"}
|
package/dist/client/stream.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
export async function janusClientStream({ url, config, encoding, fetchOptions, }) {
|
|
2
|
-
const response = await fetch(url, fetchOptions);
|
|
3
|
-
if (!response.ok || !response.body) {
|
|
4
|
-
throw new Error(`Failed downloading stream.\n====\nURL: ${url}\n====\n
|
|
5
|
-
Status:${response.status}`);
|
|
6
|
-
}
|
|
7
|
-
return await _janusClient(response.body, config, encoding);
|
|
8
|
-
}
|
|
9
|
-
async function _janusClient(inputStream, config, encoding = "utf8") {
|
|
10
|
-
const decodedStream = inputStream.pipeThrough(new TextDecoderStream(encoding));
|
|
11
|
-
const reader = decodedStream.getReader();
|
|
12
|
-
let text = "";
|
|
13
|
-
try {
|
|
14
|
-
while (true) {
|
|
15
|
-
const { done, value } = await reader.read();
|
|
16
|
-
if (done)
|
|
17
|
-
break;
|
|
18
|
-
text += value;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
finally {
|
|
22
|
-
reader.releaseLock();
|
|
23
|
-
}
|
|
24
|
-
const { janusClient } = await import("./client.js");
|
|
25
|
-
return janusClient(text, config);
|
|
26
|
-
}
|
package/dist/server/server.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../lib/server/server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAa7C,wBAAsB,WAAW,CAC/B,IAAI,EAAE,MAAM,EACZ,MAAM,GAAE,MAAsB,mBAU/B"}
|
package/dist/server/server.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { validateText, getTags, normalizeWhitespace, serialize, } from "../utils/index.js";
|
|
2
|
-
const defaultConfig = {
|
|
3
|
-
tagsToRemove: [],
|
|
4
|
-
tagsToPreserve: [],
|
|
5
|
-
};
|
|
6
|
-
export async function janusServer(text, config = defaultConfig) {
|
|
7
|
-
validateText(text);
|
|
8
|
-
const { parse } = await import("node-html-parser");
|
|
9
|
-
const root = parse(text);
|
|
10
|
-
const { removedTags, preservedTags } = getTags(config);
|
|
11
|
-
return normalizeWhitespace(serialize({ node: root, removedTags, preservedTags }));
|
|
12
|
-
}
|
package/dist/server/stream.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../lib/server/stream.ts"],"names":[],"mappings":"AAKA,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"}
|
package/dist/server/stream.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { open } from "node:fs/promises";
|
|
2
|
-
import { Readable } from "node:stream";
|
|
3
|
-
import { buffer } from "node:stream/consumers";
|
|
4
|
-
export async function janusServerStream({ encoding = "utf8", filePath = "", fetchOptions = {}, config = {}, url = "", }) {
|
|
5
|
-
if (url.trim().length > 0) {
|
|
6
|
-
const response = await fetch(url, fetchOptions);
|
|
7
|
-
if (!response.ok || !response.body) {
|
|
8
|
-
throw new Error(`Failed downloading stream.\n====\nURL: ${url}\n====\n
|
|
9
|
-
Status:${response.status}`);
|
|
10
|
-
}
|
|
11
|
-
const webStream = Readable.from(response.body);
|
|
12
|
-
return await _janusServer(webStream, config, encoding);
|
|
13
|
-
}
|
|
14
|
-
let handle;
|
|
15
|
-
try {
|
|
16
|
-
handle = await open(filePath, "r"); // Read mode.
|
|
17
|
-
const file = handle.createReadStream();
|
|
18
|
-
return await _janusServer(file, config, encoding);
|
|
19
|
-
}
|
|
20
|
-
finally {
|
|
21
|
-
if (handle) {
|
|
22
|
-
await handle.close();
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
async function _janusServer(inputStream, config, encoding = "utf8") {
|
|
27
|
-
const bytesBuffer = await buffer(inputStream);
|
|
28
|
-
const decoder = new TextDecoder(encoding);
|
|
29
|
-
const text = decoder.decode(bytesBuffer);
|
|
30
|
-
const { janusServer } = await import("./server.js");
|
|
31
|
-
return await janusServer(text, config);
|
|
32
|
-
}
|
package/dist/utils/index.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { HTMLElement as NodeHTMLElement } from "node-html-parser";
|
|
2
|
-
export type Config = {
|
|
3
|
-
tagsToRemove?: string[];
|
|
4
|
-
tagsToPreserve?: string[];
|
|
5
|
-
};
|
|
6
|
-
export declare function validateText(text: string): void;
|
|
7
|
-
export declare function normalizeWhitespace(text?: string): string;
|
|
8
|
-
export declare function getTags(config: Config): {
|
|
9
|
-
removedTags: Set<string>;
|
|
10
|
-
preservedTags: Set<string>;
|
|
11
|
-
};
|
|
12
|
-
export declare function serialize({ node, removedTags, preservedTags, }: {
|
|
13
|
-
node: Node;
|
|
14
|
-
removedTags: Set<string>;
|
|
15
|
-
preservedTags: Set<string>;
|
|
16
|
-
}): string;
|
|
17
|
-
type Node = NodeHTMLElement | ChildNode;
|
|
18
|
-
export {};
|
|
19
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,IAAI,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAEvE,MAAM,MAAM,MAAM,GAAG;IACnB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;CAC3B,CAAC;AAIF,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,QAOxC;AAED,wBAAgB,mBAAmB,CAAC,IAAI,SAAK,UAE5C;AAED,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM;;;EAiBrC;AAED,wBAAgB,SAAS,CAAC,EACxB,IAAI,EACJ,WAAW,EACX,aAAa,GACd,EAAE;IACD,IAAI,EAAE,IAAI,CAAC;IACX,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACzB,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CAC5B,GAAG,MAAM,CAgCT;AAwBD,KAAK,IAAI,GAAG,eAAe,GAAG,SAAS,CAAC"}
|
package/dist/utils/index.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
const defaultBlacklistTags = new Set(["script", "style"]);
|
|
2
|
-
export function validateText(text) {
|
|
3
|
-
if (typeof text !== "string") {
|
|
4
|
-
throw new TypeError(`\n======================================================\n=> ${text} recieved. String expected.\n======================================================`, { cause: `${text} is not of type string.` });
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
export function normalizeWhitespace(text = "") {
|
|
8
|
-
return text.replace(new RegExp(String.raw `\s+`, "g"), " ").trim();
|
|
9
|
-
}
|
|
10
|
-
export function getTags(config) {
|
|
11
|
-
const removedTags = new Set(defaultBlacklistTags);
|
|
12
|
-
const preservedTags = new Set();
|
|
13
|
-
if (config.tagsToRemove) {
|
|
14
|
-
addBlackListTags(removedTags, config.tagsToRemove);
|
|
15
|
-
}
|
|
16
|
-
if (config.tagsToPreserve) {
|
|
17
|
-
removeBlackListTags({
|
|
18
|
-
removedTags,
|
|
19
|
-
preservedTags,
|
|
20
|
-
tagsToPreserve: config.tagsToPreserve,
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
return { removedTags, preservedTags };
|
|
24
|
-
}
|
|
25
|
-
export function serialize({ node, removedTags, preservedTags, }) {
|
|
26
|
-
if (!node)
|
|
27
|
-
return "";
|
|
28
|
-
if (node.nodeType === 3) {
|
|
29
|
-
// DOM Text node => 3
|
|
30
|
-
const rawText = getNodeProperty(node, "rawText");
|
|
31
|
-
const text = getNodeProperty(node, "text");
|
|
32
|
-
return rawText ?? text ?? node.textContent ?? "";
|
|
33
|
-
}
|
|
34
|
-
const tagName = getNodeProperty(node, "tagName") ?? "";
|
|
35
|
-
const tag = tagName.toLowerCase();
|
|
36
|
-
// Completely ignore blacklisted tags
|
|
37
|
-
if (removedTags.has(tag))
|
|
38
|
-
return "";
|
|
39
|
-
// Keep preserved tags intact along with their outer HTML structure
|
|
40
|
-
if (preservedTags.has(tag)) {
|
|
41
|
-
const outerHTML = getNodeProperty(node, "outerHTML");
|
|
42
|
-
return (outerHTML ??
|
|
43
|
-
(typeof node.toString === "function" ? node.toString() : undefined) ??
|
|
44
|
-
"");
|
|
45
|
-
}
|
|
46
|
-
const children = [...node.childNodes];
|
|
47
|
-
return children
|
|
48
|
-
.map((child) => serialize({ node: child, removedTags, preservedTags }))
|
|
49
|
-
.join("");
|
|
50
|
-
}
|
|
51
|
-
function removeBlackListTags({ removedTags, preservedTags, tagsToPreserve, }) {
|
|
52
|
-
for (const tag of tagsToPreserve) {
|
|
53
|
-
const normalized = tag.toLowerCase();
|
|
54
|
-
removedTags.delete(normalized);
|
|
55
|
-
preservedTags.add(normalized);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
function addBlackListTags(preserveTags, tagsToRemove) {
|
|
59
|
-
for (const tag of tagsToRemove) {
|
|
60
|
-
preserveTags.add(tag.toLowerCase());
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
function getNodeProperty(node, lookup = "outerHTML") {
|
|
64
|
-
const targetNode = node;
|
|
65
|
-
const value = targetNode[lookup];
|
|
66
|
-
return value ?? undefined;
|
|
67
|
-
}
|