@synerise/ds-rich-text-renderer 0.1.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/CHANGELOG.md +10 -0
- package/LICENSE.md +21 -0
- package/README.md +111 -0
- package/dist/RichTextRenderer.d.ts +7 -0
- package/dist/RichTextRenderer.js +15 -0
- package/dist/RichTextRenderer.styles.d.ts +2 -0
- package/dist/RichTextRenderer.styles.js +8 -0
- package/dist/RichTextRenderer.types.d.ts +11 -0
- package/dist/RichTextRenderer.types.js +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +10 -0
- package/dist/renderers/index.d.ts +3 -0
- package/dist/renderers/index.js +8 -0
- package/dist/renderers/renderBlock.d.ts +6 -0
- package/dist/renderers/renderBlock.js +47 -0
- package/dist/renderers/renderInline.d.ts +3 -0
- package/dist/renderers/renderInline.js +18 -0
- package/dist/renderers/renderMark.d.ts +3 -0
- package/dist/renderers/renderMark.js +23 -0
- package/package.json +52 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# 0.1.0 (2026-07-24)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
- **rich-text:** add markdown serializers, code snippet and table support ([56acd09](https://github.com/Synerise/synerise-design/commit/56acd097e2106a8743e85f98226caedca87a33e3))
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Synerise
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: rich-text-renderer
|
|
3
|
+
title: RichTextRenderer
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
RichTextRenderer UI Component — renders a `RichTextDocument` to React without any editor dependency. Weighs ~1 KB gzip.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
npm i @synerise/ds-rich-text-renderer
|
|
12
|
+
or
|
|
13
|
+
yarn add @synerise/ds-rich-text-renderer
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
### Basic
|
|
19
|
+
|
|
20
|
+
```tsx
|
|
21
|
+
import RichTextRenderer from '@synerise/ds-rich-text-renderer';
|
|
22
|
+
|
|
23
|
+
<RichTextRenderer document={article.bodyJson} />
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Custom block rendering
|
|
27
|
+
|
|
28
|
+
Use `blockOverrides` to replace how specific block types are rendered — for example, replacing `<img>` with a Next.js `<Image>` component:
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
import RichTextRenderer from '@synerise/ds-rich-text-renderer';
|
|
32
|
+
import Image from 'next/image';
|
|
33
|
+
|
|
34
|
+
const CustomImage = ({ block }) => (
|
|
35
|
+
<Image
|
|
36
|
+
src={block.attrs.src}
|
|
37
|
+
alt={block.attrs.alt ?? ''}
|
|
38
|
+
width={800}
|
|
39
|
+
height={400}
|
|
40
|
+
loading="lazy"
|
|
41
|
+
/>
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
<RichTextRenderer
|
|
45
|
+
document={article.bodyJson}
|
|
46
|
+
blockOverrides={{ image: CustomImage }}
|
|
47
|
+
/>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Using individual render functions
|
|
51
|
+
|
|
52
|
+
For advanced use cases, import the building blocks directly:
|
|
53
|
+
|
|
54
|
+
```tsx
|
|
55
|
+
import { renderBlock, renderInline, renderMark } from '@synerise/ds-rich-text-renderer';
|
|
56
|
+
|
|
57
|
+
// Render a single block
|
|
58
|
+
const element = renderBlock(block, 0);
|
|
59
|
+
|
|
60
|
+
// Render inline content
|
|
61
|
+
const inline = renderInline(textNode, 0);
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## API
|
|
65
|
+
|
|
66
|
+
| Property | Description | Type | Default |
|
|
67
|
+
| --- | --- | --- | --- |
|
|
68
|
+
| document | The `RichTextDocument` to render | `RichTextDocument` | **required** |
|
|
69
|
+
| className | CSS class on the wrapper element | `string` | - |
|
|
70
|
+
| style | Inline styles on the wrapper element | `CSSProperties` | - |
|
|
71
|
+
| blockOverrides | Override rendering per block type | `Partial<Record<Block['type'], ComponentType<BlockOverrideProps>>>` | - |
|
|
72
|
+
|
|
73
|
+
### BlockOverrideProps
|
|
74
|
+
|
|
75
|
+
Props passed to custom block renderers:
|
|
76
|
+
|
|
77
|
+
| Property | Description | Type |
|
|
78
|
+
| --- | --- | --- |
|
|
79
|
+
| block | The block node to render | `Block` |
|
|
80
|
+
|
|
81
|
+
### Supported block types
|
|
82
|
+
|
|
83
|
+
| Block type | Renders as |
|
|
84
|
+
| --- | --- |
|
|
85
|
+
| `paragraph` | `<p>` |
|
|
86
|
+
| `heading` | `<h1>` – `<h6>` |
|
|
87
|
+
| `bulletList` | `<ul>` |
|
|
88
|
+
| `orderedList` | `<ol>` |
|
|
89
|
+
| `codeBlock` | `<pre><code>` |
|
|
90
|
+
| `image` | `<img>` |
|
|
91
|
+
| `blockquote` | `<blockquote>` |
|
|
92
|
+
| `horizontalRule` | `<hr>` |
|
|
93
|
+
|
|
94
|
+
### Supported mark types
|
|
95
|
+
|
|
96
|
+
| Mark type | Renders as |
|
|
97
|
+
| --- | --- |
|
|
98
|
+
| `bold` | `<strong>` |
|
|
99
|
+
| `italic` | `<em>` |
|
|
100
|
+
| `underline` | `<u>` |
|
|
101
|
+
| `strike` | `<s>` |
|
|
102
|
+
| `code` | `<code>` |
|
|
103
|
+
| `link` | `<a>` |
|
|
104
|
+
|
|
105
|
+
## Styling
|
|
106
|
+
|
|
107
|
+
The renderer applies the same typography styles as the `@synerise/ds-rich-text` editor (headings, paragraphs, lists, links, code blocks, images, blockquotes) using design system tokens via `styled-components`. This ensures visual parity between editing and read-only rendering.
|
|
108
|
+
|
|
109
|
+
## Related
|
|
110
|
+
|
|
111
|
+
- [`@synerise/ds-rich-text`](../rich-text/README.md) — the WYSIWYG editor component
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
import { RichTextRendererProps } from './RichTextRenderer.types';
|
|
3
|
+
declare const RichTextRenderer: {
|
|
4
|
+
({ document, className, style, blockOverrides, }: RichTextRendererProps): React.JSX.Element;
|
|
5
|
+
displayName: string;
|
|
6
|
+
};
|
|
7
|
+
export default RichTextRenderer;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { RendererWrapper } from "./RichTextRenderer.styles.js";
|
|
3
|
+
import { renderBlock } from "./renderers/renderBlock.js";
|
|
4
|
+
import "react";
|
|
5
|
+
import "@synerise/ds-rich-text";
|
|
6
|
+
const RichTextRenderer = ({
|
|
7
|
+
document,
|
|
8
|
+
className,
|
|
9
|
+
style,
|
|
10
|
+
blockOverrides
|
|
11
|
+
}) => /* @__PURE__ */ jsx(RendererWrapper, { className, style, "data-testid": "rich-text-renderer", children: document.children.map((block, i) => renderBlock(block, i, blockOverrides)) });
|
|
12
|
+
RichTextRenderer.displayName = "RichTextRenderer";
|
|
13
|
+
export {
|
|
14
|
+
RichTextRenderer as default
|
|
15
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import styled from "styled-components";
|
|
2
|
+
const RendererWrapper = /* @__PURE__ */ styled.div.withConfig({
|
|
3
|
+
displayName: "RichTextRendererstyles__RendererWrapper",
|
|
4
|
+
componentId: "sc-42i2bw-0"
|
|
5
|
+
})(["font-size:13px;line-height:1.6;color:", ";> * + *{margin-top:0.5em;}h1{font-size:24px;font-weight:500;line-height:1.3;margin:0 0 0.5em;}h2{font-size:20px;font-weight:500;line-height:1.3;margin:0 0 0.5em;}h3{font-size:16px;font-weight:500;line-height:1.3;margin:0 0 0.5em;}p{margin:0 0 4px;}ul,ol{padding-left:24px;margin:0 0 4px;}li{margin:2px 0;}a{color:", ";text-decoration:underline;cursor:pointer;}code{background:", ";padding:2px 4px;border-radius:3px;font-family:'SFMono-Regular',Consolas,'Liberation Mono',Menlo,monospace;font-size:12px;}pre{background:", ";padding:12px;border-radius:3px;overflow-x:auto;code{background:none;padding:0;border-radius:0;font-size:12px;}}pre[data-type='code-snippet']{display:flex;align-items:center;background:", ";padding:6px 12px;margin:0 0 4px;code{white-space:pre;overflow-x:auto;}}blockquote{border-left:3px solid ", ";padding-left:12px;margin:0 0 4px;color:", ";}table{border-collapse:collapse;width:100%;margin:0 0 4px;table-layout:fixed;td,th{border:1px solid ", ";padding:6px 10px;vertical-align:top;text-align:left;> *{margin:0;}}th{background:", ";font-weight:500;}}s{text-decoration:line-through;}img{max-width:100%;height:auto;border-radius:3px;display:block;margin:8px 0;}"], (props) => props.theme.palette["grey-800"], (props) => props.theme.palette["blue-600"], (props) => props.theme.palette["grey-100"], (props) => props.theme.palette["grey-100"], (props) => props.theme.palette["grey-050"], (props) => props.theme.palette["grey-300"], (props) => props.theme.palette["grey-500"], (props) => props.theme.palette["grey-300"], (props) => props.theme.palette["grey-050"]);
|
|
6
|
+
export {
|
|
7
|
+
RendererWrapper
|
|
8
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CSSProperties } from 'react';
|
|
2
|
+
import { Block, RichTextDocument } from '@synerise/ds-rich-text';
|
|
3
|
+
export type BlockOverrideProps<T extends Block = Block> = {
|
|
4
|
+
block: T;
|
|
5
|
+
};
|
|
6
|
+
export type RichTextRendererProps = {
|
|
7
|
+
document: RichTextDocument;
|
|
8
|
+
className?: string;
|
|
9
|
+
style?: CSSProperties;
|
|
10
|
+
blockOverrides?: Partial<Record<Block['type'], React.ComponentType<BlockOverrideProps>>>;
|
|
11
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { default as default2 } from "./RichTextRenderer.js";
|
|
2
|
+
import { renderBlock } from "./renderers/renderBlock.js";
|
|
3
|
+
import { renderInline } from "./renderers/renderInline.js";
|
|
4
|
+
import { renderMark } from "./renderers/renderMark.js";
|
|
5
|
+
export {
|
|
6
|
+
default2 as default,
|
|
7
|
+
renderBlock,
|
|
8
|
+
renderInline,
|
|
9
|
+
renderMark
|
|
10
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { default as React, ReactNode } from 'react';
|
|
2
|
+
import { Block } from '@synerise/ds-rich-text';
|
|
3
|
+
import { BlockOverrideProps } from '../RichTextRenderer.types';
|
|
4
|
+
type Overrides = Partial<Record<Block['type'], React.ComponentType<BlockOverrideProps>>>;
|
|
5
|
+
export declare const renderBlock: (block: Block, index: number, overrides?: Overrides) => ReactNode;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { renderInline } from "./renderInline.js";
|
|
3
|
+
const renderBlock = (block, index, overrides) => {
|
|
4
|
+
const Override = overrides?.[block.type];
|
|
5
|
+
if (Override) {
|
|
6
|
+
return /* @__PURE__ */ jsx(Override, { block }, index);
|
|
7
|
+
}
|
|
8
|
+
switch (block.type) {
|
|
9
|
+
case "paragraph": {
|
|
10
|
+
const style = block.attrs?.textAlign ? {
|
|
11
|
+
textAlign: block.attrs.textAlign
|
|
12
|
+
} : void 0;
|
|
13
|
+
return /* @__PURE__ */ jsx("p", { style, children: block.children.map(renderInline) }, index);
|
|
14
|
+
}
|
|
15
|
+
case "heading": {
|
|
16
|
+
const Tag = `h${block.attrs.level}`;
|
|
17
|
+
const style = block.attrs.textAlign ? {
|
|
18
|
+
textAlign: block.attrs.textAlign
|
|
19
|
+
} : void 0;
|
|
20
|
+
return /* @__PURE__ */ jsx(Tag, { style, children: block.children.map(renderInline) }, index);
|
|
21
|
+
}
|
|
22
|
+
case "codeBlock":
|
|
23
|
+
return /* @__PURE__ */ jsx("pre", { children: /* @__PURE__ */ jsx("code", { children: block.children.map(renderInline) }) }, index);
|
|
24
|
+
case "codeSnippet":
|
|
25
|
+
return /* @__PURE__ */ jsx("pre", { "data-type": "code-snippet", children: /* @__PURE__ */ jsx("code", { children: block.children.map(renderInline) }) }, index);
|
|
26
|
+
case "image":
|
|
27
|
+
return /* @__PURE__ */ jsx("img", { src: block.attrs.src, alt: block.attrs.alt ?? "", title: block.attrs.title ?? void 0 }, index);
|
|
28
|
+
case "bulletList":
|
|
29
|
+
return /* @__PURE__ */ jsx("ul", { children: block.children.map((li, i) => /* @__PURE__ */ jsx("li", { children: li.children.map((child, j) => renderBlock(child, j, overrides)) }, i)) }, index);
|
|
30
|
+
case "orderedList":
|
|
31
|
+
return /* @__PURE__ */ jsx("ol", { start: block.attrs?.start, children: block.children.map((li, i) => /* @__PURE__ */ jsx("li", { children: li.children.map((child, j) => renderBlock(child, j, overrides)) }, i)) }, index);
|
|
32
|
+
case "blockquote":
|
|
33
|
+
return /* @__PURE__ */ jsx("blockquote", { children: block.children.map((child, i) => renderBlock(child, i, overrides)) }, index);
|
|
34
|
+
case "horizontalRule":
|
|
35
|
+
return /* @__PURE__ */ jsx("hr", {}, index);
|
|
36
|
+
case "table":
|
|
37
|
+
return /* @__PURE__ */ jsx("table", { children: /* @__PURE__ */ jsx("tbody", { children: block.children.map((row, r) => /* @__PURE__ */ jsx("tr", { children: row.children.map((cell, c) => {
|
|
38
|
+
const CellTag = cell.type === "tableHeader" ? "th" : "td";
|
|
39
|
+
return /* @__PURE__ */ jsx(CellTag, { colSpan: cell.attrs?.colspan, rowSpan: cell.attrs?.rowspan, children: cell.children.map((child, j) => renderBlock(child, j, overrides)) }, c);
|
|
40
|
+
}) }, r)) }) }, index);
|
|
41
|
+
default:
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
export {
|
|
46
|
+
renderBlock
|
|
47
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { renderMark } from "./renderMark.js";
|
|
4
|
+
const renderInline = (node, index) => {
|
|
5
|
+
if (node.type === "hardBreak") {
|
|
6
|
+
return /* @__PURE__ */ jsx("br", {}, index);
|
|
7
|
+
}
|
|
8
|
+
let content = node.text;
|
|
9
|
+
if (node.marks) {
|
|
10
|
+
for (let i = 0; i < node.marks.length; i++) {
|
|
11
|
+
content = renderMark(node.marks[i], content, i);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return /* @__PURE__ */ jsx(React.Fragment, { children: content }, index);
|
|
15
|
+
};
|
|
16
|
+
export {
|
|
17
|
+
renderInline
|
|
18
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { sanitizeUrl } from "@synerise/ds-rich-text";
|
|
3
|
+
const renderMark = (mark, children, key) => {
|
|
4
|
+
switch (mark.type) {
|
|
5
|
+
case "bold":
|
|
6
|
+
return /* @__PURE__ */ jsx("strong", { children }, key);
|
|
7
|
+
case "italic":
|
|
8
|
+
return /* @__PURE__ */ jsx("em", { children }, key);
|
|
9
|
+
case "underline":
|
|
10
|
+
return /* @__PURE__ */ jsx("u", { children }, key);
|
|
11
|
+
case "strike":
|
|
12
|
+
return /* @__PURE__ */ jsx("s", { children }, key);
|
|
13
|
+
case "code":
|
|
14
|
+
return /* @__PURE__ */ jsx("code", { children }, key);
|
|
15
|
+
case "link": {
|
|
16
|
+
const safeHref = sanitizeUrl(mark.href);
|
|
17
|
+
return /* @__PURE__ */ jsx("a", { href: safeHref || void 0, target: mark.target || "_blank", rel: mark.rel || "noopener noreferrer", children }, key);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
export {
|
|
22
|
+
renderMark
|
|
23
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@synerise/ds-rich-text-renderer",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "RichTextRenderer UI Component for the Synerise Design System",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"repository": "Synerise/synerise-design",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"/dist",
|
|
10
|
+
"CHANGELOG.md",
|
|
11
|
+
"README.md",
|
|
12
|
+
"package.json",
|
|
13
|
+
"LICENSE.md"
|
|
14
|
+
],
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "vite build",
|
|
20
|
+
"build:watch": "vite build --watch",
|
|
21
|
+
"prepublish": "pnpm run build",
|
|
22
|
+
"types": "tsc --noEmit",
|
|
23
|
+
"pack:ci": "pnpm pack --pack-destination ../../storybook/storybook-static/static",
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"test:watch": "vitest",
|
|
26
|
+
"check:circular-dependencies": "madge --circular --extensions ts,tsx,js,jsx --ts-config tsconfig.json src/ --exclude '/dist/'",
|
|
27
|
+
"upgrade:ds": "ncu -f \"@synerise/ds-*\" -u"
|
|
28
|
+
},
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"types": "dist/index.d.ts",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"import": "./dist/index.js",
|
|
35
|
+
"default": "./dist/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./dist/*.js": "./dist/*.js",
|
|
38
|
+
"./dist/*": "./dist/*.js"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@synerise/ds-rich-text": "^0.1.0"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@synerise/ds-core": "*",
|
|
45
|
+
"react": ">=16.9.0 <= 18.3.1",
|
|
46
|
+
"styled-components": "^5.3.3"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"vitest": "4"
|
|
50
|
+
},
|
|
51
|
+
"gitHead": "6fcfd8345a1522b7e4ccd34337d5d25d1ca4e2ce"
|
|
52
|
+
}
|