@blinkk/root-cms 3.0.1-beta.7 → 3.0.1-beta.9

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/dist/richtext.js CHANGED
@@ -5,6 +5,101 @@ import { StringParamsProvider, useTranslations } from "@blinkk/root";
5
5
  import { Component, createContext } from "preact";
6
6
  import { useContext } from "preact/hooks";
7
7
  import { renderToString } from "preact-render-to-string";
8
+
9
+ // shared/sanitize.ts
10
+ import sanitizeHtml from "sanitize-html";
11
+ var INLINE_TAGS = [
12
+ "a",
13
+ "b",
14
+ "strong",
15
+ "i",
16
+ "em",
17
+ "u",
18
+ "s",
19
+ "del",
20
+ "code",
21
+ "span",
22
+ "br",
23
+ "sub",
24
+ "sup",
25
+ "mark",
26
+ "small"
27
+ ];
28
+ var BLOCK_TAGS = [
29
+ ...INLINE_TAGS,
30
+ "p",
31
+ "h1",
32
+ "h2",
33
+ "h3",
34
+ "h4",
35
+ "h5",
36
+ "h6",
37
+ "ul",
38
+ "ol",
39
+ "li",
40
+ "blockquote",
41
+ "pre",
42
+ "hr",
43
+ "table",
44
+ "thead",
45
+ "tbody",
46
+ "tfoot",
47
+ "tr",
48
+ "th",
49
+ "td",
50
+ "caption",
51
+ "figure",
52
+ "figcaption",
53
+ "img",
54
+ "div"
55
+ ];
56
+ var ALLOWED_ATTRIBUTES = {
57
+ a: ["href", "title", "target", "rel", "id"],
58
+ span: ["class"],
59
+ code: ["class"],
60
+ pre: ["class"],
61
+ th: ["align", "scope", "colspan", "rowspan"],
62
+ td: ["align", "colspan", "rowspan"],
63
+ img: ["src", "alt", "title", "width", "height"]
64
+ };
65
+ var COMMON_OPTIONS = {
66
+ allowedSchemes: ["http", "https", "mailto"],
67
+ allowedSchemesByTag: {
68
+ img: ["http", "https", "data"]
69
+ },
70
+ allowProtocolRelative: false,
71
+ transformTags: {
72
+ a: (tagName, attribs) => {
73
+ const next = { ...attribs };
74
+ if (next.target === "_blank") {
75
+ next.rel = "noopener noreferrer";
76
+ }
77
+ return { tagName, attribs: next };
78
+ }
79
+ }
80
+ };
81
+ function sanitizeInlineHtml(html) {
82
+ if (!html) {
83
+ return "";
84
+ }
85
+ return sanitizeHtml(html, {
86
+ ...COMMON_OPTIONS,
87
+ allowedTags: INLINE_TAGS,
88
+ allowedAttributes: ALLOWED_ATTRIBUTES
89
+ });
90
+ }
91
+ function sanitizeBlockHtml(html) {
92
+ if (!html) {
93
+ return "";
94
+ }
95
+ return sanitizeHtml(html, {
96
+ ...COMMON_OPTIONS,
97
+ allowedTags: BLOCK_TAGS,
98
+ allowedAttributes: ALLOWED_ATTRIBUTES
99
+ });
100
+ }
101
+
102
+ // core/richtext.tsx
8
103
  import { jsx, jsxs } from "preact/jsx-runtime";
9
104
  var RichTextContext = createContext({});
10
105
  function useRichTextContext() {
@@ -82,7 +177,7 @@ RichText.ParagraphBlock = (props) => {
82
177
  return null;
83
178
  }
84
179
  const t = useRichTextTranslations();
85
- const html = t(props.data.text);
180
+ const html = sanitizeInlineHtml(t(props.data.text));
86
181
  return /* @__PURE__ */ jsx("p", { dangerouslySetInnerHTML: { __html: html } });
87
182
  };
88
183
  RichText.HeadingBlock = (props) => {
@@ -92,7 +187,7 @@ RichText.HeadingBlock = (props) => {
92
187
  const t = useRichTextTranslations();
93
188
  const level = props.data.level || 2;
94
189
  const Component2 = `h${level}`;
95
- const html = t(props.data.text);
190
+ const html = sanitizeInlineHtml(t(props.data.text));
96
191
  return /* @__PURE__ */ jsx(Component2, { dangerouslySetInnerHTML: { __html: html } });
97
192
  };
98
193
  RichText.ListBlock = (props) => {
@@ -139,7 +234,7 @@ RichText.ImageBlock = (props) => {
139
234
  };
140
235
  RichText.HtmlBlock = (props) => {
141
236
  const t = useRichTextTranslations();
142
- const html = t(props.data?.html || "");
237
+ const html = sanitizeBlockHtml(t(props.data?.html || ""));
143
238
  if (!html) {
144
239
  return null;
145
240
  }