@learnpress/utils 0.2.0 → 0.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 +22 -2
- package/dist/index.cjs +14 -11
- package/dist/index.d.cts +4 -9
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +4 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -7
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
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
|
-
##
|
|
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 {
|
|
18
|
+
import { stripHtml } from "@learnpress/utils";
|
|
19
|
+
|
|
20
|
+
stripHtml("<p>Hello & <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
|
@@ -16,7 +16,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
}
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
20
20
|
value: mod,
|
|
21
21
|
enumerable: true
|
|
22
22
|
}) : target, mod));
|
|
@@ -24,6 +24,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
24
24
|
let entities = require("entities");
|
|
25
25
|
let sanitize_html = require("sanitize-html");
|
|
26
26
|
sanitize_html = __toESM(sanitize_html, 1);
|
|
27
|
+
let tailwind_variants = require("tailwind-variants");
|
|
27
28
|
//#region src/html.ts
|
|
28
29
|
const stripHtml = (html) => (0, entities.decodeHTML)(html.replace(/<[^>]*>/g, "")).trim();
|
|
29
30
|
const sanitizeHtml = (html) => (0, sanitize_html.default)(html, {
|
|
@@ -127,15 +128,17 @@ const sanitizeHtml = (html) => (0, sanitize_html.default)(html, {
|
|
|
127
128
|
}) }
|
|
128
129
|
});
|
|
129
130
|
//#endregion
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
exports.isNil = isNil;
|
|
137
|
-
exports.isNumber = isNumber;
|
|
138
|
-
exports.isObject = isObject;
|
|
139
|
-
exports.isString = isString;
|
|
131
|
+
Object.defineProperty(exports, "cn", {
|
|
132
|
+
enumerable: true,
|
|
133
|
+
get: function() {
|
|
134
|
+
return tailwind_variants.cn;
|
|
135
|
+
}
|
|
136
|
+
});
|
|
140
137
|
exports.sanitizeHtml = sanitizeHtml;
|
|
141
138
|
exports.stripHtml = stripHtml;
|
|
139
|
+
Object.defineProperty(exports, "tv", {
|
|
140
|
+
enumerable: true,
|
|
141
|
+
get: function() {
|
|
142
|
+
return tailwind_variants.tv;
|
|
143
|
+
}
|
|
144
|
+
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
|
+
import { VariantProps, cn, tv } from "tailwind-variants";
|
|
1
2
|
//#region src/html.d.ts
|
|
2
|
-
declare const stripHtml: (html: string) => string;
|
|
3
|
-
declare const sanitizeHtml: (html: string) => string;
|
|
3
|
+
export declare const stripHtml: (html: string) => string;
|
|
4
|
+
export declare const sanitizeHtml: (html: string) => string;
|
|
4
5
|
//#endregion
|
|
5
|
-
|
|
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 };
|
|
6
|
+
export { type VariantProps, cn, tv };
|
|
12
7
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/html.ts"
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/html.ts"],"mappings":";;qBAGa,YAAS;qBAET,eAAY"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
|
+
import { VariantProps, cn, tv } from "tailwind-variants";
|
|
1
2
|
//#region src/html.d.ts
|
|
2
|
-
declare const stripHtml: (html: string) => string;
|
|
3
|
-
declare const sanitizeHtml: (html: string) => string;
|
|
3
|
+
export declare const stripHtml: (html: string) => string;
|
|
4
|
+
export declare const sanitizeHtml: (html: string) => string;
|
|
4
5
|
//#endregion
|
|
5
|
-
|
|
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 };
|
|
6
|
+
export { type VariantProps, cn, tv };
|
|
12
7
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/html.ts"
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/html.ts"],"mappings":";;qBAGa,YAAS;qBAET,eAAY"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { decodeHTML } from "entities";
|
|
2
2
|
import sanitizeHtmlLib from "sanitize-html";
|
|
3
|
+
import { cn, tv } from "tailwind-variants";
|
|
3
4
|
//#region src/html.ts
|
|
4
5
|
const stripHtml = (html) => decodeHTML(html.replace(/<[^>]*>/g, "")).trim();
|
|
5
6
|
const sanitizeHtml = (html) => sanitizeHtmlLib(html, {
|
|
@@ -103,12 +104,6 @@ const sanitizeHtml = (html) => sanitizeHtmlLib(html, {
|
|
|
103
104
|
}) }
|
|
104
105
|
});
|
|
105
106
|
//#endregion
|
|
106
|
-
|
|
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 };
|
|
107
|
+
export { cn, sanitizeHtml, stripHtml, tv };
|
|
113
108
|
|
|
114
109
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/html.ts"
|
|
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,EAAE,CAAC,CAAC,CAAC,KAAK;AAEjG,MAAa,gBAAgB,SAC3B,gBAAgB,MAAM;CACpB,aAAa,gBAAgB,SAAS,YAAY,OAAO;EACvD;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;CACD,mBAAmB;EACjB,GAAG,gBAAgB,SAAS;EAC5B,KAAK;GAAC;GAAS;GAAM;EAAO;EAC5B,KAAK;GAAC;GAAO;GAAU;GAAS;GAAO;GAAS;GAAU;GAAW;EAAU;EAC/E,GAAG;GAAC;GAAQ;GAAQ;GAAU;GAAO;EAAO;EAC5C,QAAQ;GAAC;GAAO;GAAS;GAAU;GAAe;GAAS;EAAiB;EAC5E,OAAO;GACL;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;EACF;EACA,OAAO;GAAC;GAAO;GAAY;GAAW;GAAQ;EAAU;EACxD,QAAQ;GAAC;GAAO;GAAQ;EAAO;EAC/B,OAAO;GAAC;GAAO;GAAQ;GAAW;GAAS;EAAS;CACtD;CACA,gBAAgB;EAAC;EAAQ;EAAS;EAAU;EAAO;CAAM;CACzD,qBAAqB,EAAE,KAAK;EAAC;EAAQ;EAAS;CAAM,EAAE;CACtD,wBAAwB;EAAC;EAAmB;EAAe;CAAkB;CAC7E,eAAe,EACb,IAAI,SAAS,aAAa;EACxB;EACA,SAAS;GACP,GAAG;GACH,GAAI,QAAQ,WAAW,WAAW,EAAE,KAAK,sBAAsB,IAAI,CAAC;EACtE;CACF,GACF;AACF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@learnpress/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Shared utility helpers for LearnPress packages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"learnpress",
|
|
@@ -41,7 +41,9 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"entities": "^8.0.0",
|
|
44
|
-
"sanitize-html": "^2.17.
|
|
44
|
+
"sanitize-html": "^2.17.7",
|
|
45
|
+
"tailwind-merge": "^3.6.0",
|
|
46
|
+
"tailwind-variants": "^3.3.1"
|
|
45
47
|
},
|
|
46
48
|
"devDependencies": {
|
|
47
49
|
"@types/sanitize-html": "^2.16.1",
|