-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRichTextRenderer.tsx
More file actions
148 lines (120 loc) · 3.67 KB
/
RichTextRenderer.tsx
File metadata and controls
148 lines (120 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use client';
import React from 'react';
import 'react-quill-new/dist/quill.snow.css';
interface RichTextRendererProps {
content: string;
className?: string;
}
const RichTextRenderer: React.FC<RichTextRendererProps> = ({
content,
className = '',
}) => {
const rawContent = content || '';
// Normalize non-breaking spaces only when we detect overflow-prone content
// (e.g. very long runs of nbsp that prevent wrapping).
const hasOverflowRiskNbsp =
/(?: |\u00A0){6,}/.test(rawContent) ||
/(?:\w(?: |\u00A0)){12,}\w/i.test(rawContent);
const normalizedContent = hasOverflowRiskNbsp
? rawContent.replace(/ /g, ' ').replace(/\u00A0/g, ' ')
: rawContent;
return (
<div className={`rich-text-content ${className}`}>
<div
className="ql-editor"
dangerouslySetInnerHTML={{ __html: normalizedContent }}
/>
<style jsx global>{`
.rich-text-content .ql-editor {
max-width: 100%;
overflow-wrap: anywhere;
word-break: break-word;
padding: 0;
font-size: 14px;
line-height: 1.6;
}
.rich-text-content .ql-editor p {
margin-bottom: 1em;
}
.rich-text-content .ql-editor h1 {
font-size: 2em;
font-weight: bold;
margin-bottom: 0.5em;
}
.rich-text-content .ql-editor h2 {
font-size: 1.5em;
font-weight: bold;
margin-bottom: 0.5em;
}
.rich-text-content .ql-editor h3 {
font-size: 1.25em;
font-weight: bold;
margin-bottom: 0.5em;
}
.rich-text-content .ql-editor ul,
.rich-text-content .ql-editor ol {
list-style-position: outside;
padding-left: 1.5em;
margin-bottom: 1em;
}
.rich-text-content .ql-editor ul {
list-style-type: disc !important;
}
.rich-text-content .ql-editor ul ul {
list-style-type: circle !important;
}
.rich-text-content .ql-editor ol {
list-style-type: decimal !important;
}
.rich-text-content .ql-editor li {
margin-bottom: 0.25em;
}
/*
* Keep Quill-managed lists (li[data-list]) untouched.
* Only normalize plain HTML lists from backend content.
*/
.rich-text-content .ql-editor ul > li:not([data-list]),
.rich-text-content .ql-editor ol > li:not([data-list]) {
display: list-item !important;
list-style-type: inherit !important;
padding-left: 0 !important;
}
.rich-text-content .ql-editor li:not([data-list])::before {
content: none !important;
}
.rich-text-content .ql-editor a {
color: #3b82f6;
text-decoration: underline;
}
.rich-text-content .ql-editor a:hover {
color: #2563eb;
}
.rich-text-content.text-white .ql-editor a {
color: #60a5fa;
}
.rich-text-content.text-white .ql-editor a:hover {
color: #93c5fd;
}
.rich-text-content .ql-editor img {
max-width: 100%;
height: auto;
margin: 1em 0;
}
.rich-text-content .ql-editor strong,
.rich-text-content .ql-editor b {
font-weight: 700 !important;
}
.rich-text-content .ql-editor em {
font-style: italic;
}
.rich-text-content .ql-editor u {
text-decoration: underline;
}
.rich-text-content .ql-editor s {
text-decoration: line-through;
}
`}</style>
</div>
);
};
export default RichTextRenderer;