Skip to content
Permalink
Browse files

Add build check for broken links

Use markdown-link-check to ensure no broken links found in repo.

Because we want all links in our Handbook to go somewhere useful for our Handbook users.

This branch will only be mergeable once all links have been fixed. The fixing of those links should happen in others branches and not pollute this change.
  • Loading branch information
lukemorton committed Jan 24, 2020
1 parent e462ba3 commit 5fc1ffe126b9d21b00906f7d37d423e092fdd6ed
Showing with 721 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +63 −0 build.js
  3. +24 −0 circle.yml
  4. +608 −0 package-lock.json
  5. +24 −0 package.json
@@ -0,0 +1,2 @@
node_modules
.DS_Store
@@ -0,0 +1,63 @@
const glob = require('glob')
const fs = require('fs')
const path = require('path')
const markdownLinkCheck = require('markdown-link-check')
const chalk = require('chalk')

function handleError (err) {
if (err) {
console.error(chalk.red('%s'), err)
process.exit(1)
}
}

function checkFile (fileName) {
return new Promise((resolve, reject) => {
fs.readFile(fileName, 'utf8', (err, md) => {
handleError(err)

const baseUrl = `file://${path.dirname(path.resolve(fileName))}`

markdownLinkCheck(md, { baseUrl }, (err, results) => {
handleError(err)

let hasErrored = false

results.forEach(function (result) {
if (result.status !== 'alive') {
if (!hasErrored) {
console.log('%s', fileName)
hasErrored = true
}

console.log(chalk.grey(' [' + chalk.red('%s') + '] %s'), result.status, result.link)
}
})

if (hasErrored) {
reject(fileName)
} else {
resolve(fileName)
}
})
})
})
}

glob('**/*.md', (err, fileNames) => {
handleError(err)

const checkableFileNames = fileNames.filter((fileName) => !fileName.includes('node_module'))

const allChecks = Promise.allSettled(
checkableFileNames.map((fileName) => checkFile(fileName))
)

allChecks.then((results) => {
if (results.some(({ status }) => status === 'rejected')) {
handleError('Broken links found')
} else {
console.log(chalk.green('%s'), 'No broken links found')
}
})
})
@@ -0,0 +1,24 @@
version: 2
jobs:
build:
docker:
- image: circleci/node:12

working_directory: ~/repo

steps:
- checkout

- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
- v1-dependencies-

- run: npm install

- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}

- run: npm test

0 comments on commit 5fc1ffe

Please sign in to comment.
You can’t perform that action at this time.