-
Notifications
You must be signed in to change notification settings - Fork 72
/
renderTokens.tsx
188 lines (183 loc) · 5.78 KB
/
renderTokens.tsx
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { uniqueId } from "lodash";
import { Link } from "react-router-dom";
const tableClasses = {
table: "marked-table",
row: "marked-table-row",
cell: "marked-table-cell",
header: "marked-table-header",
};
export type Token = {
type:
| "text"
| "escape"
| "strong"
| "em"
| "del"
| "hr"
| "blockquote"
| "paragraph"
| "link"
| "list"
| "list_item"
| "html"
| "table"
| "codespan"
| "code"
| "space";
raw?: string;
text?: string;
tokens?: Token[];
href?: string;
items?: Token[];
header?: Token[];
rows?: Token[][];
};
export type Option = {
searchEndpoint: string;
};
// Custom renderer into React components
export function renderTokens(tokens: Token[], options?: Option): any {
const renderers = {
text(token: Token) {
return token.tokens
? renderTokens(token.tokens, options)
: token.text ?? "";
},
escape(token: Token) {
return token.text ?? "";
},
strong(token: Token) {
return (
<strong key={uniqueId()}>
{renderTokens(token.tokens ?? [], options)}
</strong>
);
},
em(token: Token) {
return (
<em key={uniqueId()}>
{renderTokens(token.tokens ?? [], options)}
</em>
);
},
del(token: Token) {
return (
<del key={uniqueId()}>
{renderTokens(token.tokens ?? [], options)}
</del>
);
},
hr() {
return <hr key={uniqueId()} />;
},
blockquote(token: Token) {
return (
<blockquote key={uniqueId()} className="blockquote">
{renderTokens(token.tokens ?? [], options)}
</blockquote>
);
},
paragraph(token: Token) {
return (
<p key={uniqueId()} style={{ margin: "0" }}>
{renderTokens(token.tokens ?? [], options)}
</p>
);
},
link(token: Token) {
if (token.href && token.href.startsWith("search#")) {
const query = token.href.slice("search#".length);
const search =
"?" +
new URLSearchParams({
q: decodeURIComponent(query),
}).toString();
return (
<Link
key={uniqueId()}
to={{
pathname: options?.searchEndpoint,
search,
}}
>
{renderTokens(token.tokens ?? [], options)}
</Link>
);
}
return (
<a key={uniqueId()} href={token.href}>
{renderTokens(token.tokens ?? [], options)}
</a>
);
},
list(token: Token) {
return (
<ul key={uniqueId()} style={{ margin: "0" }}>
{token.items?.map((item: Token) =>
renderTokens([item], options)
)}
</ul>
);
},
list_item(token: Token) {
return (
<li key={uniqueId()}>
{renderTokens(token.tokens ?? [], options)}
</li>
);
},
html(token: Token) {
return token.text;
},
table(token: Token) {
return (
<div className="table-responsive" key={uniqueId()}>
<div
className={`${tableClasses.table} table table-striped table-bordered table-hover`}
>
<div
className={`${tableClasses.header} ${tableClasses.row}`}
>
{token.header?.map((head: Token, index: number) => (
<div className={tableClasses.cell} key={index}>
{renderTokens(head.tokens ?? [], options)}
</div>
))}
</div>
{token.rows?.map((row: Token[], rowsIndex: number) => (
<div key={rowsIndex} className={tableClasses.row}>
{row.map((cell: Token, cellIndex: number) => (
<div
key={cellIndex}
className={tableClasses.cell}
>
{renderTokens(
cell.tokens ?? [],
options
)}
</div>
))}
</div>
))}
</div>
</div>
);
},
codespan(token: Token) {
return <code key={uniqueId()}>{token.text}</code>;
},
code(token: Token) {
return <pre key={uniqueId()}>{token.text}</pre>;
},
space() {
return [];
},
};
return tokens.map((token: Token) => {
const renderer = renderers[token.type];
if (!renderer) {
return [<i>{`(No renderer for ${token.type})`}</i>];
}
return renderer(token);
});
}