Skip to content

Commit

Permalink
Restore previous repo behavior of remark-validate-links (#32)
Browse files Browse the repository at this point in the history
Since 9.0.0, remark-validate-links tries to get the repo url from
git remotes (which is not desirable for forks) rather than the
package.json. Hallmark now reads package.json itself and passes
the repo url to remark-validate-links.
  • Loading branch information
vweevers committed Aug 9, 2019
1 parent 7e1b2eb commit 48c7abc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
25 changes: 19 additions & 6 deletions cli.js
Expand Up @@ -27,22 +27,35 @@ if (argv.help) {
} else {
const deglob = require('deglob')
const engine = require('unified-engine')
const pkgConfig = require('pkg-config')
const options = require('./options')

const cwd = process.cwd()
const glob = argv._.length ? argv._ : ['*.md']
const configKey = 'hallmark'
const packageOpts = pkgConfig(configKey, { root: false, cwd }) || {}
const ignore = [].concat(argv.ignore || [])
const pkg = getNearestPackage(cwd) || {}
const packageOpts = pkg.hallmark || {}
const repo = pkg.repository ? pkg.repository.url || pkg.repository : ''
const ignore = [].concat(packageOpts.ignore || []).concat(argv.ignore || [])

deglob(glob, { configKey, cwd, ignore }, function (err, files) {
deglob(glob, { usePackageJson: false, cwd, ignore }, function (err, files) {
if (err) throw err
if (files.length === 0) process.exit()

engine(options(argv, packageOpts, files, cwd), function (err, code) {
engine(options(argv, packageOpts, files, cwd, repo), function (err, code) {
if (err) throw err
process.exit(code)
})
})
}

function getNearestPackage (cwd) {
const findRoot = require('find-root')
const fs = require('fs')
const path = require('path')

try {
const fp = path.join(findRoot(cwd), 'package.json')
return JSON.parse(fs.readFileSync(fp, 'utf8'))
} catch (err) {

}
}
10 changes: 6 additions & 4 deletions lint.js
@@ -1,4 +1,4 @@
module.exports = function (fix, validateLinks) {
module.exports = function (fix, validateLinks, repo) {
const preset = {
plugins: [
require('remark-lint'),
Expand Down Expand Up @@ -44,9 +44,11 @@ module.exports = function (fix, validateLinks) {
// HTML anchors - as used in various Level readme's. Those readme's should be
// updated to use markdown only.
if (validateLinks) {
preset.plugins.push(
require('remark-validate-links')
)
preset.plugins.push([require('remark-validate-links'), {
// If we don't pass this, remark-validate-links tries to get the repo url
// from `git remote -v` which is not desirable for forks.
repository: repo || false
}])
}

return preset
Expand Down
4 changes: 2 additions & 2 deletions options.js
Expand Up @@ -4,7 +4,7 @@ const color = require('supports-color').stdout
const extensions = require('markdown-extensions')
const processor = require('remark')

module.exports = function (argv, packageOpts, files, cwd) {
module.exports = function (argv, packageOpts, files, cwd, repo) {
let reporter
let reporterOptions

Expand Down Expand Up @@ -38,7 +38,7 @@ module.exports = function (argv, packageOpts, files, cwd) {
test: /^table of contents$/i,
summary: 'Click to expand'
}],
require('./lint')(argv.fix, packageOpts.validateLinks !== false)
require('./lint')(argv.fix, packageOpts.validateLinks !== false, repo)
],
settings: {
// One style for code blocks, whether they have a language or not.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -11,8 +11,8 @@
},
"dependencies": {
"deglob": "~4.0.0",
"find-root": "~1.1.0",
"markdown-extensions": "~1.1.1",
"pkg-config": "~1.1.1",
"remark": "~11.0.0",
"remark-collapse": "~0.1.2",
"remark-git-contributors": "~2.0.0",
Expand Down

0 comments on commit 48c7abc

Please sign in to comment.