|
#!/usr/bin/env node |
|
'use strict'; |
|
|
|
/** |
|
* Wrapper script to convert markdown-link-check stdout to lsp_json format |
|
* for use by Trunk.io VSCode Extension |
|
* |
|
* ref: https://github.com/tcort/markdown-link-check/tree/v3.10.0 |
|
*/ |
|
|
|
const fs = require('fs') |
|
const { exec } = require('child_process'); |
|
|
|
const default_config = '.markdown-link-check.json' |
|
const opts = process.argv.slice(2) |
|
|
|
// add default config file if not specified |
|
if (!opts.includes('--config')) { |
|
opts.unshift(default_config) |
|
opts.unshift('--config') |
|
} |
|
|
|
// check if config file exists |
|
if (opts.includes('--config')) { |
|
// TODO: split opt if --config=some/file.json |
|
const k = opts.indexOf('--config') |
|
const v = k+1 |
|
const file = opts[v] |
|
if (!fs.existsSync(file)) { |
|
// Fail if missing a custom config file |
|
if (default_config !== file) { |
|
console.log(`ERROR: missing custom config: ${file}`) |
|
process.exit(1) |
|
} |
|
// Remove default config opts |
|
opts[k] = opts[v] = '' |
|
} |
|
} |
|
|
|
const args = opts.filter((v) => v != '').join(' ') |
|
|
|
exec(`markdown-link-check ${args}`, (err, stdout, stderr) => { |
|
if (err) { |
|
// NOTE: dead links are duplicated in stdout so grab those with Status codes |
|
const dead = [...stdout.matchAll(/\[β\] (?<message>.*) \S Status: (?<status>.*)/g)] |
|
// NOTE: skipped URLs are defined in config. Much faster when only checking local links |
|
const skipped = [...stdout.matchAll(/\[\/\] (?<message>.*)/g)] |
|
// const live = [...stdout.match(/\[β\] (?<message>.*)/g)] |
|
|
|
// Convert stdout text to lsp_json |
|
dead.forEach((v,i,a) => { |
|
const {status, message} = v.groups |
|
a[i] = { |
|
"message": `Dead link: ${status} ${message.replace('[β] ', '')}`, |
|
"code": "link-dead", |
|
"source": "markdown-link-check", |
|
"severity": "Error", |
|
"range": { |
|
"start": { |
|
"line": 0, |
|
"character": 0 |
|
}, |
|
"end": { |
|
"line": 0, |
|
"character": 0 |
|
} |
|
} |
|
} |
|
}) |
|
|
|
skipped.forEach((v,i,a) => { |
|
const {message} = v.groups |
|
a[i] = { |
|
"message": `Skipped link: ${message.replace('[/] ', '')}`, |
|
"code": "link-skipped", |
|
"source": "markdown-link-check", |
|
"severity": "Warning", |
|
"range": { |
|
"start": { |
|
"line": 0, |
|
"character": 0 |
|
}, |
|
"end": { |
|
"line": 0, |
|
"character": 0 |
|
} |
|
} |
|
} |
|
}) |
|
|
|
// List all results |
|
const results = dead.concat(skipped) |
|
|
|
// Output lsp_json formatted results |
|
console.log(JSON.stringify(results)) |
|
process.exit(1) |
|
} |
|
process.exit(0) |
|
}) |
@metafeather they have a public plug-in repo now for folks to contribute