janus-parse 1.0.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.
@@ -0,0 +1,2 @@
1
+ export * from "./janus-parse.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./janus-parse.js";
@@ -0,0 +1,4 @@
1
+ import { Config } from "./utils/index.js";
2
+ export declare function janusServer(text: string, config?: Config): Promise<string>;
3
+ export declare function janusClient(text: string, config?: Config): string | null;
4
+ //# sourceMappingURL=janus-parse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"janus-parse.d.ts","sourceRoot":"","sources":["../lib/janus-parse.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,MAAM,EAAW,MAAM,kBAAkB,CAAC;AAO9E,wBAAsB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,GAAE,MAAoB,mBAU3E;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,GAAE,MAAoB,iBAYrE"}
@@ -0,0 +1,25 @@
1
+ import { validateText, removeNodes, getTags } from "./utils/index.js";
2
+ const janusConfig = {
3
+ addBlacklistTags: [],
4
+ removeBlacklistTags: [],
5
+ };
6
+ export async function janusServer(text, config = janusConfig) {
7
+ validateText(text);
8
+ const tags = getTags(config);
9
+ const parser = await import("node-html-parser");
10
+ const root = parser.parse(text);
11
+ const nodesToRemove = root.querySelectorAll(tags);
12
+ // @ts-expect-error
13
+ removeNodes(nodesToRemove);
14
+ return root.textContent.replace(new RegExp("\\s+", "g"), " ").trim();
15
+ }
16
+ export function janusClient(text, config = janusConfig) {
17
+ validateText(text);
18
+ const tags = getTags(config);
19
+ const parser = new DOMParser();
20
+ const virtualDoc = parser.parseFromString(text, "text/html");
21
+ const nodesToRemove = virtualDoc.querySelectorAll(tags);
22
+ removeNodes(nodesToRemove);
23
+ const cleanText = virtualDoc.body.textContent;
24
+ return cleanText;
25
+ }
@@ -0,0 +1,8 @@
1
+ export type Config = {
2
+ addBlacklistTags?: string[];
3
+ removeBlacklistTags?: string[];
4
+ };
5
+ export declare function validateText(text: string): void;
6
+ export declare function getTags(config: Config): string;
7
+ export declare function removeNodes(nodesToRemove: HTMLElement[] | NodeListOf<Element>): NodeListOf<Element> | HTMLElement[];
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/utils/index.ts"],"names":[],"mappings":"AAAA,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,OAAO,CAAC,MAAM,EAAE,MAAM,UAWrC;AAED,wBAAgB,WAAW,CACzB,aAAa,EAAE,WAAW,EAAE,GAAG,UAAU,CAAC,OAAO,CAAC,uCAOnD"}
@@ -0,0 +1,37 @@
1
+ const defaultBlacklistTags = new Set(["script", "style"]);
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 });
6
+ }
7
+ }
8
+ export function getTags(config) {
9
+ let tags = new Set();
10
+ if (config.addBlacklistTags) {
11
+ tags = addBlackListTags(config.addBlacklistTags);
12
+ }
13
+ if (config.removeBlacklistTags) {
14
+ tags = removeBlackListTags(config.removeBlacklistTags);
15
+ }
16
+ return [...tags].join(",");
17
+ }
18
+ export function removeNodes(nodesToRemove) {
19
+ for (const node of nodesToRemove) {
20
+ node.remove();
21
+ }
22
+ return nodesToRemove;
23
+ }
24
+ function removeBlackListTags(removeBlacklistTags) {
25
+ const blacklistTags = new Set(defaultBlacklistTags);
26
+ for (const tag of removeBlacklistTags) {
27
+ blacklistTags.delete(tag);
28
+ }
29
+ return blacklistTags;
30
+ }
31
+ function addBlackListTags(addBlacklistTags) {
32
+ const blacklistTags = new Set(defaultBlacklistTags);
33
+ for (const tag of addBlacklistTags) {
34
+ blacklistTags.add(tag);
35
+ }
36
+ return blacklistTags;
37
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "janus-parse",
3
+ "license": "MIT",
4
+ "version": "1.0.0",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "type": "module",
11
+ "prepack": "pnpm build",
12
+ "dependencies": {
13
+ "node-html-parser": "9.0.0"
14
+ },
15
+ "devDependencies": {
16
+ "@types/node": "^26.1.1",
17
+ "eslint": "^10.7.0",
18
+ "happy-dom": "^20.11.0",
19
+ "prettier": "^3.9.5",
20
+ "tsx": "4.23.1",
21
+ "typescript": "5.7.2",
22
+ "vitest": "^4.1.10"
23
+ },
24
+ "prettier": {},
25
+ "scripts": {
26
+ "janus": "tsx ./lib/index.ts",
27
+ "test": "vitest",
28
+ "fmt": "prettier --write .",
29
+ "build": "tsc"
30
+ }
31
+ }