@synerise/ds-rich-text 2.0.11 → 2.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 CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [2.1.0](https://github.com/Synerise/synerise-design/compare/@synerise/ds-rich-text@2.0.11...@synerise/ds-rich-text@2.1.0) (2026-10-28)
7
+
8
+
9
+ ### Features
10
+
11
+ * **rich-text:** support markdown paste ([de3bf47](https://github.com/Synerise/synerise-design/commit/de3bf4795cf5682156090e7365e18d6c58226946))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [2.0.11](https://github.com/Synerise/synerise-design/compare/@synerise/ds-rich-text@2.0.10...@synerise/ds-rich-text@2.0.11) (2026-10-26)
7
18
 
8
19
 
package/README.md CHANGED
@@ -44,6 +44,21 @@ const [doc, setDoc] = useState<RichTextDocument>();
44
44
  />
45
45
  ```
46
46
 
47
+ ### Markdown mode
48
+
49
+ ```tsx
50
+ <RichText
51
+ format="markdown"
52
+ value={markdown}
53
+ onChange={(markdown) => setMarkdown(markdown)}
54
+ />
55
+ ```
56
+
57
+ - **Load:** `value` is read as markdown. Inline HTML the editor writes (`<u>`, `<br>`) is supported.
58
+ - **Paste:** pasted plain text is parsed as markdown, and every pasted line stays a line. HTML tags in it stay text. Rich content copied from documents or another editor is pasted as is.
59
+ - **Save:** line breaks are saved as `<br>`, and `\`, `<` and `&` in text are escaped (`List\<String>`).
60
+ - Only the formats in `toolbarFeatures` are kept. The text of any other block stays as plain paragraphs.
61
+
47
62
  ### With image upload
48
63
 
49
64
  ```tsx
@@ -74,7 +89,7 @@ const [doc, setDoc] = useState<RichTextDocument>();
74
89
 
75
90
  | Property | Description | Type | Default |
76
91
  | --- | --- | --- | --- |
77
- | format | Output format — `'html'` emits string, `'document'` emits `RichTextDocument` | `'html' \| 'document'` | `'html'` |
92
+ | format | Output format — `'html'` emits an HTML string, `'markdown'` a markdown string, `'document'` a `RichTextDocument` | `'html' \| 'markdown' \| 'document'` | `'html'` |
78
93
  | value | Controlled value (accepts both HTML string and `RichTextDocument`) | `string \| RichTextDocument` | - |
79
94
  | defaultValue | Initial value for uncontrolled mode | `string \| RichTextDocument` | - |
80
95
  | onChange | Called when content changes | `(value: string \| RichTextDocument) => void` | - |
package/dist/RichText.js CHANGED
@@ -7,22 +7,28 @@ import Icon, { EditS } from "@synerise/ds-icon";
7
7
  import { useCombinedRefs, useOnClickOutside } from "@synerise/ds-utils";
8
8
  import { documentToMarkdown } from "./blockModel/serializers/documentToMarkdown.js";
9
9
  import { documentToTiptapJson } from "./blockModel/serializers/documentToTiptap.js";
10
- import { markdownToDocument } from "./blockModel/serializers/markdownToDocument.js";
10
+ import "./blockModel/serializers/markdownToDocument.js";
11
11
  import { tiptapJsonToDocument } from "./blockModel/serializers/tiptapToDocument.js";
12
12
  import "@synerise/ds-button";
13
13
  import "@synerise/ds-input";
14
14
  import { RichTextWrapper, ContentAbove, EditorWrapper, ToolbarArea, EditorArea, SubtleSuffix, ContentBelow, ErrorText, Description } from "./RichText.styles.js";
15
15
  import { getExtensions } from "./utils/getExtensions.js";
16
+ import { handleMarkdownPaste } from "./utils/handleMarkdownPaste.js";
17
+ import { markdownToHtml } from "./utils/markdownToHtml.js";
16
18
  import { Toolbar } from "./components/Toolbar.js";
17
19
  import { DEFAULT_TEXTS, ALL_TOOLBAR_FEATURES, DEFAULT_HEADING_LEVELS } from "./RichText.types.js";
18
20
  const SUBTLE_IGNORE_SELECTORS = ["[data-popover-content]"];
21
+ const MARKDOWN_EDITOR_PROPS = {
22
+ handlePaste: handleMarkdownPaste
23
+ };
24
+ const DEFAULT_EDITOR_PROPS = {};
19
25
  const isDocument = (val) => typeof val === "object" && val !== null && val.type === "doc";
20
26
  const toEditorContent = (val, format) => {
21
27
  if (val === void 0) {
22
28
  return "";
23
29
  }
24
30
  if (typeof val === "string") {
25
- return format === "markdown" ? documentToTiptapJson(markdownToDocument(val)) : val;
31
+ return format === "markdown" ? markdownToHtml(val) : val;
26
32
  }
27
33
  return documentToTiptapJson(val);
28
34
  };
@@ -103,6 +109,7 @@ const RichText = forwardRef(({
103
109
  content: toEditorContent(value ?? defaultValue, format),
104
110
  editable: isEditable,
105
111
  autofocus: autoFocus,
112
+ editorProps: format === "markdown" ? MARKDOWN_EDITOR_PROPS : DEFAULT_EDITOR_PROPS,
106
113
  onUpdate: handleUpdate,
107
114
  onFocus: () => setIsFocused(true),
108
115
  onBlur: () => setIsFocused(false)
@@ -147,7 +154,7 @@ const RichText = forwardRef(({
147
154
  }
148
155
  } else if (format === "markdown") {
149
156
  if (lastEmittedRef.current !== value) {
150
- editor.commands.setContent(documentToTiptapJson(markdownToDocument(value)), false);
157
+ editor.commands.setContent(markdownToHtml(value), false);
151
158
  }
152
159
  } else {
153
160
  const currentHTML = editor.getHTML();
@@ -14,11 +14,13 @@ const serializeMark = (mark, inner) => {
14
14
  return `[${inner}](${mark.href})`;
15
15
  }
16
16
  };
17
+ const escapeText = (text) => text.replace(/[\\<&]/g, "\\$&");
17
18
  const serializeInline = (node) => {
18
19
  if (node.type === "hardBreak") {
19
- return " \n";
20
+ return "<br>";
20
21
  }
21
- let text = node.text;
22
+ const isCode = node.marks?.some((mark) => mark.type === "code");
23
+ let text = isCode ? node.text : escapeText(node.text);
22
24
  if (node.marks) {
23
25
  for (const mark of node.marks) {
24
26
  text = serializeMark(mark, text);
@@ -27,15 +29,18 @@ const serializeInline = (node) => {
27
29
  return text;
28
30
  };
29
31
  const serializeInlines = (children) => children.map(serializeInline).join("");
32
+ const serializeCode = (children) => children.map((node) => node.type === "text" ? node.text : "\n").join("");
30
33
  const indentLines = (text, indent) => text.split("\n").map((line) => line ? indent + line : line).join("\n");
31
34
  const serializeListItem = (item, marker, indent) => {
32
35
  const [first, ...rest] = item.children;
33
36
  const firstText = first ? serializeBlock(first) : "";
34
37
  const continuation = indent + " ".repeat(marker.length);
35
38
  let line = `${indent}${marker}${firstText}`;
39
+ let previous = first;
36
40
  for (const child of rest) {
37
- line += `
38
- ${indentLines(serializeBlock(child), continuation)}`;
41
+ const isAfterList = previous?.type === "bulletList" || previous?.type === "orderedList";
42
+ line += `${isAfterList ? "\n\n" : "\n"}${indentLines(serializeBlock(child), continuation)}`;
43
+ previous = child;
39
44
  }
40
45
  return line;
41
46
  };
@@ -47,10 +52,10 @@ const serializeBlock = (block, indent = "") => {
47
52
  return `${"#".repeat(block.attrs.level)} ${serializeInlines(block.children)}`;
48
53
  case "codeBlock":
49
54
  return `\`\`\`
50
- ${serializeInlines(block.children)}
55
+ ${serializeCode(block.children)}
51
56
  \`\`\``;
52
57
  case "codeSnippet":
53
- return `\`${serializeInlines(block.children)}\``;
58
+ return `\`${serializeCode(block.children)}\``;
54
59
  case "image":
55
60
  return `![${block.attrs.alt ?? ""}](${block.attrs.src})`;
56
61
  case "bulletList":
@@ -14,6 +14,8 @@ import "@tiptap/extension-text-align";
14
14
  import "@tiptap/extension-underline";
15
15
  import "@tiptap/starter-kit";
16
16
  import "../extensions/CodeSnippet.js";
17
+ import "../utils/handleMarkdownPaste.js";
18
+ import "../utils/markdownToHtml.js";
17
19
  import { normalizeUrl } from "../utils/normalizeUrl.js";
18
20
  const ImagePopover = ({
19
21
  editor,
@@ -14,6 +14,8 @@ import "@tiptap/extension-text-align";
14
14
  import "@tiptap/extension-underline";
15
15
  import "@tiptap/starter-kit";
16
16
  import "../extensions/CodeSnippet.js";
17
+ import "../utils/handleMarkdownPaste.js";
18
+ import "../utils/markdownToHtml.js";
17
19
  import { normalizeUrl } from "../utils/normalizeUrl.js";
18
20
  const LinkPopover = ({
19
21
  editor,
package/dist/index.js CHANGED
@@ -18,6 +18,8 @@ import "@tiptap/extension-text-align";
18
18
  import "@tiptap/extension-underline";
19
19
  import "@tiptap/starter-kit";
20
20
  import "./extensions/CodeSnippet.js";
21
+ import "./utils/handleMarkdownPaste.js";
22
+ import "./utils/markdownToHtml.js";
21
23
  import { normalizeUrl } from "./utils/normalizeUrl.js";
22
24
  import { sanitizeUrl } from "./utils/sanitizeUrl.js";
23
25
  export {
@@ -0,0 +1,2 @@
1
+ import { EditorView } from '@tiptap/pm/view';
2
+ export declare const handleMarkdownPaste: (view: EditorView, event: ClipboardEvent) => boolean;
@@ -0,0 +1,46 @@
1
+ import { DOMParser, Slice } from "@tiptap/pm/model";
2
+ import MarkdownIt from "markdown-it";
3
+ import { markdownToHtml } from "./markdownToHtml.js";
4
+ const PROSEMIRROR_SLICE_ATTRIBUTE = "data-pm-slice";
5
+ const STRUCTURED_HTML_PATTERN = /<(h[1-6]|ul|ol|table|blockquote|pre|a|strong|b|em|i|u|s|code)[\s>]/i;
6
+ const isInsideCode = ({
7
+ state
8
+ }) => state.selection.$from.parent.type.spec.code || state.selection.$to.parent.type.spec.code;
9
+ const {
10
+ linkify
11
+ } = new MarkdownIt();
12
+ const isUrlOverSelection = ({
13
+ state
14
+ }, text) => {
15
+ const links = linkify.match(text.trim());
16
+ return !state.selection.empty && links?.length === 1 && links[0].raw === text.trim();
17
+ };
18
+ const parseWithSchema = (view, markdown) => {
19
+ const {
20
+ body
21
+ } = new window.DOMParser().parseFromString(markdownToHtml(markdown, {
22
+ allowHtml: false
23
+ }), "text/html");
24
+ const {
25
+ content
26
+ } = DOMParser.fromSchema(view.state.schema).parse(body);
27
+ const openStart = content.firstChild?.type.name === "paragraph" ? 1 : 0;
28
+ const openEnd = content.lastChild?.type.name === "paragraph" ? 1 : 0;
29
+ return new Slice(content, openStart, openEnd);
30
+ };
31
+ const handleMarkdownPaste = (view, event) => {
32
+ const text = event.clipboardData?.getData("text/plain");
33
+ const html = event.clipboardData?.getData("text/html") ?? "";
34
+ if (!text?.trim() || html.includes(PROSEMIRROR_SLICE_ATTRIBUTE) || STRUCTURED_HTML_PATTERN.test(html) || isInsideCode(view) || isUrlOverSelection(view, text)) {
35
+ return false;
36
+ }
37
+ const slice = parseWithSchema(view, text);
38
+ if (!slice.content.size) {
39
+ return false;
40
+ }
41
+ view.dispatch(view.state.tr.replaceSelection(slice).scrollIntoView().setMeta("paste", true));
42
+ return true;
43
+ };
44
+ export {
45
+ handleMarkdownPaste
46
+ };
@@ -1,3 +1,5 @@
1
1
  export { getExtensions } from './getExtensions';
2
+ export { handleMarkdownPaste } from './handleMarkdownPaste';
3
+ export { markdownToHtml } from './markdownToHtml';
2
4
  export { normalizeUrl } from './normalizeUrl';
3
5
  export { sanitizeUrl } from './sanitizeUrl';
@@ -1,8 +1,12 @@
1
1
  import { getExtensions } from "./getExtensions.js";
2
+ import { handleMarkdownPaste } from "./handleMarkdownPaste.js";
3
+ import { markdownToHtml } from "./markdownToHtml.js";
2
4
  import { normalizeUrl } from "./normalizeUrl.js";
3
5
  import { sanitizeUrl } from "./sanitizeUrl.js";
4
6
  export {
5
7
  getExtensions,
8
+ handleMarkdownPaste,
9
+ markdownToHtml,
6
10
  normalizeUrl,
7
11
  sanitizeUrl
8
12
  };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Converts markdown to HTML for the editor to read, both when a value loads and when text is pasted.
3
+ * Pass `allowHtml: false` for pasted text, so tags in it stay text.
4
+ */
5
+ export declare const markdownToHtml: (value: string, { allowHtml }?: {
6
+ allowHtml?: boolean;
7
+ }) => string;
@@ -0,0 +1,17 @@
1
+ import MarkdownIt from "markdown-it";
2
+ const savedValueParser = new MarkdownIt({
3
+ html: true,
4
+ linkify: true,
5
+ breaks: false
6
+ });
7
+ const pastedTextParser = new MarkdownIt({
8
+ html: false,
9
+ linkify: true,
10
+ breaks: true
11
+ });
12
+ const markdownToHtml = (value, {
13
+ allowHtml = true
14
+ } = {}) => (allowHtml ? savedValueParser : pastedTextParser).render(value).replace(/\n<\/code><\/pre>/g, "</code></pre>");
15
+ export {
16
+ markdownToHtml
17
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@synerise/ds-rich-text",
3
- "version": "2.0.11",
3
+ "version": "2.1.0",
4
4
  "description": "RichText UI Component for the Synerise Design System",
5
5
  "license": "ISC",
6
6
  "repository": "Synerise/synerise-design",
@@ -38,14 +38,14 @@
38
38
  "./dist/*": "./dist/*.js"
39
39
  },
40
40
  "dependencies": {
41
- "@synerise/ds-button": "^2.1.5",
42
- "@synerise/ds-copy-icon": "^2.0.10",
43
- "@synerise/ds-form-field": "^2.0.10",
41
+ "@synerise/ds-button": "^2.1.6",
42
+ "@synerise/ds-copy-icon": "^2.0.11",
43
+ "@synerise/ds-form-field": "^2.0.11",
44
44
  "@synerise/ds-icon": "^2.2.3",
45
- "@synerise/ds-input": "^2.0.11",
46
- "@synerise/ds-popover": "^2.0.4",
47
- "@synerise/ds-toolbar": "^2.0.10",
48
- "@synerise/ds-tooltip": "^2.0.10",
45
+ "@synerise/ds-input": "^2.0.12",
46
+ "@synerise/ds-popover": "^2.1.0",
47
+ "@synerise/ds-toolbar": "^2.0.11",
48
+ "@synerise/ds-tooltip": "^2.0.11",
49
49
  "@synerise/ds-utils": "^2.2.2",
50
50
  "@tiptap/core": "^2.11.0",
51
51
  "@tiptap/extension-image": "^2.11.0",
@@ -71,5 +71,5 @@
71
71
  "@types/markdown-it": "14.1.2",
72
72
  "vitest": "4"
73
73
  },
74
- "gitHead": "5ac0f7dcb7c3995b402c86c087d8f32770f80a1d"
74
+ "gitHead": "c356039703d8826bbbdb6dadcb18bb008fea8bc5"
75
75
  }