@learnpress/utils 0.1.0 → 0.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/README.md CHANGED
@@ -8,8 +8,28 @@ Shared utility helpers for LearnPress packages.
8
8
  npm install @learnpress/utils
9
9
  ```
10
10
 
11
- ## Usage
11
+ ## API
12
+
13
+ ### `stripHtml(html: string): string`
14
+
15
+ Strip all HTML tags and decode HTML entities, returning trimmed plain text. Useful for titles, excerpts, or any place you need a clean text version of rendered HTML.
12
16
 
13
17
  ```ts
14
- import { isString, isObject } from "@learnpress/utils";
18
+ import { stripHtml } from "@learnpress/utils";
19
+
20
+ stripHtml("<p>Hello &amp; <strong>world</strong></p>");
21
+ // "Hello & world"
22
+ ```
23
+
24
+ ### `sanitizeHtml(html: string): string`
25
+
26
+ Sanitize an HTML string for safe rendering. Allows a curated set of tags and attributes commonly used in WordPress post content (`img`, `figure`, `iframe`, `video`, `audio`, `source`, `track`, …), plus safe iframe embeds from YouTube and Vimeo. External links with `target="_blank"` automatically get `rel="noopener noreferrer"`.
27
+
28
+ ```tsx
29
+ import parse from "html-react-parser";
30
+ import { sanitizeHtml } from "@learnpress/utils";
31
+
32
+ export function HtmlContent({ html }: { html: string }) {
33
+ return <div>{parse(sanitizeHtml(html))}</div>;
34
+ }
15
35
  ```
package/dist/index.cjs CHANGED
@@ -127,15 +127,5 @@ const sanitizeHtml = (html) => (0, sanitize_html.default)(html, {
127
127
  }) }
128
128
  });
129
129
  //#endregion
130
- //#region src/is.ts
131
- const isString = (value) => typeof value === "string";
132
- const isNumber = (value) => typeof value === "number" && !Number.isNaN(value);
133
- const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
134
- const isNil = (value) => value == null;
135
- //#endregion
136
- exports.isNil = isNil;
137
- exports.isNumber = isNumber;
138
- exports.isObject = isObject;
139
- exports.isString = isString;
140
130
  exports.sanitizeHtml = sanitizeHtml;
141
131
  exports.stripHtml = stripHtml;
package/dist/index.d.cts CHANGED
@@ -2,11 +2,5 @@
2
2
  declare const stripHtml: (html: string) => string;
3
3
  declare const sanitizeHtml: (html: string) => string;
4
4
  //#endregion
5
- //#region src/is.d.ts
6
- declare const isString: (value: unknown) => value is string;
7
- declare const isNumber: (value: unknown) => value is number;
8
- declare const isObject: (value: unknown) => value is Record<string, unknown>;
9
- declare const isNil: (value: unknown) => value is null | undefined;
10
- //#endregion
11
- export { isNil, isNumber, isObject, isString, sanitizeHtml, stripHtml };
5
+ export { sanitizeHtml, stripHtml };
12
6
  //# sourceMappingURL=index.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","names":[],"sources":["../src/html.ts","../src/is.ts"],"mappings":";cAGa,SAAA,GAAa,IAAA;AAAA,cAEb,YAAA,GAAgB,IAAA;;;cCLhB,QAAA,GAAY,KAAA,cAAiB,KAAA;AAAA,cAE7B,QAAA,GAAY,KAAA,cAAiB,KAAA;AAAA,cAG7B,QAAA,GAAY,KAAA,cAAiB,KAAA,IAAS,MAAA;AAAA,cAGtC,KAAA,GAAS,KAAA,cAAiB,KAAA"}
1
+ {"version":3,"file":"index.d.cts","names":[],"sources":["../src/html.ts"],"mappings":";cAGa,SAAA,GAAa,IAAA;AAAA,cAEb,YAAA,GAAgB,IAAA"}
package/dist/index.d.ts CHANGED
@@ -2,11 +2,5 @@
2
2
  declare const stripHtml: (html: string) => string;
3
3
  declare const sanitizeHtml: (html: string) => string;
4
4
  //#endregion
5
- //#region src/is.d.ts
6
- declare const isString: (value: unknown) => value is string;
7
- declare const isNumber: (value: unknown) => value is number;
8
- declare const isObject: (value: unknown) => value is Record<string, unknown>;
9
- declare const isNil: (value: unknown) => value is null | undefined;
10
- //#endregion
11
- export { isNil, isNumber, isObject, isString, sanitizeHtml, stripHtml };
5
+ export { sanitizeHtml, stripHtml };
12
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/html.ts","../src/is.ts"],"mappings":";cAGa,SAAA,GAAa,IAAA;AAAA,cAEb,YAAA,GAAgB,IAAA;;;cCLhB,QAAA,GAAY,KAAA,cAAiB,KAAA;AAAA,cAE7B,QAAA,GAAY,KAAA,cAAiB,KAAA;AAAA,cAG7B,QAAA,GAAY,KAAA,cAAiB,KAAA,IAAS,MAAA;AAAA,cAGtC,KAAA,GAAS,KAAA,cAAiB,KAAA"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/html.ts"],"mappings":";cAGa,SAAA,GAAa,IAAA;AAAA,cAEb,YAAA,GAAgB,IAAA"}
package/dist/index.js CHANGED
@@ -103,12 +103,6 @@ const sanitizeHtml = (html) => sanitizeHtmlLib(html, {
103
103
  }) }
104
104
  });
105
105
  //#endregion
106
- //#region src/is.ts
107
- const isString = (value) => typeof value === "string";
108
- const isNumber = (value) => typeof value === "number" && !Number.isNaN(value);
109
- const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
110
- const isNil = (value) => value == null;
111
- //#endregion
112
- export { isNil, isNumber, isObject, isString, sanitizeHtml, stripHtml };
106
+ export { sanitizeHtml, stripHtml };
113
107
 
114
108
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/html.ts","../src/is.ts"],"sourcesContent":["import { decodeHTML } from \"entities\";\nimport sanitizeHtmlLib from \"sanitize-html\";\n\nexport const stripHtml = (html: string): string => decodeHTML(html.replace(/<[^>]*>/g, \"\")).trim();\n\nexport const sanitizeHtml = (html: string): string =>\n sanitizeHtmlLib(html, {\n allowedTags: sanitizeHtmlLib.defaults.allowedTags.concat([\n \"img\",\n \"figure\",\n \"figcaption\",\n \"iframe\",\n \"video\",\n \"audio\",\n \"source\",\n \"track\",\n ]),\n allowedAttributes: {\n ...sanitizeHtmlLib.defaults.allowedAttributes,\n \"*\": [\"class\", \"id\", \"style\"],\n img: [\"src\", \"srcset\", \"sizes\", \"alt\", \"width\", \"height\", \"loading\", \"decoding\"],\n a: [\"href\", \"name\", \"target\", \"rel\", \"title\"],\n iframe: [\"src\", \"width\", \"height\", \"frameborder\", \"allow\", \"allowfullscreen\"],\n video: [\n \"src\",\n \"controls\",\n \"poster\",\n \"width\",\n \"height\",\n \"preload\",\n \"playsinline\",\n \"muted\",\n \"loop\",\n \"autoplay\",\n ],\n audio: [\"src\", \"controls\", \"preload\", \"loop\", \"autoplay\"],\n source: [\"src\", \"type\", \"media\"],\n track: [\"src\", \"kind\", \"srclang\", \"label\", \"default\"],\n },\n allowedSchemes: [\"http\", \"https\", \"mailto\", \"tel\", \"data\"],\n allowedSchemesByTag: { img: [\"http\", \"https\", \"data\"] },\n allowedIframeHostnames: [\"www.youtube.com\", \"youtube.com\", \"player.vimeo.com\"],\n transformTags: {\n a: (tagName, attribs) => ({\n tagName,\n attribs: {\n ...attribs,\n ...(attribs.target === \"_blank\" ? { rel: \"noopener noreferrer\" } : {}),\n },\n }),\n },\n });\n","export const isString = (value: unknown): value is string => typeof value === \"string\";\n\nexport const isNumber = (value: unknown): value is number =>\n typeof value === \"number\" && !Number.isNaN(value);\n\nexport const isObject = (value: unknown): value is Record<string, unknown> =>\n typeof value === \"object\" && value !== null && !Array.isArray(value);\n\nexport const isNil = (value: unknown): value is null | undefined => value == null;\n"],"mappings":";;;AAGA,MAAa,aAAa,SAAyB,WAAW,KAAK,QAAQ,YAAY,GAAG,CAAC,CAAC,MAAM;AAElG,MAAa,gBAAgB,SAC3B,gBAAgB,MAAM;CACpB,aAAa,gBAAgB,SAAS,YAAY,OAAO;EACvD;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;CACF,mBAAmB;EACjB,GAAG,gBAAgB,SAAS;EAC5B,KAAK;GAAC;GAAS;GAAM;GAAQ;EAC7B,KAAK;GAAC;GAAO;GAAU;GAAS;GAAO;GAAS;GAAU;GAAW;GAAW;EAChF,GAAG;GAAC;GAAQ;GAAQ;GAAU;GAAO;GAAQ;EAC7C,QAAQ;GAAC;GAAO;GAAS;GAAU;GAAe;GAAS;GAAkB;EAC7E,OAAO;GACL;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACD;EACD,OAAO;GAAC;GAAO;GAAY;GAAW;GAAQ;GAAW;EACzD,QAAQ;GAAC;GAAO;GAAQ;GAAQ;EAChC,OAAO;GAAC;GAAO;GAAQ;GAAW;GAAS;GAAU;EACtD;CACD,gBAAgB;EAAC;EAAQ;EAAS;EAAU;EAAO;EAAO;CAC1D,qBAAqB,EAAE,KAAK;EAAC;EAAQ;EAAS;EAAO,EAAE;CACvD,wBAAwB;EAAC;EAAmB;EAAe;EAAmB;CAC9E,eAAe,EACb,IAAI,SAAS,aAAa;EACxB;EACA,SAAS;GACP,GAAG;GACH,GAAI,QAAQ,WAAW,WAAW,EAAE,KAAK,uBAAuB,GAAG,EAAE;GACtE;EACF,GACF;CACF,CAAC;;;ACnDJ,MAAa,YAAY,UAAoC,OAAO,UAAU;AAE9E,MAAa,YAAY,UACvB,OAAO,UAAU,YAAY,CAAC,OAAO,MAAM,MAAM;AAEnD,MAAa,YAAY,UACvB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,MAAM;AAEtE,MAAa,SAAS,UAA8C,SAAS"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/html.ts"],"sourcesContent":["import { decodeHTML } from \"entities\";\nimport sanitizeHtmlLib from \"sanitize-html\";\n\nexport const stripHtml = (html: string): string => decodeHTML(html.replace(/<[^>]*>/g, \"\")).trim();\n\nexport const sanitizeHtml = (html: string): string =>\n sanitizeHtmlLib(html, {\n allowedTags: sanitizeHtmlLib.defaults.allowedTags.concat([\n \"img\",\n \"figure\",\n \"figcaption\",\n \"iframe\",\n \"video\",\n \"audio\",\n \"source\",\n \"track\",\n ]),\n allowedAttributes: {\n ...sanitizeHtmlLib.defaults.allowedAttributes,\n \"*\": [\"class\", \"id\", \"style\"],\n img: [\"src\", \"srcset\", \"sizes\", \"alt\", \"width\", \"height\", \"loading\", \"decoding\"],\n a: [\"href\", \"name\", \"target\", \"rel\", \"title\"],\n iframe: [\"src\", \"width\", \"height\", \"frameborder\", \"allow\", \"allowfullscreen\"],\n video: [\n \"src\",\n \"controls\",\n \"poster\",\n \"width\",\n \"height\",\n \"preload\",\n \"playsinline\",\n \"muted\",\n \"loop\",\n \"autoplay\",\n ],\n audio: [\"src\", \"controls\", \"preload\", \"loop\", \"autoplay\"],\n source: [\"src\", \"type\", \"media\"],\n track: [\"src\", \"kind\", \"srclang\", \"label\", \"default\"],\n },\n allowedSchemes: [\"http\", \"https\", \"mailto\", \"tel\", \"data\"],\n allowedSchemesByTag: { img: [\"http\", \"https\", \"data\"] },\n allowedIframeHostnames: [\"www.youtube.com\", \"youtube.com\", \"player.vimeo.com\"],\n transformTags: {\n a: (tagName, attribs) => ({\n tagName,\n attribs: {\n ...attribs,\n ...(attribs.target === \"_blank\" ? { rel: \"noopener noreferrer\" } : {}),\n },\n }),\n },\n });\n"],"mappings":";;;AAGA,MAAa,aAAa,SAAyB,WAAW,KAAK,QAAQ,YAAY,GAAG,CAAC,CAAC,MAAM;AAElG,MAAa,gBAAgB,SAC3B,gBAAgB,MAAM;CACpB,aAAa,gBAAgB,SAAS,YAAY,OAAO;EACvD;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;CACF,mBAAmB;EACjB,GAAG,gBAAgB,SAAS;EAC5B,KAAK;GAAC;GAAS;GAAM;GAAQ;EAC7B,KAAK;GAAC;GAAO;GAAU;GAAS;GAAO;GAAS;GAAU;GAAW;GAAW;EAChF,GAAG;GAAC;GAAQ;GAAQ;GAAU;GAAO;GAAQ;EAC7C,QAAQ;GAAC;GAAO;GAAS;GAAU;GAAe;GAAS;GAAkB;EAC7E,OAAO;GACL;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACD;EACD,OAAO;GAAC;GAAO;GAAY;GAAW;GAAQ;GAAW;EACzD,QAAQ;GAAC;GAAO;GAAQ;GAAQ;EAChC,OAAO;GAAC;GAAO;GAAQ;GAAW;GAAS;GAAU;EACtD;CACD,gBAAgB;EAAC;EAAQ;EAAS;EAAU;EAAO;EAAO;CAC1D,qBAAqB,EAAE,KAAK;EAAC;EAAQ;EAAS;EAAO,EAAE;CACvD,wBAAwB;EAAC;EAAmB;EAAe;EAAmB;CAC9E,eAAe,EACb,IAAI,SAAS,aAAa;EACxB;EACA,SAAS;GACP,GAAG;GACH,GAAI,QAAQ,WAAW,WAAW,EAAE,KAAK,uBAAuB,GAAG,EAAE;GACtE;EACF,GACF;CACF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@learnpress/utils",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "Shared utility helpers for LearnPress packages",
5
5
  "keywords": [
6
6
  "learnpress",