janus-parse 2.1.1 → 2.3.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 CHANGED
@@ -12,7 +12,7 @@ A lightweight, high-performance, isomorphic HTML text extractor and sanitizer bu
12
12
 
13
13
  Here is exactly what it handles:
14
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.
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
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
18
 
@@ -131,11 +131,11 @@ type JanusServerStreamConfig = {
131
131
 
132
132
  ## 📦 Bundle Size
133
133
 
134
- This package is split into targeted modules. Modern bundlers will only bundle the exact module you import, keeping your builds incredibly lightweight.
134
+ 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
135
 
136
136
  | Import Path | Target Environment | Size (Gzipped) |
137
137
  | :-------------------------- | :----------------- | :------------- |
138
- | `janus-parse/server` | Node.js | **~47.6 KB** |
139
- | `janus-parse/server:stream` | Node.js | **~49 KB** |
138
+ | `janus-parse/server` | Node.js | **~1.10 KB** |
139
+ | `janus-parse/server:stream` | Node.js | **~1.51 KB** |
140
140
  | `janus-parse/client` | Browser | **~700 B** |
141
141
  | `janus-parse/client:stream` | Browser | **~1 KB** |
@@ -1 +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"}
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,UAcvE"}
@@ -6,7 +6,12 @@ const defaultConfig = {
6
6
  export function janusClient(text, config = defaultConfig) {
7
7
  validateText(text);
8
8
  const parser = new DOMParser();
9
- const { removedTags, preservedTags } = getTags(config);
9
+ const { removedTags, preservedTags, preserveTagsWithAttrs } = getTags(config);
10
10
  const root = parser.parseFromString(text, "text/html").body;
11
- return normalizeWhitespace(serialize({ node: root, removedTags, preservedTags }));
11
+ return normalizeWhitespace(serialize({
12
+ node: root,
13
+ removedTags,
14
+ preservedTags,
15
+ preserveTagsWithAttrs,
16
+ }));
12
17
  }
@@ -1 +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"}
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"}
@@ -1,4 +1,3 @@
1
- import { janusClient } from "./client.js";
2
1
  export async function janusClientStream({ url, config, encoding, fetchOptions, }) {
3
2
  const response = await fetch(url, fetchOptions);
4
3
  if (!response.ok || !response.body) {
@@ -22,5 +21,6 @@ async function _janusClient(inputStream, config, encoding = "utf8") {
22
21
  finally {
23
22
  reader.releaseLock();
24
23
  }
24
+ const { janusClient } = await import("./client.js");
25
25
  return janusClient(text, config);
26
26
  }
@@ -1 +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
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../lib/server/server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAc7C,wBAAsB,WAAW,CAC/B,IAAI,EAAE,MAAM,EACZ,MAAM,GAAE,MAAsB,mBAc/B"}
@@ -1,12 +1,18 @@
1
- import { parse } from "node-html-parser";
2
1
  import { validateText, getTags, normalizeWhitespace, serialize, } from "../utils/index.js";
3
2
  const defaultConfig = {
4
3
  tagsToRemove: [],
5
4
  tagsToPreserve: [],
5
+ preserveTagsWithAttrs: [],
6
6
  };
7
7
  export async function janusServer(text, config = defaultConfig) {
8
8
  validateText(text);
9
+ const { parse } = await import("node-html-parser");
9
10
  const root = parse(text);
10
- const { removedTags, preservedTags } = getTags(config);
11
- return normalizeWhitespace(serialize({ node: root, removedTags, preservedTags }));
11
+ const { removedTags, preservedTags, preserveTagsWithAttrs } = getTags(config);
12
+ return normalizeWhitespace(serialize({
13
+ node: root,
14
+ removedTags,
15
+ preservedTags,
16
+ preserveTagsWithAttrs,
17
+ }));
12
18
  }
@@ -2,18 +2,22 @@ import type { HTMLElement as NodeHTMLElement } from "node-html-parser";
2
2
  export type Config = {
3
3
  tagsToRemove?: string[];
4
4
  tagsToPreserve?: string[];
5
+ preserveTagsWithAttrs?: string[];
5
6
  };
6
7
  export declare function validateText(text: string): void;
7
8
  export declare function normalizeWhitespace(text?: string): string;
8
9
  export declare function getTags(config: Config): {
9
10
  removedTags: Set<string>;
10
11
  preservedTags: Set<string>;
12
+ preserveTagsWithAttrs: Set<string>;
11
13
  };
12
- export declare function serialize({ node, removedTags, preservedTags, }: {
13
- node: Node;
14
+ type SerializableNode = NodeHTMLElement | HTMLElement;
15
+ type SerializeParameters = {
16
+ node: SerializableNode;
14
17
  removedTags: Set<string>;
15
18
  preservedTags: Set<string>;
16
- }): string;
17
- type Node = NodeHTMLElement | ChildNode;
19
+ preserveTagsWithAttrs?: Set<string>;
20
+ };
21
+ export declare function serialize({ node, removedTags, preservedTags, preserveTagsWithAttrs, }: SerializeParameters): string;
18
22
  export {};
19
23
  //# 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,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;CAC3B,CAAC;AAIF,wBAAgB,YAAY,CAAC,IAAI,EA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,MAAM,CAiD9B"}
@@ -10,6 +10,7 @@ export function normalizeWhitespace(text = "") {
10
10
  export function getTags(config) {
11
11
  const removedTags = new Set(defaultBlacklistTags);
12
12
  const preservedTags = new Set();
13
+ const preserveTagsWithAttributes = new Set(config.preserveTagsWithAttrs);
13
14
  if (config.tagsToRemove) {
14
15
  addBlackListTags(removedTags, config.tagsToRemove);
15
16
  }
@@ -20,9 +21,13 @@ export function getTags(config) {
20
21
  tagsToPreserve: config.tagsToPreserve,
21
22
  });
22
23
  }
23
- return { removedTags, preservedTags };
24
+ return {
25
+ removedTags,
26
+ preservedTags,
27
+ preserveTagsWithAttrs: preserveTagsWithAttributes,
28
+ };
24
29
  }
25
- export function serialize({ node, removedTags, preservedTags, }) {
30
+ export function serialize({ node, removedTags, preservedTags, preserveTagsWithAttrs, }) {
26
31
  if (!node)
27
32
  return "";
28
33
  if (node.nodeType === 3) {
@@ -36,17 +41,46 @@ export function serialize({ node, removedTags, preservedTags, }) {
36
41
  // Completely ignore blacklisted tags
37
42
  if (removedTags.has(tag))
38
43
  return "";
39
- // Keep preserved tags intact along with their outer HTML structure
44
+ const children = [...node.childNodes];
45
+ function serializeChildren() {
46
+ return children
47
+ .map((child) => serialize({
48
+ node: child,
49
+ removedTags,
50
+ preservedTags,
51
+ preserveTagsWithAttrs,
52
+ }))
53
+ .join("");
54
+ }
40
55
  if (preservedTags.has(tag)) {
56
+ if (preserveTagsWithAttrs &&
57
+ preserveTagsWithAttrs.size > 0 &&
58
+ !hasPreservedAttribute(node, preserveTagsWithAttrs)) {
59
+ return serializeChildren();
60
+ }
41
61
  const outerHTML = getNodeProperty(node, "outerHTML");
42
62
  return (outerHTML ??
43
- (typeof node.toString === "function" ? node.toString() : undefined) ??
63
+ (typeof String(node) === "function" ? String(node) : undefined) ??
44
64
  "");
45
65
  }
46
- const children = [...node.childNodes];
47
- return children
48
- .map((child) => serialize({ node: child, removedTags, preservedTags }))
49
- .join("");
66
+ return serializeChildren();
67
+ }
68
+ function sanitizeAttribute(attribute) {
69
+ return attribute.replace(/\s+/g, " ").replace(/'/g, '"').trim();
70
+ }
71
+ function hasPreservedAttribute(node, preserveTagsWithAttributes) {
72
+ const nodeType = "nodeType" in node ? node.nodeType : undefined;
73
+ const rawAttributes = "rawAttrs" in node
74
+ ? (node.rawAttrs ?? "")
75
+ : (nodeType === 1
76
+ ? [...node.attributes]
77
+ .map(({ name, value }) => `${name}="${value}"`)
78
+ .join(" ")
79
+ : "");
80
+ const normalizedAttributes = sanitizeAttribute(rawAttributes);
81
+ return (rawAttributes &&
82
+ [...preserveTagsWithAttributes].some((attribute) => Boolean(attribute.trim()) &&
83
+ normalizedAttributes.includes(sanitizeAttribute(attribute))));
50
84
  }
51
85
  function removeBlackListTags({ removedTags, preservedTags, tagsToPreserve, }) {
52
86
  for (const tag of tagsToPreserve) {
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "janus-parse",
3
3
  "license": "MIT",
4
- "version": "2.1.1",
5
- "packageManager": "pnpm@11.20.0",
4
+ "version": "2.3.0",
5
+ "packageManager": "pnpm@12.18.0",
6
6
  "keywords": [
7
7
  "html-parser",
8
8
  "text-extractor",
@@ -73,27 +73,27 @@
73
73
  "bundle:size": "tsx scripts/bundle-size.ts"
74
74
  },
75
75
  "dependencies": {
76
- "node-html-parser": "9.0.1"
76
+ "node-html-parser": "9.0.7"
77
77
  },
78
78
  "devDependencies": {
79
79
  "@eslint/js": "^10.0.1",
80
80
  "@semantic-release/commit-analyzer": "^13.0.1",
81
- "@semantic-release/github": "^12.0.9",
82
- "@semantic-release/npm": "^13.1.5",
81
+ "@semantic-release/github": "12.0.11",
82
+ "@semantic-release/npm": "13.2.0",
83
83
  "@semantic-release/release-notes-generator": "^14.1.1",
84
- "@types/node": "^26.1.1",
85
- "esbuild": "^0.28.1",
86
- "eslint": "^10.7.0",
84
+ "@types/node": "26.6.6",
85
+ "eslint": "10.15.0",
86
+ "esbuild": "^0.28.2",
87
87
  "eslint-import-resolver-typescript": "^4.4.5",
88
88
  "eslint-plugin-import-x": "^4.17.1",
89
- "eslint-plugin-unicorn": "^72.0.0",
90
- "happy-dom": "^20.11.1",
91
- "prettier": "^3.9.6",
89
+ "happy-dom": "21.0.4",
90
+ "eslint-plugin-unicorn": "79.0.0",
91
+ "prettier": "3.9.10",
92
92
  "semantic-release": "^25.0.8",
93
- "tsx": "4.23.1",
93
+ "tsx": "4.23.15",
94
94
  "typescript": "6.0.3",
95
- "typescript-eslint": "^8.65.0",
96
- "vitest": "^4.1.10"
95
+ "typescript-eslint": "8.72.1",
96
+ "vitest": "5.1.0"
97
97
  },
98
98
  "prettier": {},
99
99
  "--Typescript V 7--": "cannot use TS v7 until typescript-eslint adds support"