This repository was archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
77 lines (68 loc) · 1.8 KB
/
App.js
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
import './App.css';
import React from 'react';
import MD from 'react-showdown';
import hljs from "highlight.js";
import 'highlight.js/styles/solarized-dark.css';
import Header from './Header'
const { location } = window;
function App() {
return (
<div className="App">
<Header />
<div id="content">
<Content />
</div>
</div>
);
}
class Content extends React.Component {
constructor(props) {
super(...arguments)
this.props = props
this.state = {
value: 'Loading...'
};
}
async componentDidMount() {
this.update();
window.addEventListener('hashchange', this.update.bind(this));
}
async update() {
this.setState({
value: 'Loading...'
})
let value
try {
let data = await fetch(getPage());
if (!data.ok) value = `Error loading page! ${data.status} ${data.statusText}`
else value = await data.text();
}
catch (e) {
if (navigator.onLine)
value = `Error loading page! ${e}`
else
value = 'This page does not appear to be cached! Please connect to the internet and try again.'
}
this.setState({
value
})
}
componentDidUpdate() {
document.querySelectorAll("pre code").forEach(block => {
hljs.highlightBlock(block);
});
}
render() {
return (
<MD
markdown={this.state.value} options={{ ghCodeBlocks: true }} />
)
}
}
function getPage() {
let hash = location.hash.substr(1)
let url = hash ? hash + '.md' : `index.md`
url = process.env.PUBLIC_URL + '/' + url;
return url;
};
export default App;