Skip to content

Commit

Permalink
feat: add support for markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Nov 28, 2020
1 parent c915959 commit 6c068b8
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ package-lock.json
.nyc_output
build/
coverage
.vscode
9 changes: 5 additions & 4 deletions package.json
Expand Up @@ -21,23 +21,24 @@
"dependencies": {
"chalk": "^4.0.0",
"cheerio": "^1.0.0-rc.2",
"finalhandler": "^1.1.2",
"express": "^4.17.1",

"gaxios": "^4.0.0",
"jsonexport": "^3.0.0",
"marked": "^1.2.5",
"meow": "^8.0.0",
"p-queue": "^6.2.1",
"serve-static": "^1.14.1",
"server-destroy": "^1.0.1",
"update-notifier": "^5.0.0"
},
"devDependencies": {
"@types/chai": "^4.2.7",
"@types/cheerio": "^0.22.10",
"@types/finalhandler": "^1.1.0",
"@types/express": "^4.17.9",
"@types/marked": "^1.2.0",
"@types/meow": "^5.0.0",
"@types/mocha": "^8.0.0",
"@types/node": "^12.7.12",
"@types/serve-static": "^1.13.3",
"@types/server-destroy": "^1.0.0",
"@types/sinon": "^9.0.0",
"@types/update-notifier": "^5.0.0",
Expand Down
63 changes: 50 additions & 13 deletions src/index.ts
Expand Up @@ -2,14 +2,18 @@ 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 * as fs from 'fs';
import * as util from 'util';
import * as path from 'path';
import * as marked from 'marked';
import PQueue, {DefaultAddOptions} from 'p-queue';

import {getLinks} from './links';
import {URL} from 'url';
import PriorityQueue from 'p-queue/dist/priority-queue';

import finalhandler = require('finalhandler');
import serveStatic = require('serve-static');
const stat = util.promisify(fs.stat);

export interface CheckOptions {
concurrency?: number;
Expand Down Expand Up @@ -61,10 +65,20 @@ export class LinkChecker extends EventEmitter {
options.linksToSkip = options.linksToSkip || [];
let server: http.Server | undefined;
if (!options.path.startsWith('http')) {
let localDirectory = options.path;
let localFile = '';
const s = await stat(options.path);
if (s.isFile()) {
const pathParts = options.path.split(path.sep);
localFile = path.sep + pathParts[pathParts.length - 1];
localDirectory = pathParts
.slice(0, pathParts.length - 1)
.join(path.sep);
}
const port = options.port || 5000 + Math.round(Math.random() * 1000);
server = await this.startWebServer(options.path, port);
server = await this.startWebServer(localDirectory, port);
enableDestroy(server);
options.path = `http://localhost:${port}`;
options.path = `http://localhost:${port}${localFile}`;
}

const queue = new PQueue({
Expand Down Expand Up @@ -104,16 +118,39 @@ export class LinkChecker extends EventEmitter {
* @private
* @returns Promise that resolves with the instance of the HTTP server
*/
private startWebServer(root: string, port: number): Promise<http.Server> {
return new Promise((resolve, reject) => {
const serve = serveStatic(root);
const server = http
.createServer((req, res) =>
serve(req, res, finalhandler(req, res) as () => void)
)
.listen(port, () => resolve(server))
.on('error', reject);
private async startWebServer(root: string, port: number) {
const app = express()
.use((req, res, next) => {
const pathParts = req.path
.toLowerCase()
.split('/')
.filter(x => !!x);
if (pathParts.length === 0) {
return next();
}
const ext = path.extname(pathParts[pathParts.length - 1]);
if (ext === '.md') {
fs.readFile(root + req.path, {encoding: 'utf-8'}, (err, data) => {
if (err) {
return next(err);
}
marked(data, {gfm: true}, (err, result) => {
if (err) {
return next(err);
}
res.send(result).end();
return;
});
});
} else {
return next();
}
})
.use(express.static(root));
const server = await new Promise<http.Server>(resolve => {
const s = app.listen(port, () => resolve(s));
});
return server;
}

/**
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/markdown/LICENSE.md
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Justin Beckwith <justin.beckwith@gmail.com> (jbeckwith.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
6 changes: 6 additions & 0 deletions test/fixtures/markdown/README.md
@@ -0,0 +1,6 @@
# Say hello to my README
This has [a link](LICENSE.md) to something.

Also here is my cat.
![booboobadkitteh](boo.jpg)

Binary file added test/fixtures/markdown/boo.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions test/test.ts
Expand Up @@ -280,4 +280,13 @@ describe('linkinator', () => {
});
assert.ok(!results.passed);
});

it('should handle markdown', async () => {
const results = await check({
path: 'test/fixtures/markdown/README.md',
recurse: true,
});
assert.strictEqual(results.links.length, 3);
assert.ok(results.passed);
});
});
3 changes: 1 addition & 2 deletions tsconfig.json
Expand Up @@ -7,7 +7,6 @@
},
"include": [
"src/*.ts",
"test/*.ts",
"system-test/*.ts"
"test/*.ts"
]
}

0 comments on commit 6c068b8

Please sign in to comment.