-
-
Notifications
You must be signed in to change notification settings - Fork 553
/
index.tsx
212 lines (197 loc) Β· 6.63 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
import * as React from "react";
import ReactMarkdown from "react-markdown";
import math from "remark-math";
import remark2rehype from "remark-rehype";
import katex from "rehype-katex";
import stringify from "rehype-stringify";
import { InlineMath, BlockMath } from "react-katex";
import flush from "styled-jsx/server";
// $FlowFixMe
import { Display } from "@nteract/display-area";
import {
displayOrder as defaultDisplayOrder,
transforms as defaultTransforms,
Transforms
} from "@nteract/transforms";
import {
emptyNotebook,
appendCellToNotebook,
fromJS,
createCodeCell,
ImmutableCodeCell,
ImmutableNotebook
} from "@nteract/commutable";
import {
themes,
Cell,
Input,
Prompt,
Source,
Outputs,
Cells
} from "@nteract/presentational-components";
interface Props {
displayOrder: string[];
notebook: ImmutableNotebook;
transforms: object;
theme: "light" | "dark";
}
interface State {
notebook: ImmutableNotebook;
}
export default class NotebookRender extends React.PureComponent<Props, State> {
static defaultProps = {
displayOrder: defaultDisplayOrder,
transforms: defaultTransforms,
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("cellType");
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(
"executionCount"
)}
/>
<Source language={language} theme={this.props.theme}>
{source}
</Source>
</Input>
<Outputs
hidden={outputHidden}
expanded={
cell!.getIn(["metadata", "outputExpanded"]) || true
}
>
<Display
outputs={(cell as ImmutableCodeCell)
.get("outputs")
.toJS()}
transforms={this.props.transforms as Transforms}
displayOrder={this.props.displayOrder}
/>
</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}>
<div className="content-margin">
<ReactMarkdown
escapeHtml={false}
source={source}
plugins={remarkPlugins}
renderers={remarkRenderers}
/>
</div>
<style jsx>{`
.content-margin {
padding-left: calc(var(--prompt-width, 50px) + 10px);
padding-top: 10px;
padding-bottom: 10px;
padding-right: 10px;
}
`}</style>
</Cell>
);
case "raw":
return (
<Cell key={cellId}>
<pre className="raw-cell">
{source}
<style jsx>{`
raw-cell {
background: repeating-linear-gradient(
-45deg,
transparent,
transparent 10px,
#efefef 10px,
#f1f1f1 20px
);
}
`}</style>
</pre>
</Cell>
);
default:
return (
<Cell key={cellId}>
<Outputs>
<pre>{`Cell Type "${cellType}" is not implemented`}</pre>
</Outputs>
</Cell>
);
}
})}
</Cells>
<style>{/* render styled jsx styles */ flush()}</style>
<style>{`:root {
${themes[this.props.theme]}
--theme-cell-shadow-hover: none;
--theme-cell-shadow-focus: none;
--theme-cell-prompt-bg-hover: var(--theme-cell-prompt-bg);
--theme-cell-prompt-bg-focus: var(--theme-cell-prompt-bg);
--theme-cell-prompt-fg-hover: var(--theme-cell-prompt-fg);
--theme-cell-prompt-fg-focus: var(--theme-cell-prompt-fg);
}
`}</style>
</div>
);
}
}