@supatype/common 0.3.6 → 0.8.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.
Files changed (48) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/.turbo/turbo-test.log +14 -0
  3. package/.turbo/turbo-typecheck.log +2 -2
  4. package/LICENSE +1 -1
  5. package/dist/analytics.d.ts.map +1 -1
  6. package/dist/analytics.js.map +1 -1
  7. package/dist/auth.d.ts.map +1 -1
  8. package/dist/auth.js +11 -1
  9. package/dist/auth.js.map +1 -1
  10. package/dist/build-env.d.ts +7 -0
  11. package/dist/build-env.d.ts.map +1 -0
  12. package/dist/build-env.js +17 -0
  13. package/dist/build-env.js.map +1 -0
  14. package/dist/config.d.ts.map +1 -1
  15. package/dist/config.js +2 -6
  16. package/dist/config.js.map +1 -1
  17. package/dist/content.d.ts.map +1 -1
  18. package/dist/content.js +2 -6
  19. package/dist/content.js.map +1 -1
  20. package/dist/index.d.ts +1 -1
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +1 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/richtext-tree.d.ts +55 -0
  25. package/dist/richtext-tree.d.ts.map +1 -0
  26. package/dist/richtext-tree.js +208 -0
  27. package/dist/richtext-tree.js.map +1 -0
  28. package/dist/richtext.d.ts +8 -67
  29. package/dist/richtext.d.ts.map +1 -1
  30. package/dist/richtext.js +59 -80
  31. package/dist/richtext.js.map +1 -1
  32. package/package.json +8 -4
  33. package/src/analytics.ts +3 -2
  34. package/src/auth.ts +10 -1
  35. package/src/build-env.ts +16 -0
  36. package/src/config.ts +3 -5
  37. package/src/content.ts +2 -5
  38. package/src/index.ts +1 -1
  39. package/src/posthog-js.d.ts +1 -1
  40. package/src/richtext-tree.ts +274 -0
  41. package/src/richtext.ts +67 -112
  42. package/tests/fixtures/lexical-code-block.json +92 -0
  43. package/tests/fixtures/lexical-documents.json +1345 -0
  44. package/tests/richtext-cases.ts +154 -0
  45. package/tests/richtext.test.ts +73 -0
  46. package/tests/tsconfig.json +10 -0
  47. package/tsconfig.json +3 -2
  48. package/tsconfig.tsbuildinfo +1 -1
package/src/richtext.ts CHANGED
@@ -1,127 +1,82 @@
1
1
  /**
2
- * Rich text rendering: converts Lexical JSON to HTML.
2
+ * Rich text rendering: prints a saved Lexical document as an HTML string.
3
+ *
4
+ * The document is walked once by `richTextTree` (shared with the React renderer); this module only
5
+ * prints that tree, escaping every text run and attribute value.
3
6
  */
4
7
 
5
- export type RichTextNode =
6
- | { type: "heading"; tag: "h2" | "h3" | "h4"; children: RichTextNode[] }
7
- | { type: "paragraph"; children: RichTextNode[] }
8
- | { type: "text"; text: string; format?: { bold?: boolean; italic?: boolean; code?: boolean } }
9
- | { type: "link"; url: string; children: RichTextNode[] }
10
- | { type: "code"; language: string; code: string }
11
- | { type: "image"; src: string; alt: string; width?: number; height?: number }
12
- | { type: "blockquote"; children: RichTextNode[] }
13
- | { type: "list"; listType: "ordered" | "unordered"; children: RichTextNode[] }
14
- | { type: "listitem"; children: RichTextNode[] }
15
- | { type: "horizontalrule" }
16
- | { type: "root"; children: RichTextNode[] }
17
- | { type: "callout"; variant: "tip" | "warning" | "danger"; children: RichTextNode[] }
18
- | { type: "code-tabs"; tabs: Array<{ label: string; language: string; code: string }> }
19
- | { type: "cta"; text: string; url: string }
20
- | { type: "embed"; provider: "youtube" | "codesandbox"; embedId: string }
8
+ import { richTextTree, type RichTextAttributes, type RichTextChild, type RichTextContent, type RichTextStyle } from "./richtext-tree.js"
21
9
 
22
- const CALLOUT_ICONS: Record<"tip" | "warning" | "danger", string> = {
23
- tip: "\u{1F4A1}",
24
- warning: "\u26A0\uFE0F",
25
- danger: "\u{1F6AB}",
26
- }
10
+ export {
11
+ richTextTree,
12
+ safeHref,
13
+ type RichTextAttributes,
14
+ type RichTextChild,
15
+ type RichTextContent,
16
+ type RichTextElement,
17
+ type RichTextNode,
18
+ type RichTextStyle,
19
+ type RichTextTag,
20
+ } from "./richtext-tree.js"
21
+
22
+ const VOID_TAGS: ReadonlySet<string> = new Set(["br", "hr", "input"])
23
+
24
+ const ESCAPES: Readonly<Record<string, string>> = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }
27
25
 
28
26
  function escapeHtml(text: string): string {
29
- return text
30
- .replace(/&/g, "&amp;")
31
- .replace(/</g, "&lt;")
32
- .replace(/>/g, "&gt;")
33
- .replace(/"/g, "&quot;")
34
- .replace(/'/g, "&#39;")
27
+ return text.replace(/[&<>"']/g, (char) => ESCAPES[char] ?? char)
35
28
  }
36
29
 
37
- function renderChildren(children: RichTextNode[]): string {
38
- return children.map(renderRichText).join("")
30
+ function styleText(style: RichTextStyle): string {
31
+ return Object.entries(style)
32
+ .map(([property, value]) => `${property.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`)}:${value}`)
33
+ .join(";")
39
34
  }
40
35
 
41
- /**
42
- * Convert a Lexical JSON node tree into an HTML string.
43
- */
44
- export function renderRichText(node: RichTextNode): string {
45
- switch (node.type) {
46
- case "root":
47
- return renderChildren(node.children)
48
-
49
- case "heading":
50
- return `<${node.tag}>${renderChildren(node.children)}</${node.tag}>`
51
-
52
- case "paragraph":
53
- return `<p>${renderChildren(node.children)}</p>`
54
-
55
- case "text": {
56
- let html = escapeHtml(node.text)
57
- if (node.format?.code) html = `<code>${html}</code>`
58
- if (node.format?.bold) html = `<strong>${html}</strong>`
59
- if (node.format?.italic) html = `<em>${html}</em>`
60
- return html
61
- }
62
-
63
- case "link":
64
- return `<a href="${escapeHtml(node.url)}">${renderChildren(node.children)}</a>`
65
-
66
- case "code":
67
- return `<pre><code class="language-${escapeHtml(node.language)}">${escapeHtml(node.code)}</code></pre>`
68
-
69
- case "image": {
70
- const attrs = [
71
- `src="${escapeHtml(node.src)}"`,
72
- `alt="${escapeHtml(node.alt)}"`,
73
- ...(node.width !== undefined ? [`width="${node.width}"`] : []),
74
- ...(node.height !== undefined ? [`height="${node.height}"`] : []),
75
- ]
76
- return `<img ${attrs.join(" ")} />`
77
- }
78
-
79
- case "blockquote":
80
- return `<blockquote>${renderChildren(node.children)}</blockquote>`
81
-
82
- case "list": {
83
- const tag = node.listType === "ordered" ? "ol" : "ul"
84
- return `<${tag}>${renderChildren(node.children)}</${tag}>`
85
- }
86
-
87
- case "listitem":
88
- return `<li>${renderChildren(node.children)}</li>`
89
-
90
- case "horizontalrule":
91
- return `<hr />`
92
-
93
- case "callout":
94
- return `<div class="callout callout-${node.variant}">${CALLOUT_ICONS[node.variant]} ${renderChildren(node.children)}</div>`
36
+ /** Every attribute an element can carry: the compiler checks this names each one, and no other. */
37
+ const ATTRIBUTE_NAMES: Readonly<Record<keyof RichTextAttributes, true>> = {
38
+ href: true,
39
+ rel: true,
40
+ target: true,
41
+ title: true,
42
+ dir: true,
43
+ start: true,
44
+ value: true,
45
+ class: true,
46
+ type: true,
47
+ checked: true,
48
+ disabled: true,
49
+ style: true,
50
+ }
95
51
 
96
- case "code-tabs": {
97
- const buttons = node.tabs
98
- .map(
99
- (tab, i) =>
100
- `<button class="code-tab-button${i === 0 ? " active" : ""}" data-tab="${i}">${escapeHtml(tab.label)}</button>`,
101
- )
102
- .join("")
103
- const panels = node.tabs
104
- .map(
105
- (tab, i) =>
106
- `<div class="code-tab-panel${i === 0 ? " active" : ""}" data-tab="${i}"><pre><code class="language-${escapeHtml(tab.language)}">${escapeHtml(tab.code)}</code></pre></div>`,
107
- )
108
- .join("")
109
- return `<div class="code-tabs"><div class="code-tabs-buttons">${buttons}</div>${panels}</div>`
110
- }
52
+ function isAttributeName(name: string): name is keyof RichTextAttributes {
53
+ return Object.hasOwn(ATTRIBUTE_NAMES, name)
54
+ }
111
55
 
112
- case "cta":
113
- return `<a class="cta-button" href="${escapeHtml(node.url)}">${escapeHtml(node.text)}</a>`
56
+ function attributeText(attributes: RichTextAttributes): string {
57
+ let out = ""
58
+ // `Object.entries` types an interface's values as `any`; read each through its own key instead.
59
+ for (const name of Object.keys(attributes).filter(isAttributeName)) {
60
+ const value = attributes[name]
61
+ if (value === undefined || value === false) continue
62
+ if (value === true) out += ` ${name}`
63
+ else if (typeof value === "object") out += ` ${name}="${escapeHtml(styleText(value))}"`
64
+ else out += ` ${name}="${escapeHtml(String(value))}"`
65
+ }
66
+ return out
67
+ }
114
68
 
115
- case "embed": {
116
- if (node.provider === "youtube") {
117
- return `<iframe src="https://www.youtube-nocookie.com/embed/${escapeHtml(node.embedId)}" frameborder="0" allowfullscreen loading="lazy"></iframe>`
118
- }
119
- return `<iframe src="https://codesandbox.io/embed/${escapeHtml(node.embedId)}" frameborder="0" allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" loading="lazy"></iframe>`
120
- }
69
+ function printChild(child: RichTextChild): string {
70
+ if (typeof child === "string") return escapeHtml(child)
71
+ const attributes = attributeText(child.attributes)
72
+ if (VOID_TAGS.has(child.tag)) return `<${child.tag}${attributes} />`
73
+ return `<${child.tag}${attributes}>${child.children.map(printChild).join("")}</${child.tag}>`
74
+ }
121
75
 
122
- default: {
123
- const _exhaustive: never = node
124
- return ""
125
- }
126
- }
76
+ /**
77
+ * Print a saved Lexical document (the editor state, its root node, or a list of nodes) as HTML.
78
+ * Null, undefined and anything else that is not a document print as an empty string.
79
+ */
80
+ export function renderRichText(content: unknown): string {
81
+ return richTextTree(content).map(printChild).join("")
127
82
  }
@@ -0,0 +1,92 @@
1
+ {
2
+ "codeBlock": {
3
+ "root": {
4
+ "children": [
5
+ {
6
+ "children": [
7
+ {
8
+ "detail": 0,
9
+ "format": 0,
10
+ "mode": "normal",
11
+ "style": "",
12
+ "text": "const",
13
+ "type": "code-highlight",
14
+ "version": 1,
15
+ "highlightType": "keyword"
16
+ },
17
+ {
18
+ "detail": 0,
19
+ "format": 0,
20
+ "mode": "normal",
21
+ "style": "",
22
+ "text": " a = ",
23
+ "type": "code-highlight",
24
+ "version": 1
25
+ },
26
+ {
27
+ "detail": 0,
28
+ "format": 0,
29
+ "mode": "normal",
30
+ "style": "",
31
+ "text": "\"<b>&\"",
32
+ "type": "code-highlight",
33
+ "version": 1,
34
+ "highlightType": "string"
35
+ },
36
+ {
37
+ "type": "linebreak",
38
+ "version": 1
39
+ },
40
+ {
41
+ "detail": 2,
42
+ "format": 0,
43
+ "mode": "normal",
44
+ "style": "",
45
+ "text": "\t",
46
+ "type": "tab",
47
+ "version": 1
48
+ },
49
+ {
50
+ "detail": 0,
51
+ "format": 0,
52
+ "mode": "normal",
53
+ "style": "",
54
+ "text": "return a",
55
+ "type": "code-highlight",
56
+ "version": 1
57
+ }
58
+ ],
59
+ "direction": null,
60
+ "format": "",
61
+ "indent": 0,
62
+ "type": "code",
63
+ "version": 1,
64
+ "language": "typescript"
65
+ },
66
+ {
67
+ "children": [
68
+ {
69
+ "detail": 0,
70
+ "format": 0,
71
+ "mode": "normal",
72
+ "style": "",
73
+ "text": "x < y",
74
+ "type": "text",
75
+ "version": 1
76
+ }
77
+ ],
78
+ "direction": null,
79
+ "format": "",
80
+ "indent": 0,
81
+ "type": "code",
82
+ "version": 1
83
+ }
84
+ ],
85
+ "direction": null,
86
+ "format": "",
87
+ "indent": 0,
88
+ "type": "root",
89
+ "version": 1
90
+ }
91
+ }
92
+ }