@eldrajs/rich-text 0.1.1 → 0.1.2
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/CHANGELOG.md +1 -0
- package/dist/index.d.ts +23 -13
- package/dist/index.js +65 -13
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,7 @@ Release-please writes the generated notes from commit messages and does not repl
|
|
|
5
5
|
|
|
6
6
|
## Unreleased
|
|
7
7
|
|
|
8
|
+
- **`price` nodes (WS-1474).** An inline `price` node holds a price token: `{ id, amounts }` (minor units per currency) or `{ setting }`. `toHtml(doc, { resolvePrice })` writes it as text with its marks, and leaves out the paragraph or heading holding one it cannot write; `resolvePrices(content, resolve)` does the same to a document. Without `resolvePrice` every such paragraph is left out, never a raw token.
|
|
8
9
|
- Tree-shakes: importing only `safeImageSrc` (or another URL helper) no longer bundles the HTML
|
|
9
10
|
renderer and the embed host list, 4.3 kB down to 235 B minified. No behaviour change.
|
|
10
11
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
//#region src/types.d.ts
|
|
2
1
|
interface RichTextMark {
|
|
3
2
|
type: string;
|
|
4
3
|
attrs?: Record<string, any>;
|
|
@@ -13,8 +12,6 @@ interface RichTextNode {
|
|
|
13
12
|
[key: string]: any;
|
|
14
13
|
}
|
|
15
14
|
type RichTextDocument = RichTextNode;
|
|
16
|
-
//#endregion
|
|
17
|
-
//#region src/text.d.ts
|
|
18
15
|
interface TextRendererOptions {
|
|
19
16
|
wordCount?: number;
|
|
20
17
|
ellipsis?: string;
|
|
@@ -27,8 +24,22 @@ export declare function normalizeTipTapText(text: string): string;
|
|
|
27
24
|
export declare function extractTipTapText(content: RichTextDocument | null | undefined): string;
|
|
28
25
|
export declare function truncateTipTapText(text: string, wordCount: number | null | undefined, ellipsis?: string): TextRendererResult;
|
|
29
26
|
export declare function renderTipTapText(content: RichTextDocument | null | undefined, options?: TextRendererOptions): TextRendererResult;
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Writes a `price` node's attributes in the shopper's currency, or answers `null` when it cannot.
|
|
29
|
+
* A theme binds `@eldrajs/sdk/tokens`' `resolvePriceNode` to its currency, regional locale and
|
|
30
|
+
* money formatter; this package imports nothing, so the resolver is always the caller's.
|
|
31
|
+
*/
|
|
32
|
+
type PriceResolver = (attrs: unknown) => string | null;
|
|
33
|
+
/**
|
|
34
|
+
* The document with every `price` node (a price token in rich text, WS-1474) written out as text in
|
|
35
|
+
* the shopper's currency, carrying the node's own marks. A paragraph or heading holding a price that
|
|
36
|
+
* cannot be written is left out whole: a missing amount reads as a shorter text, never as a sentence
|
|
37
|
+
* with a hole in it, and never as another currency's amount.
|
|
38
|
+
*
|
|
39
|
+
* Without a resolver no price resolves. The input is never modified; a document with no `price`
|
|
40
|
+
* node comes back as the same object.
|
|
41
|
+
*/
|
|
42
|
+
export declare function resolvePrices(content: RichTextDocument | null | undefined, resolve?: PriceResolver | null): RichTextDocument | null | undefined;
|
|
32
43
|
type StyledTagOverride = {
|
|
33
44
|
tag?: string;
|
|
34
45
|
class?: string | string[] | Record<string, boolean>;
|
|
@@ -43,8 +54,6 @@ interface ResolvedOverride<TComponent> {
|
|
|
43
54
|
style?: StyledTagOverride['style'];
|
|
44
55
|
}
|
|
45
56
|
export declare const resolveOverride: <TComponent>(override: RichTextOverride<TComponent> | undefined, defaultComponent: string | TComponent) => ResolvedOverride<TComponent> | null;
|
|
46
|
-
//#endregion
|
|
47
|
-
//#region src/html.d.ts
|
|
48
57
|
interface HtmlRenderContext {
|
|
49
58
|
render: (node: RichTextNode) => string;
|
|
50
59
|
escape: (text: string) => string;
|
|
@@ -56,19 +65,21 @@ type HtmlOverride<TRenderer> = string | StyledTagOverride | TRenderer | null;
|
|
|
56
65
|
interface ToHtmlOptions {
|
|
57
66
|
nodes?: Partial<Record<string, HtmlOverride<HtmlNodeRenderer>>>;
|
|
58
67
|
marks?: Partial<Record<string, HtmlOverride<HtmlMarkRenderer>>>;
|
|
68
|
+
/**
|
|
69
|
+
* Writes a `price` node (a price token, WS-1474) in the shopper's currency; see
|
|
70
|
+
* `resolvePrices`. The written text is escaped like any other text. Without one no price
|
|
71
|
+
* resolves, and the paragraph or heading holding it is left out.
|
|
72
|
+
*/
|
|
73
|
+
resolvePrice?: PriceResolver | null;
|
|
59
74
|
}
|
|
60
75
|
type Attributes = Record<string, string | number | boolean | null | undefined>;
|
|
61
76
|
type Extra = Pick<StyledTagOverride, 'class' | 'style'>;
|
|
62
77
|
export declare function escapeHtml(text: string): string;
|
|
63
78
|
export declare function element(tag: string, attributes: Attributes, children?: string, extra?: Extra): string;
|
|
64
79
|
export declare function toHtml(content: RichTextDocument | null | undefined, options?: ToHtmlOptions): string;
|
|
65
|
-
//#endregion
|
|
66
|
-
//#region src/url.d.ts
|
|
67
80
|
export declare const safeHref: (value: unknown) => string | undefined;
|
|
68
81
|
export declare const safeImageSrc: (value: unknown) => string | undefined;
|
|
69
82
|
export declare const safeCssColor: (value: unknown) => string | undefined;
|
|
70
|
-
//#endregion
|
|
71
|
-
//#region src/embed.d.ts
|
|
72
83
|
type SupportedEmbedProvider = 'youtube' | 'twitch' | 'vimeo' | 'soundcloud' | 'spotify' | 'instagram' | 'tiktok' | 'x' | 'facebook' | 'googleMaps' | 'appleMusic' | 'applePodcasts' | 'reddit' | 'pinterest' | 'linkedin';
|
|
73
84
|
type EmbedRenderMode = 'iframe' | 'script';
|
|
74
85
|
type EmbedWidth = number | `${number}%`;
|
|
@@ -87,5 +98,4 @@ export declare const normalizeEmbedInput: (input: string) => NormalizedEmbedAttr
|
|
|
87
98
|
export declare const extractSupportedEmbedUrl: (input: string) => string | null;
|
|
88
99
|
export declare const EMBED_SOURCE_HOSTS: ReadonlySet<string>;
|
|
89
100
|
export declare const isTrustedEmbedSource: (value: unknown) => value is string;
|
|
90
|
-
|
|
91
|
-
export type { EmbedRenderMode, EmbedWidth, HtmlMarkRenderer, HtmlNodeRenderer, HtmlOverride, HtmlRenderContext, NormalizedEmbedAttributes, ResolvedOverride, RichTextDocument, RichTextMark, RichTextNode, RichTextOverride, StyledTagOverride, SupportedEmbedProvider, TextRendererOptions, TextRendererResult, ToHtmlOptions };
|
|
101
|
+
export type { EmbedRenderMode, EmbedWidth, HtmlMarkRenderer, HtmlNodeRenderer, HtmlOverride, HtmlRenderContext, NormalizedEmbedAttributes, PriceResolver, ResolvedOverride, RichTextDocument, RichTextMark, RichTextNode, RichTextOverride, StyledTagOverride, SupportedEmbedProvider, TextRendererOptions, TextRendererResult, ToHtmlOptions };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/text.ts
|
|
2
2
|
const blockNodeTypes = /* @__PURE__ */ new Set([
|
|
3
3
|
"blockquote",
|
|
4
4
|
"bulletList",
|
|
@@ -55,8 +55,63 @@ function truncateTipTapText(text, wordCount, ellipsis = "...") {
|
|
|
55
55
|
function renderTipTapText(content, options = {}) {
|
|
56
56
|
return truncateTipTapText(extractTipTapText(content), options.wordCount, options.ellipsis);
|
|
57
57
|
}
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
// src/prices.ts
|
|
59
|
+
/** The nodes a price can sit in directly: dropping one drops the sentence-sized unit. */
|
|
60
|
+
const TEXTBLOCKS = /* @__PURE__ */ new Set(["paragraph", "heading"]);
|
|
61
|
+
/**
|
|
62
|
+
* The document with every `price` node (a price token in rich text, WS-1474) written out as text in
|
|
63
|
+
* the shopper's currency, carrying the node's own marks. A paragraph or heading holding a price that
|
|
64
|
+
* cannot be written is left out whole: a missing amount reads as a shorter text, never as a sentence
|
|
65
|
+
* with a hole in it, and never as another currency's amount.
|
|
66
|
+
*
|
|
67
|
+
* Without a resolver no price resolves. The input is never modified; a document with no `price`
|
|
68
|
+
* node comes back as the same object.
|
|
69
|
+
*/
|
|
70
|
+
function resolvePrices(content, resolve) {
|
|
71
|
+
if (!content || !holdsPrice(content)) return content;
|
|
72
|
+
return transform(content, resolve ?? null) ?? void 0;
|
|
73
|
+
}
|
|
74
|
+
function holdsPrice(node) {
|
|
75
|
+
return node.type === "price" || (node.content ?? []).some(holdsPrice);
|
|
76
|
+
}
|
|
77
|
+
/** The node with its prices written, or `null` when it is a price or a block to leave out. */
|
|
78
|
+
function transform(node, resolve) {
|
|
79
|
+
if (node.type === "price") {
|
|
80
|
+
const text = resolve?.(node.attrs) ?? null;
|
|
81
|
+
if (text === null) return null;
|
|
82
|
+
return node.marks ? {
|
|
83
|
+
type: "text",
|
|
84
|
+
text,
|
|
85
|
+
marks: node.marks
|
|
86
|
+
} : {
|
|
87
|
+
type: "text",
|
|
88
|
+
text
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
if (!node.content) return node;
|
|
92
|
+
if (typeof node.type === "string" && TEXTBLOCKS.has(node.type)) {
|
|
93
|
+
const children = [];
|
|
94
|
+
for (const child of node.content) {
|
|
95
|
+
const written = transform(child, resolve);
|
|
96
|
+
if (written === null) return null;
|
|
97
|
+
children.push(written);
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
...node,
|
|
101
|
+
content: children
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const children = [];
|
|
105
|
+
for (const child of node.content) {
|
|
106
|
+
const written = transform(child, resolve);
|
|
107
|
+
if (written !== null) children.push(written);
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
...node,
|
|
111
|
+
content: children
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
// src/embed.ts
|
|
60
115
|
const DEFAULT_EMBED_DIMENSIONS = {
|
|
61
116
|
youtube: {
|
|
62
117
|
width: 640,
|
|
@@ -692,8 +747,7 @@ const isTrustedEmbedSource = (value) => {
|
|
|
692
747
|
}
|
|
693
748
|
return url.protocol === "https:" && EMBED_SOURCE_HOSTS.has(url.hostname.replace(/^www\./, ""));
|
|
694
749
|
};
|
|
695
|
-
|
|
696
|
-
//#region src/overrides.ts
|
|
750
|
+
// src/overrides.ts
|
|
697
751
|
const isStringTag = (value) => typeof value === "string";
|
|
698
752
|
const isStyledOverride = (value) => typeof value === "object" && value !== null && !("render" in value) && !("setup" in value) && !("template" in value) && ("tag" in value || "class" in value || "style" in value);
|
|
699
753
|
const resolveOverride = (override, defaultComponent) => {
|
|
@@ -707,8 +761,7 @@ const resolveOverride = (override, defaultComponent) => {
|
|
|
707
761
|
};
|
|
708
762
|
return { is: override };
|
|
709
763
|
};
|
|
710
|
-
|
|
711
|
-
//#region src/url.ts
|
|
764
|
+
// src/url.ts
|
|
712
765
|
const HREF_SCHEMES = /^(https?|mailto|tel):/i;
|
|
713
766
|
const IMAGE_SCHEMES = /^(https?:|data:image\/)/i;
|
|
714
767
|
const CONTROL_AND_SPACE = /[- ]/g;
|
|
@@ -725,8 +778,7 @@ const safeHref = (value) => safeUrl(value, HREF_SCHEMES);
|
|
|
725
778
|
const safeImageSrc = (value) => safeUrl(value, IMAGE_SCHEMES);
|
|
726
779
|
const CSS_COLOR = /^(#[0-9a-f]{3,8}|(rgb|hsl)a?\([\d\s.,%/]+\)|[a-z]+|(oklch|oklab|lab|lch)\([\d\s.,%/a-z-]+\))$/i;
|
|
727
780
|
const safeCssColor = (value) => typeof value === "string" && CSS_COLOR.test(value.trim()) ? value.trim() : void 0;
|
|
728
|
-
|
|
729
|
-
//#region src/html.ts
|
|
781
|
+
// src/html.ts
|
|
730
782
|
const VOID_TAGS = /* @__PURE__ */ new Set([
|
|
731
783
|
"br",
|
|
732
784
|
"hr",
|
|
@@ -898,7 +950,8 @@ function renderWithOverride(override, fallback, children, custom) {
|
|
|
898
950
|
return custom(override);
|
|
899
951
|
}
|
|
900
952
|
function toHtml(content, options = {}) {
|
|
901
|
-
|
|
953
|
+
const document = resolvePrices(content, options.resolvePrice);
|
|
954
|
+
if (!document) return "";
|
|
902
955
|
const nodes = options.nodes ?? {};
|
|
903
956
|
const marks = options.marks ?? {};
|
|
904
957
|
const context = {
|
|
@@ -921,7 +974,6 @@ function toHtml(content, options = {}) {
|
|
|
921
974
|
if (override === void 0 && !fallback) return children();
|
|
922
975
|
return renderWithOverride(override, fallback ? (extra) => fallback(node, children(), context, extra) : null, children(), (renderer) => renderer(node, children(), context));
|
|
923
976
|
}
|
|
924
|
-
return renderNode(
|
|
977
|
+
return renderNode(document);
|
|
925
978
|
}
|
|
926
|
-
|
|
927
|
-
export { EMBED_SOURCE_HOSTS, SUPPORTED_EMBED_URL_REGEX_GLOBAL, element, escapeHtml, extractSupportedEmbedUrl, extractTipTapText, isStringTag, isStyledOverride, isTrustedEmbedSource, normalizeEmbedInput, normalizeTipTapText, renderTipTapText, resolveOverride, safeCssColor, safeHref, safeImageSrc, toHtml, truncateTipTapText };
|
|
979
|
+
export { EMBED_SOURCE_HOSTS, SUPPORTED_EMBED_URL_REGEX_GLOBAL, element, escapeHtml, extractSupportedEmbedUrl, extractTipTapText, isStringTag, isStyledOverride, isTrustedEmbedSource, normalizeEmbedInput, normalizeTipTapText, renderTipTapText, resolveOverride, resolvePrices, safeCssColor, safeHref, safeImageSrc, toHtml, truncateTipTapText };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eldrajs/rich-text",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Framework-free rendering for Eldra CMS rich text: the document types, plain-text extraction, an escaping HTML serialiser, and embed normalisation.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"eldra",
|