Skip to content

Commit

Permalink
Export programmatic API
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Oct 24, 2020
1 parent de5d280 commit 3dc1c87
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 137 deletions.
45 changes: 3 additions & 42 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,10 @@ if (argv.help) {
} else if (argv.version) {
console.log(require('./package.json').version)
} else {
const deglob = require('deglob')
const engine = require('unified-engine')
const options = require('./options')
argv.files = argv._

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

packageOpts.validateLinks = packageOpts.validateLinks !== false
packageOpts.paddedTable = packageOpts.paddedTable !== false
packageOpts.toc = packageOpts.toc !== false

deglob(glob, { usePackageJson: false, cwd, ignore }, function (err, files) {
require('./index.js')(argv, function (err, code) {
if (err) throw err
if (files.length === 0) process.exit()

engine(options(argv, pkg, packageOpts, files, cwd, repo), function (err, code) {
if (err) throw err
process.exit(code)
})
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) {

}
}

function originRepo (cwd) {
// Don't pass cwd for now (jonschlinkert/parse-git-config#13)
const origin = require('remote-origin-url').sync(/* cwd */)
const ghurl = require('github-url-from-git')

return origin ? ghurl(origin) : null
}
137 changes: 137 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
'use strict'

const deglob = require('deglob')
const closest = require('read-closest-package')
const engine = require('unified-engine')
const color = require('supports-color').stdout
const processor = require('remark')

module.exports = function hallmark (options, callback) {
if (typeof options === 'function') {
return hallmark({}, options)
}

const fix = !!options.fix
const cwd = options.cwd || process.cwd()
const pkg = closest.sync({ cwd }) || {}
const packageOpts = pkg.hallmark || {}
const globs = options.files && options.files.length ? options.files : ['*.md']
const repository = repo(options.repository) || repo(packageOpts.repository) || repo(pkg.repository) || originRepo(cwd) || ''
const ignore = concat('ignore', packageOpts, options)

deglob(globs, { usePackageJson: false, cwd, ignore }, function (err, files) {
if (err) throw err

if (!files.length) {
callback(null, 0)
return
}

let reporter
let reporterOptions

if (options.report) {
// Only take one --report option
reporter = [].concat(options.report)[0]

if (typeof reporter !== 'string') {
// Reporter was specified with subarg syntax
reporterOptions = reporter
reporter = reporter._[0]
}
}

const paddedTable = packageOpts.paddedTable !== false
const validateLinks = packageOpts.validateLinks !== false
const toc = packageOpts.toc !== false

const contributors = 'contributors' in packageOpts
? packageOpts.contributors
: packageOpts.community

const changelogOptions = Object.assign({}, packageOpts.changelog, options.changelog)
const plugins = { plugins: concat('plugins', packageOpts, options) }
const fixers = { plugins: concat('fixers', packageOpts, options) }

engine({
processor,
extensions: [
// TODO (next major): only support md and markdown
'md',
'markdown',
'mdown',
'mkdn',
'mkd',
'mdwn',
'mkdown'
],
color,
files,
cwd,
reporter,
reporterOptions,
plugins: [
// Skip updating contributors table in lint mode
fix && contributors !== false && options.contributors !== false
? [require('remark-git-contributors'), {
contributors: contributors || null
}]
: null,

[require('remark-changelog'), { cwd, fix, pkg, repository, ...changelogOptions }],
[require('remark-github'), { repository }],

// TODO: https://github.com/vweevers/hallmark/issues/36
toc ? [require('remark-toc'), { tight: true }] : null,
toc ? [require('remark-collapse'), collapseToc()] : null,

fix ? fixers : null,
require('./lint.js')({ fix, repository, paddedTable, validateLinks }),
plugins
].filter(Boolean),
settings: {
// One style for code blocks, whether they have a language or not.
fences: true,
listItemIndent: '1',

// Allow disabling padding because on big tables it creates noise.
tablePipeAlign: paddedTable,

// In addition, use fixed width columns.
stringLength: paddedTable ? (s) => String(s).length : () => 3
},
pluginPrefix: 'remark',
// "Whether to write successfully processed files"
output: fix,
// "Whether to write the processed file to streamOut"
out: false,
// "Call back with an unsuccessful (1) code on warnings as well as errors"
frail: true,
// "Do not report successful files"
quiet: true
}, callback)
})
}

function repo (repository) {
return repository ? repository.url || repository : null
}

function concat (key, packageOpts, options) {
return [].concat(packageOpts[key] || []).concat(options[key] || [])
}

function collapseToc () {
return {
test: /^table of contents$/i,
summary: 'Click to expand'
}
}

function originRepo (cwd) {
// Don't pass cwd for now (jonschlinkert/parse-git-config#13)
const origin = require('remote-origin-url').sync(/* cwd */)
const ghurl = require('github-url-from-git')

return origin ? ghurl(origin) : null
}
6 changes: 3 additions & 3 deletions lint.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function (fix, cwd, packageOpts, repository) {
module.exports = function ({ fix, repository, paddedTable, validateLinks }) {
const preset = {
plugins: [
require('remark-lint'),
Expand Down Expand Up @@ -30,7 +30,7 @@ module.exports = function (fix, cwd, packageOpts, repository) {
[require('remark-lint-code-block-style'), 'fenced'],

// TODO: support fixed-width columns (https://github.com/remarkjs/remark-lint/issues/217)
packageOpts.paddedTable ? [require('remark-lint-table-cell-padding'), 'padded'] : null,
paddedTable ? [require('remark-lint-table-cell-padding'), 'padded'] : null,

require('remark-lint-table-pipes'),
[require('remark-lint-checkbox-character-style'), {
Expand All @@ -43,7 +43,7 @@ module.exports = function (fix, cwd, packageOpts, repository) {
// Temporarily allow skipping link validation, because remark does not parse
// HTML anchors - as used in various Level readme's. Those readme's should be
// updated to use markdown only.
if (packageOpts.validateLinks) {
if (validateLinks) {
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.
Expand Down
86 changes: 0 additions & 86 deletions options.js

This file was deleted.

19 changes: 13 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@
"author": "Vincent Weevers",
"license": "GPL-3.0",
"bin": "cli.js",
"main": "index.js",
"scripts": {
"test": "standard && node cli.js && node test.js",
"test": "standard && depcheck --ignores level-community && node cli.js && node test.js",
"hallmark": "node cli.js --fix",
"check-licenses": "npm-consider install --test --production"
},
"files": [
"CHANGELOG.md",
"cli.js",
"index.js",
"lint.js",
"USAGE"
],
"dependencies": {
"deglob": "^4.0.0",
"find-root": "^1.1.0",
"github-url-from-git": "^1.5.0",
"markdown-extensions": "^1.1.1",
"read-closest-package": "^1.2.0",
"remark": "^12.0.1",
"remark-changelog": "^1.0.0",
"remark-changelog": "^1.2.0",
"remark-collapse": "~0.1.2",
"remark-git-contributors": "^3.0.0",
"remark-github": "^9.0.1",
Expand Down Expand Up @@ -48,13 +55,13 @@
"unified-engine": "^8.0.0"
},
"devDependencies": {
"depcheck": "^1.2.0",
"git-pull-or-clone": "^2.0.1",
"level-community": "^3.0.0",
"npm-consider": "^1.7.0",
"rimraf": "^3.0.0",
"standard": "^15.0.0",
"tape": "^5.0.1",
"vfile-reporter-json": "^2.0.0"
"tape": "^5.0.1"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 3dc1c87

Please sign in to comment.