forked from markserv/markserv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
780 lines (650 loc) · 19.7 KB
/
server.js
File metadata and controls
780 lines (650 loc) · 19.7 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#!/usr/bin/env node
'use strict'
// Markdown Extension Types
const markdownExtensions = [
'.markdown',
'.mdown',
'.mkdn',
'.md',
'.mkd',
'.mdwn',
'.mdtxt',
'.mdtext',
'.text',
'.txt'
]
const watchExtensions = markdownExtensions.concat([
'.less',
'.js',
'.css',
'.html',
'.htm',
'.json',
'.gif',
'.png',
'.jpg',
'.jpeg'
])
const PORT_RANGE = {
HTTP: [8000, 8100]
}
const http = require('http')
const path = require('path')
const fs = require('fs')
const open = require('open')
const connect = require('connect')
const micromatch = require('micromatch')
const watch = require('./lib/watch')
const sync = require('./lib/sync')
const config = {
MarkconfDefaults: {
watch: {
ignore: [
'node_modules',
'.git*',
'.svn',
'.hg',
'tmp',
'.DS_Store'
]
}
}
}
config.MarkconfDir = process.cwd()
const watcher = watch(config)
const fm = require('front-matter')
const md = require('markdown-it')({
html: true,
linkify: true,
typographer: false
})
.use(require('markdown-it-anchor'))
.use(require('markdown-it-sub'))
.use(require('markdown-it-sup'))
.use(require('markdown-it-footnote'))
.use(require('markdown-it-deflist'))
.use(require('markdown-it-katex'))
function setMetadata (page, options) {
// Check for metadata key:value presence and make defaults if needed
if (!page['categories']) {page.categories = 'pub'}
if (!page['layout']) {page.layout = 'post'}
if (!page['url']) {page.url = '/'+page.file}
if (!page['title']) {page.title = ''}
if (!page['author']) {page.author = ''}
// Check metadata date obj and make default if needed (parsed from filename or current date)
if (!page['date'] && !page.file.match(/[0-9]{4}-[0-9]{2}-[0-9]{2}[-_]/,'')) {
page.date = new Date()
} else if (!page['date']) {
page.date = new Date(page.file.substr(0,10))
}
return page
}
function mdparse (data, file, options='') {
try {
//parse dataObj yaml header and content into dataObj.attributes and dataObj.body
const dataParse = fm(data)
const page = {}
page.file = path.parse(file).name + '.html'
page.content = md.render(dataParse.body)
// Hoist metadata attributes from front-matter() to parent level of page object
Object.keys(dataParse.attributes).forEach(key => {
return (page[key] = dataParse.attributes[key])
})
return setMetadata(page, options)
} catch (err) {
console.log(`Warning: ${err.name} file: ${file}\n ${err.reason}, continuing with entire file...\n`)
const page = {}
page.file = path.parse(file).name + '.html'
page.content = md.render(data)
return setMetadata(page, options)
}
}
const less = require('less')
const send = require('send')
const jsdom = require('jsdom')
const flags = require('commander')
const openPort = require('openport')
const ansi = require('ansi')
const cursor = ansi(process.stdout)
const pkg = require('./package.json')
// Path Variables
const GitHubStyle = path.join(__dirname, 'less/github.less')
const BootStrapStyle = path.join(__dirname, 'less/bootstrap-custom.less')
// Options
flags.version(pkg.version)
.option('-d, --dir [type]', 'Serve from directory [dir]', './')
.option('-p, --port [type]', 'Serve on port [port]', null)
.option('-h, --header [type]', 'Header .md file', null)
.option('-r, --footer [type]', 'Footer .md file', null)
.option('-n, --navigation [type]', 'Navigation .md file', null)
.option('-a, --address [type]', 'Serve on ip/address [address]', 'localhost')
.option('-s, --less [type]', 'Path to Less styles [less]', BootStrapStyle)
.option('-f, --file [type]', 'Open specific file in browser [file]')
.option('-x, --x', 'Don\'t open browser on run.')
.option('-v, --verbose', 'verbose output')
.parse(process.argv)
const dir = flags.dir
const cssPath = flags.less
console.log(cssPath)
// Terminal Output Messages
const msg = type => cursor
.bg.green()
.fg.black()
.write(' Markserv ')
.reset()
.fg.white()
.write(' ' + type + ': ')
.reset()
const errormsg = type => cursor
.bg.red()
.fg.black()
.write(' Markserv ')
.reset()
.write(' ')
.fg.black()
.bg.red()
.write(' ' + type + ': ')
.reset()
.fg.red()
.write(' ')
// load phantomjs pdf making functionality
const pdf = require('html-pdf')
try {
var options = JSON.parse(fs.readFileSync('./css/pdf-config-cover.json'))
options.base = 'file://' + process.cwd() + '/'
} catch (err) {
errormsg('warning')
.write('config.json not found, making default config for html-pdf: ', err)
.reset().write('\n')
var options = {
"format": "Letter",
"border": {
"top": "0.5in",
"right": "0.5in",
"bottom": "0.5in",
"left": "0.5in"
},
"footer": {
"height": "0.5",
"contents": {
"first": " ",
"default": "<div style='text-align:right;color:#adadad;font-size:0.875em'>{{page}}/{{pages}}</div>"
}
},
"base": 'file://' + process.cwd() + '/'
}
}
// hasMarkdownExtension: check whether a file is Markdown type
const hasMarkdownExtension = (fileName) => {
const fileExtension = path.extname(fileName).toLowerCase()
let extensionMatch = false
markdownExtensions.forEach(extension => {
if (extension === fileExtension) {
extensionMatch = true
}
})
return extensionMatch
}
// getFile: reads utf8 content from a file
const getFile = (fileName) => new Promise((resolve, reject) => {
fs.readFile(fileName, 'utf8', (err, data) => {
if (err) {
return reject(err)
}
resolve(data)
})
})
// Get Custom Less CSS to use in all Markdown files
const buildStyleSheet = (cssPath) =>
new Promise(resolve =>
getFile(cssPath).then(data =>
less.render(data).then(data =>
resolve(data.css)
)
)
)
// markdownToHTML: turns a Markdown file into HTML content
const markdownToHTML = (data, fileName) => new Promise(resolve => {
resolve(mdparse(data, fileName))
})
// linkify: converts github style wiki markdown links to .md links
const linkify = (page) => new Promise((resolve, reject) => {
jsdom.env(page.content, (err, window) => {
if (err) {
return reject(err)
}
const links = window.document.getElementsByTagName('a')
const l = links.length
let href
let link
let markdownFile
let mdFileExists
let relativeURL
let isFileHref
for (let i = 0; i < l; i++) {
link = links[i]
href = link.href
isFileHref = href.substr(0, 8) === 'file:///'
markdownFile = href.replace(path.join('file://', __dirname), flags.dir) + '.md'
mdFileExists = fs.existsSync(markdownFile)
if (isFileHref && mdFileExists) {
relativeURL = href.replace(path.join('file://', __dirname), '') + '.md'
link.href = relativeURL
}
}
page.content = window.document.getElementsByTagName('body')[0].innerHTML
resolve(page)
})
})
// ---begin main build function---
// buildHTMLFromMarkDown: compiles the final HTML/CSS output from Markdown/Less files, includes JS
const buildHTMLFromMarkDown = (fileName, query) => new Promise(resolve => {
const stack = [
buildStyleSheet(cssPath),
// Article
getFile(fileName)
.then(data => { return markdownToHTML(data, fileName) })
.then(linkify),
// Header
flags.header && getFile(flags.header)
.then(data => { return markdownToHTML(data, fileName) })
.then(linkify),
// Footer
flags.footer && getFile(flags.footer)
.then(data => { return markdownToHTML(data, fileName) })
.then(linkify),
// Navigation
flags.navigation && getFile(flags.navigation)
.then(data => { return markdownToHTML(data, fileName) })
.then(linkify)
]
Promise.all(stack).then(dataArr => {
const css = dataArr[0]
// console.log(dataArr[1])
const page = dataArr[1]
const dirs = fileName.split('/')
console.log(
`title: ${page.title}
url: ${page.url}
tags: ${page.tags}
date: ${page.date}
author: ${page.author}`)
let dropMenuhtml
if (query === 'pdf') {
dropMenuhtml = ''
} else {
const dropMenu = {
pdf: fileName.slice(2) + '?pdf'
}
dropMenuhtml = (
// <-- template literal block
`<div class="btn-group">
<button type="button" onclick="menuToggle()" id="dropmenubutton" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-menu-hamburger"></span>
</button>
<ul class="dropdown-menu" id="dropmenu" role="menu" >
<li><a href="${dropMenu.pdf}"><span class="glyphicon glyphicon-print"></span> pdf</a></li>
<li role="separator" class="divider"></li>
</ul>
</div>`)
}
let header
let footer
let navigation
let outputHtml
if (flags.header) {
header = dataArr[2]
}
if (flags.footer) {
footer = dataArr[3]
}
if (flags.navigation) {
navigation = dataArr[4]
}
//setup stylesheet
if (flags.less === GitHubStyle) {
var cssBlock = (
// <-- template literal block
`<style>
${css}
</style>
<link rel="stylesheet" href="//sindresorhus.com/github-markdown-css/github-markdown.css">`)
} else {
var cssBlock = (
// <-- template literal block
`<style>
${css}
</style>`)
}
//setup html and document body
outputHtml = (
// <-- template literal block
`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${page.title}</title>
<meta name="description" content="${page.tags}">
<meta name="author" content="${page.author}">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css" async>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github-gist.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/matlab.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/r.min.js"></script>
${cssBlock}
<script>
function buildMenu() {
const menuDiv = document.getElementById("dropmenu")
if ( menuDiv != null && menuDiv.children.length > 0 && menuDiv.children.length < 3 ) {
const list = document.querySelectorAll("h1, h2, h3, h4")
let innerText = ''
list.forEach(item => {
const currLev = item.tagName.toLowerCase()
const node = document.createElement("li")
const ref = document.createElement("a")
ref.setAttribute("href","#" + item.id)
if (currLev === 'h1') {
innerText = item.textContent
} else if (currLev === 'h2') {
innerText = "\xA0\xA0" + item.textContent
} else if (currLev === 'h3') {
innerText = "\xA0\xA0\xA0\xA0" + item.textContent
} else {
innerText = "\xA0\xA0\xA0\xA0\xA0\xA0" + item.textContent
}
const textnode = document.createTextNode(innerText)
ref.appendChild(textnode)
node.appendChild(ref)
document.getElementById("dropmenu").appendChild(node)
})
menuDiv.addEventListener('click', menuToggle, false)
}
document.addEventListener('keydown', onDocumentKeyDown, false)
}
function onDocumentKeyDown(event) {
switch(event.which){
case 77:
menuToggle()
event.preventDefault()
break
case 27:
menuToggle()
event.preventDefault()
break
}
}
function menuToggle() {
const menuDiv = document.getElementById("dropmenu")
menuDiv.classList.toggle('show')
if (!menuDiv.classList.contains("show")) {
document.getElementById("dropmenubutton").blur()
}
}
window.onload = buildMenu
</script>
</head>
<body>
${dropMenuhtml}
<article class="markdown-body">
${page.content}
</article>
</body>
<script>hljs.initHighlightingOnLoad()</script>
</html>`)
resolve(outputHtml)
})
.catch (err => { console.log(err) })
})
// ---end main build---
// Create pdf file and save locally on server...
const printPDF = (fileName, res, query) => buildHTMLFromMarkDown(fileName, query)
.then(html => {
res.writeHead(200)
res.end(html)
console.log('Rendering pdf...')
const outFile = path.parse(fileName).name
pdf.create(html, options).toFile( outFile + '.pdf', (err, res) => {
if (err) return console.log(err)
console.log(res)
})
})
.catch (err => {
msg('error')
.write('Can\'t build HTML: ', err)
.reset().write('\n')
})
// Begin markdown compilation process, then send result when done...
const compileAndSendMarkdown = (fileName, res, query) => buildHTMLFromMarkDown(fileName, query)
.then(html => {
res.writeHead(200)
res.end(html)
})
.catch (err => {
msg('error')
.write('Can\'t build HTML: ', err)
.reset().write('\n')
})
//setup list object for initial directory index listing
const compileAndSendDirectoryListing = (fileName, res) => {
const urls = fs.readdirSync(fileName)
let list = '\n <ul>\n'
urls.forEach(subPath => {
const dir = fs.statSync(fileName + subPath).isDirectory()
let href
if (dir) {
href = subPath + '/'
list += ` <li class="dir"><a href="${href}">${href}</a></li> \n`
} else {
href = subPath
if (subPath.split('.md')[1] === '') {
list += ` <li class="md"><a href="${href}">${href}</a></li> \n`
} else {
list += ` <li class="file"><a href="${href}">${href}</a></li> \n`
}
}
})
list += ' </ul>\n'
//setup template literal for initial directory listing
buildStyleSheet(cssPath).then( css => {
const html = (
`<!DOCTYPE html>
<html>
<head>
<title>${fileName.slice(2)}</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css" async />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github-gist.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/matlab.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/r.min.js"></script>
<link rel="shortcut icon" type="image/x-icon" href="https://cdn0.iconfinder.com/data/icons/octicons/1024/markdown-128.png" />
<style>
${css}
</style>
</head>
<body>
<article class="markdown-body">
<h1>Index of ${fileName.slice(2)}</h1>
${list}
<sup><hr> Served by <a href="https://www.npmjs.com/package/markserv">MarkServ</a> | PID: ${process.pid}</sup>
</article>
</body>
</html>`
)
// Log if verbose
if (flags.verbose) {
msg('index').write(fileName).reset().write('\n')
}
// Send file
res.writeHead(200, {'Content-Type': 'text/html'})
res.write(html)
res.end()
})
}
// Remove URL params from file being fetched
const getPathFromUrl = url => {
return url.split(/[?#]/)[0]
}
// http_request_handler: handles all the browser requests
const httpRequestHandler = (req, res) => {
const originalUrl = getPathFromUrl(req.originalUrl)
const query = req.originalUrl.split(/[?]/)[1]
if (flags.verbose) {
msg('request')
.write(decodeURI(dir) + decodeURI(originalUrl))
.reset().write('\n')
}
const fileName = decodeURI(dir) + decodeURI(originalUrl)
let stat
let isDir
let isMarkdown
try {
stat = fs.statSync(fileName)
isDir = stat.isDirectory()
isMarkdown = false
if (!isDir) {
isMarkdown = hasMarkdownExtension(fileName)
}
} catch (err) {
res.writeHead(200, {'Content-Type': 'text/html'})
errormsg('404').write(fileName.slice(2)).reset().write('\n')
res.write('404 :\'(')
res.end()
return
}
// Markdown: Browser is requesting a Markdown file
if (query === 'pdf') {
printPDF(fileName, res, query)
} else if (isMarkdown) {
msg('markdown').write(fileName.slice(2)).reset().write('\n')
compileAndSendMarkdown(fileName, res, query)
} else if (isDir) {
// Index: Browser is requesting a Directory Index
msg('dir').write(fileName.slice(2)).reset().write('\n')
compileAndSendDirectoryListing(fileName, res)
} else {
// Other: Browser requests other MIME typed file (handled by 'send')
msg('file').write(fileName.slice(2)).reset().write('\n')
send(req, fileName, {root: dir}).pipe(res)
}
}
let HTTP_PORT
let CONNECT_APP
const findOpenPort = range => new Promise((resolve, reject) => {
const props = {
startingPort: range[0],
endingPort: range[1]
}
openPort.find(props, (err, port) => {
if (err) {
return reject(err)
}
resolve(port)
})
})
// ---define the connect app promise and its four primary promise children---
const startConnectApp = () => new Promise(resolve => {
CONNECT_APP = connect()
.use('/', httpRequestHandler)
resolve(CONNECT_APP)
})
const setHTTPPort = port => new Promise(resolve => {
HTTP_PORT = port
resolve(port)
})
const startHTTPServer = () => new Promise(resolve => {
const HTTP_SERVER = http.createServer(CONNECT_APP)
HTTP_SERVER.listen(HTTP_PORT, flags.address)
resolve(HTTP_SERVER)
})
const startLiveReloadServer = () => new Promise(resolve => {
const rootDir = process.cwd()
//logLevel: info, debug, silent
const syncConfig = {
logPrefix: 'Browsersync',
port: HTTP_PORT,
proxy: 'localhost:' + HTTP_PORT,
open: false,
logLevel: 'info',
notify: false
}
sync.start(syncConfig)
const files = [
'**/*.txt',
'**/*.text',
'**/*.md',
'**/*.html'
]
const watchList = []
if (files) {
files.forEach(filePattern => {
watchList.push(path.join(rootDir, filePattern))
})
}
const handleChanges = (changedFile, changeType) => {
const shortPattern = path.relative(rootDir, changedFile)
const changed = micromatch(changedFile, watchList).length > 0
console.log(changedFile)
console.log(shortPattern)
console.log(changed)
if (changed) {
// log.info(`Watch: found ${log.hl('files')} rule ${log.hl(shortPattern)} for: ${log.hl(changeType)} ${log.ul(changedFile)}`)
sync.reload(changedFile)
} else {
// log.trace(`Watch: found no rule for: ${log.hl(changeType)} ${log.ul(changedFile)}`)
console.log('Watch: found no rule')
}
}
const watchDirs = []
watchDirs.push(rootDir)
watcher.create(watchDirs, handleChanges)
resolve(syncConfig)
})
const serversActivated = () => {
const serveURL = 'http://' + flags.address + ':' + HTTP_PORT
msg('start')
.write('serving content from ')
.fg.white().write(path.resolve(flags.dir)).reset()
.write(' on port: ')
.fg.white().write(String(HTTP_PORT)).reset()
.write('\n')
msg('address')
.underline().fg.white()
.write(serveURL).reset()
.write('\n')
msg('less')
.write('using style from ')
.fg.white().write(flags.less).reset()
.write('\n')
if (process.pid) {
msg('process')
.write('your pid is: ')
.fg.white().write(String(process.pid)).reset()
.write('\n')
msg('info')
.write('to stop this server, press: ')
.fg.white().write('[Ctrl + C]').reset()
.write(', or type: ')
.fg.white().write('"kill ' + process.pid + '"').reset()
.write('\n')
}
if (flags.file) {
open(serveURL + '/' + flags.file)
} else if (!flags.x) {
open(serveURL)
}
}
// Initialize MarkServ connect app
startConnectApp()
.then(() => {
if (flags.port === null) {
return findOpenPort(PORT_RANGE.HTTP)
}
return flags.port
})
.then(setHTTPPort)
.then(startHTTPServer)
.then(startLiveReloadServer)
.then(serversActivated)
.catch (err => { console.log(err) })