Skip to content

Commit b78c7ab

Browse files
committed
add input 'gist_id', 'file_name', and submit button
1 parent 5d51d0d commit b78c7ab

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

index.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'/>
99
</head>
1010
<body>
11-
<h1>Gist HTML Preview</h1>
12-
<p>gistpreview.github.io/?&lt;gist_id&gt;/&lt;file_name&gt;</p>
11+
<div class="container">
12+
<h2>Gist HTML Preview</h2>
13+
<span>https://gistpreview.github.io/?</span>
14+
<input type="text" class="form-control input-sm" id="gist_id" placeholder="Gist ID">
15+
<span>/</span>
16+
<input type="text" class="form-control input-sm" id="file_name" placeholder="File Name">
17+
<button id="submit" class="btn btn-primary">Submit</button>
18+
</div>
19+
1320
<script src="https://cdn.rawgit.com/github/fetch/master/fetch.js"></script>
1421
<script src="./main.js"></script>
1522
</body>
1623
</html>
24+

main.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,50 @@
11
(function () {
2-
var input = location.search.substring(1).split('/');
3-
var gistId = input[0];
4-
var fileName = input[1] || 'index.html';
2+
document.getElementById('submit').onclick = function () {
3+
location.search = document.getElementById('gist_id').value + '/'
4+
+ document.getElementById('file_name').value;
5+
}
56

7+
// 1. check query string
8+
var query = location.search.substring(1);
9+
if (query.length === 0) {
10+
return;
11+
}
12+
13+
// 2. get gist id and file name
14+
query = query.split('/');
15+
var gistId = query[0];
16+
var fileName = query[1];
17+
18+
// 3. write data to blank
19+
document.getElementById('gist_id').value = gistId;
20+
document.getElementById('file_name').value = fileName;
21+
22+
// 4-1. check gist id
23+
if (/^[0-9a-f]*$/g.test(gistId) === false) {
24+
console.error('Gist Id ' + gistId + ' is invalid')
25+
return;
26+
}
27+
28+
// 4-2. check file name
29+
if (typeof fileName !== 'string' || fileName.length === 0) {
30+
console.error('File Name ' + fileName + ' is invalid');
31+
return;
32+
}
33+
34+
// 5. fetch data
635
fetch('https://api.github.com/gists/' + gistId)
736
.then(function (res) {
837
if (res.status !== 200) {
9-
var err = new Error(gistId + ' is not found');
10-
err.code = 'ENOENT';
11-
throw err;
38+
throw new Error('Gist Id ' + gistId + ' is not exist');
1239
}
1340
return res.json();
1441
})
1542
.then(function (info) {
1643
if (info.files.hasOwnProperty(fileName) === false) {
17-
var err = new Error(fileName + ' is not found');
18-
err.code = 'ENOENT';
19-
throw err;
44+
throw new Error('File ' + fileName + ' is not exist');
2045
}
2146

47+
// 6. write data
2248
var content = info.files[fileName].content;
2349
document.write(content);
2450
})

0 commit comments

Comments
 (0)