-
-
Notifications
You must be signed in to change notification settings - Fork 553
/
index.tsx
232 lines (213 loc) Β· 6.73 KB
/
index.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import {
appendCellToNotebook,
createCodeCell,
emptyNotebook,
fromJS,
ImmutableCodeCell,
ImmutableNotebook
} from "@nteract/commutable";
import {
DisplayData,
ExecuteResult,
KernelOutputError,
Media,
Output,
StreamText
} from "@nteract/outputs";
import {
Cell,
Cells,
Input,
Outputs,
Prompt,
Source,
themes
} from "@nteract/presentational-components";
import { OutputType } from "@nteract/records";
import * as React from "react";
import { BlockMath, InlineMath } from "react-katex";
import ReactMarkdown from "react-markdown";
import katex from "rehype-katex";
import stringify from "rehype-stringify";
import math from "remark-math";
import remark2rehype from "remark-rehype";
import styled, { createGlobalStyle } from "styled-components";
interface Props {
displayOrder: string[];
notebook: ImmutableNotebook;
transforms: object;
theme: "light" | "dark";
}
interface State {
notebook: ImmutableNotebook;
}
const ContentMargin = styled.div`
padding-left: calc(var(--prompt-width, 50px) + 10px);
padding-top: 10px;
padding-bottom: 10px;
padding-right: 10px;
`;
const RawCell = styled.pre`
background: repeating-linear-gradient(
-45deg,
transparent,
transparent 10px,
#efefef 10px,
#f1f1f1 20px
);
`;
const Themes = {
dark: createGlobalStyle`
:root {
${themes.dark}
}`,
light: createGlobalStyle`
:root {
${themes.light}
}`
};
export default class NotebookRender extends React.PureComponent<Props, State> {
static defaultProps = {
notebook: appendCellToNotebook(
emptyNotebook,
createCodeCell().set("source", "# where's the content?")
),
theme: "light"
};
constructor(props: Props) {
super(props);
this.state = {
notebook: fromJS(props.notebook)
};
}
componentWillReceiveProps(nextProps: Props) {
if (nextProps.notebook !== this.props.notebook) {
this.setState({ notebook: fromJS(nextProps.notebook) });
}
}
render() {
const notebook = this.state.notebook;
// Propagated from the hide_(all)_input nbextension
const allSourceHidden = notebook.getIn(["metadata", "hide_input"]) || false;
const language =
notebook.getIn([
"metadata",
"language_info",
"codemirror_mode",
"name"
]) ||
notebook.getIn(["metadata", "language_info", "codemirror_mode"]) ||
notebook.getIn(["metadata", "language_info", "name"]) ||
"text";
const cellOrder = notebook.get("cellOrder");
const cellMap = notebook.get("cellMap");
return (
<div className="notebook-render">
<Cells>
{cellOrder.map((cellId: string) => {
const cell = cellMap.get(cellId);
const cellType: string = cell!.get("cell_type");
const source = cell!.get("source");
switch (cellType) {
case "code":
const sourceHidden =
allSourceHidden ||
cell!.getIn(["metadata", "inputHidden"]) ||
cell!.getIn(["metadata", "hide_input"]);
const outputHidden =
(cell as ImmutableCodeCell).get("outputs").size === 0 ||
cell!.getIn(["metadata", "outputHidden"]);
return (
<Cell key={cellId}>
<Input hidden={sourceHidden}>
<Prompt
counter={(cell as ImmutableCodeCell).get(
"execution_count"
)}
/>
<Source language={language} theme={this.props.theme}>
{source}
</Source>
</Input>
<Outputs
hidden={outputHidden}
expanded={
cell!.getIn(["metadata", "outputExpanded"]) || true
}
>
{cell!
.get("outputs")
.toJS()
.map((output: OutputType, index: number) => (
<Output output={output} key={index}>
<DisplayData>
<Media.HTML />
<Media.Image />
<Media.Json />
<Media.JavaScript />
<Media.LaTeX />
<Media.Markdown />
<Media.Plain />
<Media.SVG />
</DisplayData>
<ExecuteResult>
<Media.HTML />
<Media.Image />
<Media.Json />
<Media.JavaScript />
<Media.LaTeX />
<Media.Markdown />
<Media.Plain />
<Media.SVG />
</ExecuteResult>
<KernelOutputError />
<StreamText />
</Output>
))}
</Outputs>
</Cell>
);
case "markdown":
const remarkPlugins = [math, remark2rehype, katex, stringify];
const remarkRenderers = {
math: function blockMath(node: { value: string }) {
return <BlockMath>{node.value}</BlockMath>;
},
inlineMath: function inlineMath(node: { value: string }) {
return <InlineMath>{node.value}</InlineMath>;
}
} as any;
return (
<Cell key={cellId}>
<ContentMargin>
<ReactMarkdown
escapeHtml={false}
source={source}
plugins={remarkPlugins}
renderers={remarkRenderers}
/>
</ContentMargin>
</Cell>
);
case "raw":
return (
<Cell key={cellId}>
<RawCell>{source}</RawCell>
</Cell>
);
default:
return (
<Cell key={cellId}>
<Outputs>
<pre>{`Cell Type "${cellType}" is not implemented`}</pre>
</Outputs>
</Cell>
);
}
})}
</Cells>
{this.props.theme === "dark" ? <Themes.dark /> : <Themes.light />}
</div>
);
}
}