Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ import {EventEmitter} from 'events'; | |
import * as gaxios from 'gaxios'; | ||
import * as http from 'http'; | ||
import enableDestroy = require('server-destroy'); | ||
import * as express from 'express'; | ||
import finalhandler = require('finalhandler'); | ||
import serveStatic = require('serve-static'); | ||
import * as fs from 'fs'; | ||
import * as util from 'util'; | ||
import * as path from 'path'; | ||
|
@@ -179,9 +180,8 @@ export class LinkChecker extends EventEmitter { | |
if (s.isFile()) { | ||
const pathParts = options.path[0].split(path.sep); | ||
options.path = [path.sep + pathParts[pathParts.length - 1]]; | ||
options.serverRoot = pathParts | ||
.slice(0, pathParts.length - 1) | ||
.join(path.sep); | ||
options.serverRoot = | ||
pathParts.slice(0, pathParts.length - 1).join(path.sep) || '.'; | ||
} else { | ||
options.serverRoot = options.path[0]; | ||
options.path = '/'; | ||
|
@@ -201,27 +201,29 @@ export class LinkChecker extends EventEmitter { | |
* @returns Promise that resolves with the instance of the HTTP server | ||
*/ | ||
private async startWebServer(root: string, port: number, markdown?: boolean) { | ||
const app = express() | ||
.use(async (req, res, next) => { | ||
if (!markdown) { | ||
return next(); | ||
} | ||
const pathParts = req.path.split('/').filter(x => !!x); | ||
const ext = path.extname(pathParts[pathParts.length - 1]); | ||
if (ext.toLowerCase() === '.md') { | ||
const filePath = path.join(path.resolve(root), req.path); | ||
const data = await readFile(filePath, {encoding: 'utf-8'}); | ||
const result = marked(data, {gfm: true}); | ||
res.send(result).end(); | ||
return; | ||
} | ||
return next(); | ||
}) | ||
.use(express.static(path.resolve(root))); | ||
const server = await new Promise<http.Server>(resolve => { | ||
const s = app.listen(port, () => resolve(s)); | ||
return new Promise<http.Server>((resolve, reject) => { | ||
const serve = serveStatic(root); | ||
const server = http | ||
.createServer(async (req, res) => { | ||
const pathParts = req.url!.split('/').filter(x => !!x); | ||
if (pathParts.length > 0) { | ||
const ext = path.extname(pathParts[pathParts.length - 1]); | ||
if (markdown && ext.toLowerCase() === '.md') { | ||
const filePath = path.join(path.resolve(root), req.url!); | ||
const data = await readFile(filePath, {encoding: 'utf-8'}); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
JustinBeckwith
Author
Owner
|
||
const result = marked(data, {gfm: true}); | ||
res.writeHead(200, { | ||
'content-type': 'text/html', | ||
}); | ||
res.end(result); | ||
return; | ||
} | ||
} | ||
return serve(req, res, finalhandler(req, res) as () => void); | ||
}) | ||
.listen(port, () => resolve(server)) | ||
.on('error', reject); | ||
}); | ||
return server; | ||
} | ||
|
||
/** | ||
|
@JustinBeckwith FYI this results in a new lgtm alert https://lgtm.com/projects/g/JustinBeckwith/linkinator/?mode=list