Skip to content

Commit

Permalink
updates for ease inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
boillodmanuel committed Nov 27, 2020
1 parent eb50343 commit 846f2f6
Show file tree
Hide file tree
Showing 9 changed files with 595 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extension": ["ts"],
"spec": "test/**/*.spec.ts",
"require": "ts-node/register"
}
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"printWidth": 120,
"semi": false,
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "all"
}
2 changes: 2 additions & 0 deletions markdown-link-check
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ stream
});

function runMarkdownLinkCheck(markdown, opts) {

markdownLinkCheck(markdown, opts, function (err, results) {
if (err) {
console.error(chalk.red('\nERROR: something went wrong!'));
Expand Down Expand Up @@ -172,4 +173,5 @@ function runMarkdownLinkCheck(markdown, opts) {
process.exit(1);
}
});

}
466 changes: 459 additions & 7 deletions package-lock.json

Large diffs are not rendered by default.

27 changes: 22 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
"bin": {
"markdown-link-check": "markdown-link-check"
},
"main": "index.js",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"format": "prettier --write \"src/**/*.ts\"",
"lint": "tslint -p tsconfig.json",
"lint-fix": "tslint -p tsconfig.json --fix",
"pretest": "jshint index.js markdown-link-check",
"test": "mocha -R spec --exit"
"test": "mocha --exit",
"build": "rm -rf ./lib && tsc"
},
"repository": {
"type": "git",
Expand All @@ -33,20 +38,32 @@
"markdown-link-check"
],
"dependencies": {
"@types/async": "^3.2.4",
"@types/lodash": "^4.14.165",
"@types/node": "^14.14.10",
"@types/progress": "^2.0.3",
"async": "^3.2.0",
"chalk": "^4.1.0",
"commander": "^6.1.0",
"link-check": "^4.5.2",
"link-check": "github:boillodmanuel/link-check",
"lodash": "^4.17.20",
"markdown-link-extractor": "^1.2.6",
"progress": "^2.0.3",
"request": "^2.88.2"
},
"devDependencies": {
"@types/expect.js": "^0.3.29",
"@types/express": "^4.17.9",
"@types/mocha": "^8.0.4",
"expect.js": "^0.3.1",
"mocha": "^8.1.3",
"express": "^4.17.1",
"jshint": "^2.12.0",
"express": "^4.17.1"
"mocha": "^8.1.3",
"prettier": "^2.2.0",
"ts-node": "^9.0.0",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0",
"typescript": "^4.1.2"
},
"jshintConfig": {
"esversion": 6,
Expand Down
63 changes: 34 additions & 29 deletions index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,73 @@
'use strict';

const _ = require('lodash');
const async = require('async');
const linkCheck = require('link-check');
const LinkCheckResult = require('link-check').LinkCheckResult;
const markdownLinkExtractor = require('markdown-link-extractor');
const ProgressBar = require('progress');
import * as _ from 'lodash'
import * as async from 'async'
import { linkCheck } from 'link-check/src'
import * as ProgressBar from 'progress'

module.exports = function markdownLinkCheck(markdown, opts, callback) {
if (arguments.length === 2 && typeof opts === 'function') {
import { MarkdownLinkCheckOptions, LinkCheckResult, Callback } from './types'

export function markdownLinkCheck(markdown: string, optionArg: Options | Callback, callbackArg?: Callback): void {
let options: Options
let callback: Callback

if (arguments.length === 2 && typeof optionArg === 'function') {
// optional 'opts' not supplied.
callback = opts;
opts = {};
callback = optionArg as Callback
options = {}
} else {
callback = callbackArg!
options = optionArg as Options
}

if(!opts.ignoreDisable) {
if(!options.ignoreDisable) {
markdown = [
/(<!--[ \t]+markdown-link-check-disable[ \t]+-->[\S\s]*?<!--[ \t]+markdown-link-check-enable[ \t]+-->)/mg,
/(<!--[ \t]+markdown-link-check-disable[ \t]+-->[\S\s]*(?!<!--[ \t]+markdown-link-check-enable[ \t]+-->))/mg,
/(<!--[ \t]+markdown-link-check-disable-next-line[ \t]+-->\r?\n[^\r\n]*)/mg,
/([^\r\n]*<!--[ \t]+markdown-link-check-disable-line[ \t]+-->[^\r\n]*)/mg
].reduce(function(_markdown, disablePattern) {
].reduce((_markdown, disablePattern) => {
return _markdown.replace(new RegExp(disablePattern), '');
}, markdown);
}

const linksCollection = _.uniq(markdownLinkExtractor(markdown));
const bar = (opts.showProgressBar) ?
const linksCollection: string[] = _.uniq(markdownLinkExtractor(markdown));
const bar = (options.showProgressBar) ?
new ProgressBar('Checking... [:bar] :percent', {
complete: '=',
incomplete: ' ',
width: 25,
total: linksCollection.length
}) : undefined;

async.mapLimit(linksCollection, 2, function (link, callback) {
if (opts.ignorePatterns) {
const shouldIgnore = opts.ignorePatterns.some(function(ignorePattern) {
async.mapLimit(linksCollection, 2, (link, callback) => {
if (options.ignorePatterns) {
const shouldIgnore = options.ignorePatterns.some( (ignorePattern) => {
return ignorePattern.pattern instanceof RegExp ? ignorePattern.pattern.test(link) : (new RegExp(ignorePattern.pattern)).test(link) ? true : false;
});

if (shouldIgnore) {
const result = new LinkCheckResult(opts, link, 0, undefined);
const result = LinkCheckResult.fromStatus(options, link, 0, undefined);
result.status = 'ignored'; // custom status for ignored links
callback(null, result);
return;
}
}

if (opts.replacementPatterns) {
for (let replacementPattern of opts.replacementPatterns) {
let pattern = replacementPattern.pattern instanceof RegExp ? replacementPattern.pattern : new RegExp(replacementPattern.pattern);
if (options.replacementPatterns) {
for (const replacementPattern of options.replacementPatterns) {
const pattern = replacementPattern.pattern instanceof RegExp ? replacementPattern.pattern : new RegExp(replacementPattern.pattern);
link = link.replace(pattern, replacementPattern.replacement);
}
}

// Make sure it is not undefined and that the appropriate headers are always recalculated for a given link.
opts.headers = {};
options.headers = {};

if (opts.httpHeaders) {
for (const httpHeader of opts.httpHeaders) {
if (options.httpHeaders) {
for (const httpHeader of options.httpHeaders) {
for (const url of httpHeader.urls) {
if (link.startsWith(url)) {
Object.assign(opts.headers, httpHeader.headers);
Object.assign(options.headers, httpHeader.headers);

// The headers of this httpHeader has been applied, the other URLs of this httpHeader don't need to be evaluated any further.
break;
Expand All @@ -71,9 +76,9 @@ module.exports = function markdownLinkCheck(markdown, opts, callback) {
}
}

linkCheck(link, opts, function (err, result) {
linkCheck(link, options, (err, result) => {

if (opts.showProgressBar) {
if (bar) {
bar.tick();
}

Expand All @@ -85,4 +90,4 @@ module.exports = function markdownLinkCheck(markdown, opts, callback) {
callback(null, result);
});
}, callback);
};
}
37 changes: 37 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { linkCheck, LinkCheckResult, Options } from 'link-check/src'

export type Callback = (err: any, result: any | null) => void


export interface MarkdownLinkCheckOptions extends Options {
ignoreDisable?: boolean
showProgressBar?: boolean
ignorePatterns?: IgnorePattern[]
replacementPatterns?: ReplacementPattern[]
}

export interface IgnorePattern {
pattern: string | RegExp
}
export interface ReplacementPattern {
pattern: string | RegExp
replacement: string
}

export enum Status {
ALIVE = 'alive',
DEAD = 'dead',
IGNORE = 'ignore',
}

export class MarkdownLinkCheckResult extends LinkCheckResult{
public readonly status: Status

// constructor(opts: Options, link: string, statusCode: number, err?: any, originalError?: any) {
// this.link = link
// this.statusCode = statusCode || 0
// this.status = isAlive(opts, statusCode) ? Status.ALIVE : Status.DEAD
// this.err = err || null
// this.originalError = originalError || err || null
// }
}
22 changes: 22 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"strict": true,
"outDir": "lib",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
},
// "include": [
// "test/**/*"
// ],
"files": [
"src/index.ts"
]
}
7 changes: 7 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": ["tslint:recommended", "tslint-config-prettier"],
"rules": {
"object-literal-sort-keys": false,
"interface-name": [true, "never-prefix"]
}
}

0 comments on commit 846f2f6

Please sign in to comment.