-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (105 loc) · 3.61 KB
/
index.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
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
#! /usr/bin/env node
import fs from 'fs'
import path from 'path'
import http from 'http'
import getPort from 'get-port'
import { micromark } from 'micromark'
import { gfm, gfmHtml } from 'micromark-extension-gfm'
import pocket from 'pocket.io'
import filewatcher from 'filewatcher'
import pkg from './package.json'
const [readme, desiredPort] = process.argv.slice(2)
function createDevClient({ port }) {
return `
<script>
(function (global) {
try {
const socketio = document.createElement('script')
socketio.src = 'https://unpkg.com/[email protected]/min.js'
socketio.onload = function init () {
var disconnected = false
var socket = io('http://localhost:${port}', {
reconnectionAttempts: 3
})
socket.on('connect', function() { console.log('presta connected on port ${port}') })
socket.on('refresh', function() {
global.location.reload()
})
socket.on('disconnect', function() {
disconnected = true
})
socket.on('reconnect_failed', function(e) {
if (disconnected) return
console.error("presta - connection to server on :${port} failed")
})
}
document.head.appendChild(socketio)
} catch (e) {}
})(this);
</script>
`
}
;(async () => {
if (!readme) {
console.error(`\n ${pkg.name}@${pkg.version}\n\n 👉 ${pkg.name} path/to/README.md [port]`)
process.exit(1)
return
}
const port = desiredPort || (await getPort())
const devClient = createDevClient({ port })
const filepath = path.join(process.cwd(), readme)
const server = http
.createServer((req, res) => {
const markdown = fs.readFileSync(filepath, 'utf-8')
const body = micromark(markdown, { extensions: [gfm()], htmlExtensions: [gfmHtml] })
res.writeHead(200, {
'Content-Type': 'text/html',
})
res.write(
`<!DOCTYPE html>
<html>
<head>
<title>${pkg.name}@${pkg.version}</title>
<link rel='stylesheet' href='https://github.githubassets.com/assets/frameworks-cbbaa69edba29ec29cf6fce3cfb63359.css' />
<link rel='stylesheet' href='https://github.githubassets.com/assets/behaviors-e78f792bb4eb683743d2b5231fb828db.css' />
<link rel="stylesheet" href="https://unpkg.com/@highlightjs/[email protected]/styles/github.min.css">
<script src="https://unpkg.com/@highlightjs/[email protected]/highlight.min.js"></script>
<style>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
pre code.hljs {
padding: 0;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
</style>
</head>
<body>
<div class='markdown-body entry-content container-lg'>
${body}
</div>
${devClient}
<script>hljs.highlightAll();</script>
</body>
</html>`
)
res.end()
})
.listen(port, () => {
console.log(`\n ${pkg.name}@${pkg.version}\n\n 👍 serving ${readme} on http://localhost:${port}\n`)
})
const socket = pocket(server, { serveClient: false })
const watcher = filewatcher()
watcher.add(filepath)
watcher.on('change', (file, stat) => {
socket.emit('refresh')
})
})()