-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
183 lines (179 loc) · 5.24 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SICP note reader</title>
<style>
#markdown-viewer {
margin: 0;
padding: 2%;
width: 46%;
position: absolute;
left: 0;
top: 0;
z-index: -1;
}
#code-viewer {
margin: 0;
border: solid 1px black;
width: 46%;
position: fixed;
right: 0;
top: 0;
max-height: 100%;
overflow: auto;
}
#code-viewer code {
padding: 2%;
}
nav {
}
</style>
<link rel="stylesheet"
href="highlight-js/styles/default.css">
<script src="highlight-js/highlight.pack.js"></script>
</head>
<body>
<nav>
<a href="..">上一層</a>
</nav>
<div id="markdown-viewer"></div>
<pre id="code-viewer"><code></code></pre>
<script src="https://gholk.github.io/marked/marked.min.js"></script>
<script>
async function fetchText(url) {
let text
try {
const response = await fetch(url)
text = await response.text()
}
catch (fetchError) {
text = `${url} not found`
}
return text
}
async function fetchExt(name, extention) {
const response = extention.map(ext => fetchText(`${name}.${ext}`))
const text = await Promise.all(response)
return text
}
const highLight = {}
highLight['markdown-viewer'] = document.querySelector('#markdown-viewer')
highLight['code-viewer'] = document.querySelector('#code-viewer code')
highLight['markdown'] = function (markdown) {
const html = marked(markdown)
this['markdown-viewer'].innerHTML = html
}
highLight['set-code'] = function (code, language) {
const node = this['code-viewer']
node.className = language
node.textContent = code
hljs.highlightBlock(node)
}
highLight['javascript'] = function (code) {
this['set-code'](code, 'javascript')
}
highLight['scheme'] = function (code) {
this['set-code'](code, 'scheme')
}
highLight['emacslisp'] = function (code) {
this['set-code'](code, 'lisp')
}
highLight['text'] = highLight['emacslisp']
highLight.load = async function (url) {
const scan = url.match(/^.*\.(.{1,8})$/)
let extension
if (scan) extension = scan[1]
else throw new Error('file has no extention')
const text = await fetchText(url)
switch (extension) {
case 'js':
highLight['javascript'](text)
break
case 'el':
highLight['emacslisp'](text)
break
case 'sch':
highLight['scheme'](text)
break
case 'md':
highLight['markdown'](text)
break
case 'txt':
default:
highLight['text'](text)
break
}
return text
}
window.addEventListener('click', function clickAnchor(click) {
const node = click.target
if (node.tagName == 'A') {
const rawUrl = node.getAttribute('href')
if (!/^https?:\/\//.test(rawUrl)) {
click.preventDefault()
browseUrl(rawUrl)
}
}
})
function browseUrl(relativeUrl) {
const query = decodeQueryString(location.search.slice(1))
const currentUrl = new URL(query.markdown, location)
const nextUrl = new URL(relativeUrl, currentUrl)
const scan = relativeUrl.match(/\.(\w{1,8})$/)
if (scan) {
const extension = scan[1]
if (extension == 'md') {
query['markdown'] = nextUrl.href
highLight.load(nextUrl.href)
nextUrl.pathname = nextUrl.pathname.replace(/\.md$/, '.sch')
query['code'] = nextUrl.href
highLight.load(nextUrl.href)
}
else {
query['code'] = nextUrl.href
highLight.load(nextUrl.href)
}
}
else {
nextUrl.pathname += '/README.md'
query['markdown'] = nextUrl.href
highLight.load(nextUrl.href)
}
history.pushState('', '', '?' + encodeQueryString(query))
}
window.addEventListener('popstate', function (pop) {
if (!location.search) highLight.load('README.md')
const query = decodeQueryString(location.search.slice(1))
if (query['markdown']) highLight.load(query['markdown'])
if (query['code']) highLight.load(query['code'])
})
if (location.search) {
const query = decodeQueryString(location.search.slice(1))
if (query['markdown']) highLight.load(query['markdown'])
if (query['code']) highLight.load(query['code'])
}
else highLight.load('README.md')
function decodeQueryString(string) {
const pair = string.split(/&/g)
const map = {}
pair.filter(Boolean).forEach((string) => {
const pair = string.split('=')
const name = decodeURIComponent(pair[0])
const value = decodeURIComponent(pair[1] || '')
map[name] = value
})
return map
}
function encodeQueryString(map) {
const queryArray = []
for (const name in map) {
const pairString = encodeURIComponent(name) + '=' +
encodeURIComponent(map[name])
queryArray.push(pairString)
}
return queryArray.join('&')
}
</script>
</body>
</html>