1
1
/* eslint-disable react/prop-types */
2
2
import React , { useState , useEffect } from "react" ;
3
3
import usePathname from "../usePathname" ;
4
+ import CodeBlock from "@theme/CodeBlock" ;
4
5
import JupyterViewer from "react-jupyter-notebook" ;
5
6
import OpenInColab from "../OpenInColab/OpenInColab" ;
7
+ import "./styles.css" ;
6
8
7
9
/**
8
10
* ipynbファイルからソースコードと出力、OpenInColabへのリンクを生成
@@ -13,21 +15,36 @@ import OpenInColab from "../OpenInColab/OpenInColab";
13
15
14
16
export default function ViewSource ( { path } ) {
15
17
const pathname = usePathname ( ) ;
18
+ const [ sources , setSources ] = useState < string [ ] > ( [ ] ) ;
16
19
const [ content , setContent ] = useState ( ) ;
17
20
useEffect ( ( ) => {
18
21
async function tmp ( ) {
19
22
// 該当のipynbファイルをjson形式でとってくる
20
23
const json = await import (
21
24
`/docs/${ pathname . slice ( 6 ) } ${ path . slice ( 0 , - 6 ) } .json`
22
25
) ;
26
+ setSources (
27
+ json . cells
28
+ . filter ( ( cell ) => cell . cell_type === "code" )
29
+ . map ( ( cell ) => cell . source . join ( "" ) )
30
+ ) ;
23
31
setContent ( json ) ;
24
32
}
25
33
tmp ( ) ;
26
34
} , [ ] ) ;
27
35
return (
28
36
< >
37
+ { sources . map ( ( source , i ) => (
38
+ < React . Fragment key = { i } >
39
+ < CodeBlock language = "python" > { source } </ CodeBlock >
40
+ </ React . Fragment >
41
+ ) ) }
29
42
{ content !== undefined && (
30
- < JupyterViewer rawIpynb = { content } language = "python" />
43
+ < JupyterViewer
44
+ rawIpynb = { content }
45
+ language = "python"
46
+ displaySource = "hide"
47
+ />
31
48
) }
32
49
< OpenInColab path = { path } />
33
50
</ >
0 commit comments