Created
September 17, 2016 20:02
-
-
Save pfrazee/867f6b596782ffea0de9d4d81ed02b8d to your computer and use it in GitHub Desktop.
Quick link-checker script for Jekyll sites
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var linkCheck = require('link-check'); | |
var jetpack = require('fs-jetpack'); | |
var jsdom = require('jsdom'); | |
function htmlLinkCheck (file) { | |
jsdom.env(file.text, (err, window) => { | |
if (err) | |
throw err | |
Array.from(window.document.getElementsByTagName('a')).forEach(el => { | |
var link = el.getAttribute('href') | |
linkCheck(link, { baseUrl: 'http://localhost:4000/' }, (err, res) => { | |
if (err || res.statusCode !== 200) { | |
console.log(res.statusCode + ' ' + file.path + ' ' +link) | |
} | |
}) | |
}) | |
window.close() | |
}) | |
} | |
var siteDir = jetpack.cwd(__dirname).cwd('_site') | |
var sitefilePaths = siteDir.find({ matching: '*.html' }) | |
var siteFiles = sitefilePaths.map(p => { return { path: p, text: siteDir.read(p, 'utf8') } }) | |
siteFiles.forEach(htmlLinkCheck) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run
bundle exec jekyll serve
, then in another console runnode link-checker.js
. It will report any bad links you got.