1
1
import { CSSProperties } from "react" ;
2
2
import { StylesType , initRendererProps } from "./types" ;
3
- import { RendererObject } from "marked" ;
3
+ import { Renderer } from "marked" ;
4
4
import { styles } from "./styles" ;
5
5
6
6
function escapeQuotes ( value : unknown ) {
@@ -80,100 +80,99 @@ export function parseCssInJsToInlineCss(
80
80
81
81
export const initRenderer = ( {
82
82
customStyles,
83
- } : initRendererProps ) : RendererObject => {
83
+ } : initRendererProps ) : Renderer => {
84
84
const finalStyles = { ...styles , ...customStyles } ;
85
85
86
- const customRenderer : RendererObject = {
87
- blockquote ( quote ) {
88
- return `<blockquote${
89
- parseCssInJsToInlineCss ( finalStyles . blockQuote ) !== ""
90
- ? ` style="${ parseCssInJsToInlineCss ( finalStyles . blockQuote ) } "`
91
- : ""
92
- } >\n${ quote } </blockquote>\n`;
93
- } ,
86
+ const customRenderer = new Renderer ( ) ;
94
87
95
- br ( ) {
96
- return `<br ${
97
- parseCssInJsToInlineCss ( finalStyles . br ) !== ""
98
- ? ` style="${ parseCssInJsToInlineCss ( finalStyles . br ) } "`
99
- : ""
100
- } /> `;
101
- } ,
88
+ customRenderer . blockquote = ( quote ) => {
89
+ return `<blockquote ${
90
+ parseCssInJsToInlineCss ( finalStyles . blockQuote ) !== ""
91
+ ? ` style="${ parseCssInJsToInlineCss ( finalStyles . blockQuote ) } "`
92
+ : ""
93
+ } >\n ${ quote } </blockquote>\n `;
94
+ }
102
95
103
- code ( code ) {
104
- code = code . replace ( / \n $ / , "" ) + "\n" ;
96
+ customRenderer . br = ( ) => {
97
+ return `<br${
98
+ parseCssInJsToInlineCss ( finalStyles . br ) !== ""
99
+ ? ` style="${ parseCssInJsToInlineCss ( finalStyles . br ) } "`
100
+ : ""
101
+ } />`;
102
+ }
105
103
106
- return `<pre${
107
- parseCssInJsToInlineCss ( finalStyles . codeBlock ) !== ""
108
- ? ` style="${ parseCssInJsToInlineCss ( finalStyles . codeBlock ) } "`
109
- : ""
110
- } ><code>${ code } </code></pre>\n`;
111
- } ,
104
+ customRenderer . code = ( code ) => {
105
+ code = code . replace ( / \n $ / , "" ) + "\n" ;
112
106
113
- codespan ( text ) {
114
- return `<code${
115
- parseCssInJsToInlineCss ( finalStyles . codeInline ) !== ""
116
- ? ` style="${ parseCssInJsToInlineCss ( finalStyles . codeInline ) } "`
117
- : ""
118
- } >${ text } </code>`;
119
- } ,
107
+ return `<pre${
108
+ parseCssInJsToInlineCss ( finalStyles . codeBlock ) !== ""
109
+ ? ` style="${ parseCssInJsToInlineCss ( finalStyles . codeBlock ) } "`
110
+ : ""
111
+ } ><code>${ code } </code></pre>\n`;
112
+ }
120
113
121
- del ( text ) {
122
- return `<del ${
123
- parseCssInJsToInlineCss ( finalStyles . strikethrough ) !== ""
124
- ? ` style="${ parseCssInJsToInlineCss ( finalStyles . strikethrough ) } "`
125
- : ""
126
- } >${ text } </del >`;
127
- } ,
114
+ customRenderer . codespan = ( text ) => {
115
+ return `<code ${
116
+ parseCssInJsToInlineCss ( finalStyles . codeInline ) !== ""
117
+ ? ` style="${ parseCssInJsToInlineCss ( finalStyles . codeInline ) } "`
118
+ : ""
119
+ } >${ text } </code >`;
120
+ }
128
121
129
- em ( text ) {
130
- return `<em ${
131
- parseCssInJsToInlineCss ( finalStyles . italic ) !== ""
132
- ? ` style="${ parseCssInJsToInlineCss ( finalStyles . italic ) } "`
133
- : ""
134
- } >${ text } </em >`;
135
- } ,
122
+ customRenderer . del = ( text ) => {
123
+ return `<del ${
124
+ parseCssInJsToInlineCss ( finalStyles . strikethrough ) !== ""
125
+ ? ` style="${ parseCssInJsToInlineCss ( finalStyles . strikethrough ) } "`
126
+ : ""
127
+ } >${ text } </del >`;
128
+ }
136
129
137
- heading ( text , level ) {
138
- return `<h${ level } ${
139
- parseCssInJsToInlineCss (
140
- finalStyles [ `h${ level } ` as keyof StylesType ]
141
- ) !== ""
142
- ? ` style="${ parseCssInJsToInlineCss (
143
- finalStyles [ `h${ level } ` as keyof StylesType ]
144
- ) } "`
145
- : ""
146
- } >${ text } </h${ level } >`;
147
- } ,
130
+ customRenderer . em = ( text ) => {
131
+ return `<em${
132
+ parseCssInJsToInlineCss ( finalStyles . italic ) !== ""
133
+ ? ` style="${ parseCssInJsToInlineCss ( finalStyles . italic ) } "`
134
+ : ""
135
+ } >${ text } </em>`;
136
+ }
148
137
149
- hr ( ) {
150
- return `<hr${
151
- parseCssInJsToInlineCss ( finalStyles . hr ) !== ""
152
- ? ` style="${ parseCssInJsToInlineCss ( finalStyles . hr ) } "`
153
- : ""
154
- } />\n`;
155
- } ,
138
+ customRenderer . heading = ( text , level ) => {
139
+ return `<h${ level } ${
140
+ parseCssInJsToInlineCss (
141
+ finalStyles [ `h${ level } ` as keyof StylesType ]
142
+ ) !== ""
143
+ ? ` style="${ parseCssInJsToInlineCss (
144
+ finalStyles [ `h${ level } ` as keyof StylesType ]
145
+ ) } "`
146
+ : ""
147
+ } >${ text } </h${ level } >`;
148
+ }
156
149
157
- image ( href , _ , text ) {
158
- let out = `<img src="${ href } " alt="${ text } "${
159
- parseCssInJsToInlineCss ( finalStyles . image ) !== ""
160
- ? ` style="${ parseCssInJsToInlineCss ( finalStyles . image ) } "`
161
- : ""
162
- } >`;
163
- return out ;
164
- } ,
150
+ customRenderer . hr = ( ) => {
151
+ return `<hr${
152
+ parseCssInJsToInlineCss ( finalStyles . hr ) !== ""
153
+ ? ` style="${ parseCssInJsToInlineCss ( finalStyles . hr ) } "`
154
+ : ""
155
+ } />\n`;
156
+ }
157
+
158
+ customRenderer . image = ( href , _ , text ) => {
159
+ return `<img src="${ href } " alt="${ text } "${
160
+ parseCssInJsToInlineCss ( finalStyles . image ) !== ""
161
+ ? ` style="${ parseCssInJsToInlineCss ( finalStyles . image ) } "`
162
+ : ""
163
+ } >`;
164
+ }
165
165
166
- link ( href , _ , text ) {
167
- let out = `<a href="${ href } " target="_blank"${
166
+ customRenderer . link = ( href , _ , text ) => {
167
+ return `<a href="${ href } " target="_blank"${
168
168
parseCssInJsToInlineCss ( finalStyles . link ) !== ""
169
169
? ` style="${ parseCssInJsToInlineCss ( finalStyles . link ) } "`
170
170
: ""
171
171
} >${ text } </a>`;
172
- return out ;
173
- } ,
172
+ }
174
173
175
- list ( body , ordered , start ) {
176
- const type = ordered ? "ol" : "ul" ;
174
+ customRenderer . list = ( body , ordered , start ) => {
175
+ const type = ordered ? "ol" : "ul" ;
177
176
const startatt = ordered && start !== 1 ? ' start="' + start + '"' : "" ;
178
177
const styles = parseCssInJsToInlineCss (
179
178
finalStyles [ ordered ? "ol" : "ul" ]
@@ -188,34 +187,34 @@ export const initRenderer = ({
188
187
type +
189
188
">\n"
190
189
) ;
191
- } ,
190
+ }
192
191
193
- listitem ( text ) {
194
- return `<li${
195
- parseCssInJsToInlineCss ( finalStyles . li ) !== ""
196
- ? ` style="${ parseCssInJsToInlineCss ( finalStyles . li ) } "`
197
- : ""
198
- } >${ text } </li>\n`;
199
- } ,
192
+ customRenderer . listitem = ( text ) => {
193
+ return `<li${
194
+ parseCssInJsToInlineCss ( finalStyles . li ) !== ""
195
+ ? ` style="${ parseCssInJsToInlineCss ( finalStyles . li ) } "`
196
+ : ""
197
+ } >${ text } </li>\n`;
198
+ }
200
199
201
- paragraph ( text ) {
202
- return `<p${
203
- parseCssInJsToInlineCss ( finalStyles . p ) !== ""
204
- ? ` style="${ parseCssInJsToInlineCss ( finalStyles . p ) } "`
205
- : ""
206
- } >${ text } </p>\n`;
207
- } ,
200
+ customRenderer . paragraph = ( text ) => {
201
+ return `<p${
202
+ parseCssInJsToInlineCss ( finalStyles . p ) !== ""
203
+ ? ` style="${ parseCssInJsToInlineCss ( finalStyles . p ) } "`
204
+ : ""
205
+ } >${ text } </p>\n`;
206
+ }
208
207
209
- strong ( text ) {
210
- return `<strong${
211
- parseCssInJsToInlineCss ( finalStyles . bold ) !== ""
212
- ? ` style="${ parseCssInJsToInlineCss ( finalStyles . bold ) } "`
213
- : ""
214
- } >${ text } </strong>`;
215
- } ,
208
+ customRenderer . strong = ( text ) => {
209
+ return `<strong${
210
+ parseCssInJsToInlineCss ( finalStyles . bold ) !== ""
211
+ ? ` style="${ parseCssInJsToInlineCss ( finalStyles . bold ) } "`
212
+ : ""
213
+ } >${ text } </strong>`;
214
+ }
216
215
217
- table ( header , body ) {
218
- if ( body ) body = `<tbody>${ body } </tbody>` ;
216
+ customRenderer . table = ( header , body ) => {
217
+ if ( body ) body = `<tbody>${ body } </tbody>` ;
219
218
220
219
return `<table${
221
220
parseCssInJsToInlineCss ( finalStyles . table ) !== ""
@@ -226,10 +225,10 @@ export const initRenderer = ({
226
225
? ` style="${ parseCssInJsToInlineCss ( finalStyles . thead ) } "`
227
226
: ""
228
227
} >\n${ header } </thead>\n${ body } </table>\n`;
229
- } ,
228
+ }
230
229
231
- tablecell ( content , flags ) {
232
- const type = flags . header ? "th" : "td" ;
230
+ customRenderer . tablecell = ( content , flags ) => {
231
+ const type = flags . header ? "th" : "td" ;
233
232
const tag = flags . align
234
233
? `<${ type } align="${ flags . align } "${
235
234
parseCssInJsToInlineCss ( finalStyles . td ) !== ""
@@ -242,16 +241,15 @@ export const initRenderer = ({
242
241
: ""
243
242
} >`;
244
243
return tag + content + `</${ type } >\n` ;
245
- } ,
244
+ }
246
245
247
- tablerow ( content ) {
248
- return `<tr${
249
- parseCssInJsToInlineCss ( finalStyles . tr ) !== ""
250
- ? ` style="${ parseCssInJsToInlineCss ( finalStyles . tr ) } "`
251
- : ""
252
- } >\n${ content } </tr>\n`;
253
- } ,
254
- } ;
246
+ customRenderer . tablerow = ( content ) => {
247
+ return `<tr${
248
+ parseCssInJsToInlineCss ( finalStyles . tr ) !== ""
249
+ ? ` style="${ parseCssInJsToInlineCss ( finalStyles . tr ) } "`
250
+ : ""
251
+ } >\n${ content } </tr>\n`;
252
+ }
255
253
256
254
return customRenderer ;
257
255
} ;
0 commit comments