janus-parse 1.1.1 → 1.2.1

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/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -49,9 +49,7 @@ const rawHtml = `
49
49
  </main>
50
50
  `;
51
51
 
52
- const cleanText = await janusServer(rawHtml);
53
- console.log(cleanText);
54
- // Output: "Hello Universe This is a highly spaced sentence."
52
+ const cleanText = await janusServer(rawHtml); // Output: "Hello Universe This is a highly spaced sentence."
55
53
  ```
56
54
 
57
55
  ### 2. Client-Side Execution (Browser)
@@ -64,9 +62,7 @@ import { janusClient } from "janus-parse";
64
62
  const webMarkup =
65
63
  "<div> Dynamic Web App <style>body { display: none; }</style></div>";
66
64
 
67
- const textOnly = janusClient(webMarkup);
68
- console.log(textOnly);
69
- // Output: " Dynamic Web App"
65
+ const textOnly = janusClient(webMarkup); // Output: " Dynamic Web App"
70
66
  ```
71
67
 
72
68
  ### 3. Custom Configurations
@@ -75,8 +71,8 @@ You can modify which HTML tags are targeted for destruction by passing an option
75
71
 
76
72
  ```typescript
77
73
  const structuralConfig = {
78
- addBlacklistTags: ["span", "section"], // Purge extra elements
79
- removeBlacklistTags: ["script"], // Keep scripts if you are building an isolated sandbox
74
+ tagsToRemove: ["span", "section"] // Adds additonal blacklist candidates,
75
+ tagsToPreserve: ["script"] // Preserves script tag in output,
80
76
  };
81
77
  ```
82
78
 
@@ -87,9 +83,9 @@ const processedTextServer = await janusServer(htmlSource, structuralConfig);
87
83
  ```
88
84
 
89
85
  ```typescript
90
- import { janusClient } from 'janus-parse';
86
+ import { janusClient } from "janus-parse";
91
87
 
92
- const processedTextClient = janusClienthtmlSource, structuralConfig);
88
+ const processedTextClient = janusClient(htmlSource, structuralConfig);
93
89
  ```
94
90
 
95
91
  ---
@@ -101,26 +97,22 @@ const processedTextClient = janusClienthtmlSource, structuralConfig);
101
97
  TypeScript interface passed to fine-tune tag removal behaviors.
102
98
 
103
99
  ```typescript
104
- interface Config {
105
- addBlacklistTags?: string[];
106
- removeBlacklistTags?: string[];
100
+ type Config {
101
+ tagsToRemove?: string[];
102
+ tagsToPreserve?: string[];
107
103
  }
108
104
  ```
109
105
 
110
- ### `janusServer(text: string, config?: Config): Promise<string>`
106
+ #### `janusServer(text: string, config?: Config): Promise<string>`
111
107
 
112
108
  - **`text`**: The input raw HTML string.
113
109
  - **`config`**: Optional rule blocks to override standard cleaning lists.
114
110
  - **Returns**: A promise that resolves to a stripped, whitespace-normalized single-line string.
115
111
 
116
- ### `janusClient(text: string, config?: Config): string`
112
+ ---
113
+
114
+ #### `janusClient(text: string, config?: Config): string`
117
115
 
118
116
  - **`text`**: The input raw HTML string.
119
117
  - **`config`**: Optional rule blocks to override standard cleaning lists.
120
118
  - **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.
@@ -1 +1 @@
1
- {"version":3,"file":"janus-parse.d.ts","sourceRoot":"","sources":["../lib/janus-parse.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAOtC,wBAAsB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,GAAE,MAAoB,mBAS3E;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,GAAE,MAAoB,UAUrE"}
1
+ {"version":3,"file":"janus-parse.d.ts","sourceRoot":"","sources":["../lib/janus-parse.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAOtC,wBAAsB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,GAAE,MAAoB,mBAS3E;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,GAAE,MAAoB,UASrE"}
@@ -1,23 +1,19 @@
1
- import { validateText, removeNodes, getTags, normalizeWhitespace, } from "./utils";
1
+ import { validateText, getTags, normalizeWhitespace, serialize } from "./utils";
2
2
  const janusConfig = {
3
- addBlacklistTags: [],
4
- removeBlacklistTags: [],
3
+ tagsToRemove: [],
4
+ tagsToPreserve: [],
5
5
  };
6
6
  export async function janusServer(text, config = janusConfig) {
7
7
  validateText(text);
8
- const tags = getTags(config);
9
8
  const parser = await import("node-html-parser");
10
9
  const root = parser.parse(text);
11
- const nodesToRemove = root.querySelectorAll(tags);
12
- removeNodes(nodesToRemove);
13
- return normalizeWhitespace(root.textContent);
10
+ const { removedTags, preservedTags } = getTags(config);
11
+ return normalizeWhitespace(serialize({ node: root, removedTags, preservedTags }));
14
12
  }
15
13
  export function janusClient(text, config = janusConfig) {
16
14
  validateText(text);
17
- const tags = getTags(config);
18
15
  const parser = new DOMParser();
19
- const virtualDocument = parser.parseFromString(text, "text/html");
20
- const nodesToRemove = virtualDocument.querySelectorAll(tags);
21
- removeNodes(nodesToRemove);
22
- return normalizeWhitespace(virtualDocument.body.textContent);
16
+ const { removedTags, preservedTags } = getTags(config);
17
+ const root = parser.parseFromString(text, "text/html").body;
18
+ return normalizeWhitespace(serialize({ node: root, removedTags, preservedTags }));
23
19
  }
@@ -1,10 +1,19 @@
1
1
  import type { HTMLElement as NodeHTMLElement } from "node-html-parser";
2
2
  export type Config = {
3
- addBlacklistTags?: string[];
4
- removeBlacklistTags?: string[];
3
+ tagsToRemove?: string[];
4
+ tagsToPreserve?: string[];
5
5
  };
6
6
  export declare function validateText(text: string): void;
7
7
  export declare function normalizeWhitespace(text?: string): string;
8
- export declare function getTags(config: Config): string;
9
- export declare function removeNodes(nodesToRemove: NodeHTMLElement[] | NodeListOf<Element>): NodeListOf<Element> | NodeHTMLElement[];
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 {};
10
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
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,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC5B,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAC;CAChC,CAAC;AAIF,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,QAUxC;AAED,wBAAgB,mBAAmB,CAAC,IAAI,SAAK,UAE5C;AAED,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,UAWrC;AAED,wBAAgB,WAAW,CACzB,aAAa,EAAE,eAAe,EAAE,GAAG,UAAU,CAAC,OAAO,CAAC,2CAOvD"}
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"}
@@ -1,40 +1,67 @@
1
1
  const defaultBlacklistTags = new Set(["script", "style"]);
2
2
  export function validateText(text) {
3
- if (typeof text !== "string" ||
4
- (typeof text === "string" && text === "undefined")) {
5
- throw new Error("The text is not a valid string. Please provide a valid string to parse.", { cause: text });
3
+ if (typeof text !== "string") {
4
+ throw new TypeError(`\n======================================================\n=> ${text} recieved. String expected.\n======================================================`, { cause: `${text} is not of type string.` });
6
5
  }
7
6
  }
8
7
  export function normalizeWhitespace(text = "") {
9
8
  return text.replace(new RegExp(String.raw `\s+`, "g"), " ").trim();
10
9
  }
11
10
  export function getTags(config) {
12
- let tags = new Set();
13
- if (config.addBlacklistTags) {
14
- tags = addBlackListTags(config.addBlacklistTags);
11
+ const removedTags = new Set(defaultBlacklistTags);
12
+ const preservedTags = new Set();
13
+ if (config.tagsToRemove) {
14
+ addBlackListTags(removedTags, config.tagsToRemove);
15
15
  }
16
- if (config.removeBlacklistTags) {
17
- tags = removeBlackListTags(config.removeBlacklistTags);
16
+ if (config.tagsToPreserve) {
17
+ removeBlackListTags({
18
+ removedTags,
19
+ preservedTags,
20
+ tagsToPreserve: config.tagsToPreserve,
21
+ });
18
22
  }
19
- return [...tags].join(",");
23
+ return { removedTags, preservedTags };
20
24
  }
21
- export function removeNodes(nodesToRemove) {
22
- for (const node of nodesToRemove) {
23
- node.remove();
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 ?? "";
24
33
  }
25
- return nodesToRemove;
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("");
26
50
  }
27
- function removeBlackListTags(removeBlacklistTags) {
28
- const blacklistTags = new Set(defaultBlacklistTags);
29
- for (const tag of removeBlacklistTags) {
30
- blacklistTags.delete(tag);
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);
31
56
  }
32
- return blacklistTags;
33
57
  }
34
- function addBlackListTags(addBlacklistTags) {
35
- const blacklistTags = new Set(defaultBlacklistTags);
36
- for (const tag of addBlacklistTags) {
37
- blacklistTags.add(tag);
58
+ function addBlackListTags(preserveTags, tagsToRemove) {
59
+ for (const tag of tagsToRemove) {
60
+ preserveTags.add(tag.toLowerCase());
38
61
  }
39
- return blacklistTags;
62
+ }
63
+ function getNodeProperty(node, lookup = "outerHTML") {
64
+ const targetNode = node;
65
+ const value = targetNode[lookup];
66
+ return value ?? undefined;
40
67
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "janus-parse",
3
3
  "license": "MIT",
4
- "version": "1.1.1",
4
+ "version": "1.2.1",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "packageManager": "pnpm@11.15.1",
@@ -67,8 +67,8 @@
67
67
  "eslint-import-resolver-typescript": "^4.4.5",
68
68
  "eslint-plugin-import-x": "^4.17.1",
69
69
  "eslint-plugin-unicorn": "^72.0.0",
70
- "happy-dom": "^20.11.0",
71
- "prettier": "^3.9.5",
70
+ "happy-dom": "^20.11.1",
71
+ "prettier": "^3.9.6",
72
72
  "semantic-release": "^25.0.8",
73
73
  "tsx": "4.23.1",
74
74
  "typescript": "6.0.3",