Skip to content

Commit

Permalink
feat: run Node.js markdown formatter on markdown output (#98)
Browse files Browse the repository at this point in the history
To increase the probability of producing markdown that will pass the
Node.js linter, use the Node.js linter to format markdown output.
  • Loading branch information
Trott authored Oct 12, 2021
1 parent eaeb938 commit 26afb81
Show file tree
Hide file tree
Showing 4 changed files with 12,008 additions and 592 deletions.
32 changes: 22 additions & 10 deletions changelog-maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const pkg = require('./package.json')
const debug = require('debug')(pkg.name)
const argv = require('minimist')(process.argv.slice(2))

// Skip on formatting on Node.js 10.
const formatMarkdown = process.versions.node.startsWith('10.') ? false : import('./format.mjs')

const quiet = argv.quiet || argv.q
const help = argv.h || argv.help
const commitUrl = argv['commit-url'] || 'https://github.com/{ghUser}/{ghRepo}/commit/{ref}'
Expand Down Expand Up @@ -102,14 +105,13 @@ function organiseCommits (list) {
})
}

function printCommits (list) {
let out = `${list.join('\n')}\n`

if (!process.stdout.isTTY) {
out = stripAnsi(out)
async function printCommits (list) {
for await (let commit of list) {
if (!process.stdout.isTTY) {
commit = stripAnsi(commit)
}
process.stdout.write(commit)
}

process.stdout.write(out)
}

function onCommitList (err, list) {
Expand Down Expand Up @@ -142,10 +144,20 @@ function onCommitList (err, list) {
formatted.push(commitToOutput(commit, formatType.PLAINTEXT, ghId, commitUrl))
}

list = formatted
list = formatted.map((line) => `${line}\n`)
} else {
list = list.map((commit) => {
return commitToOutput(commit, format, ghId, commitUrl)
list = list.map(async (commit) => {
let output = commitToOutput(commit, format, ghId, commitUrl)
if (format === formatType.MARKDOWN) {
if (!process.stdout.isTTY) {
output = stripAnsi(output)
}
if (process.versions.node.startsWith('10.')) {
return `${output}\n`
}
return formatMarkdown.then((module) => module.default(output))
}
return `${output}\n`
})
}

Expand Down
16 changes: 16 additions & 0 deletions format.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { remark } from 'remark'
import remarkParse from 'remark-parse'
import remarkStringify from 'remark-stringify'
import presetLintNode from 'remark-preset-lint-node'

const formatter = remark()
.use(remarkParse)
.use(presetLintNode)
.use(remarkStringify)

const format = async (markdown) => {
const result = await formatter.process(markdown)
return result.toString()
}

export default format
Loading

0 comments on commit 26afb81

Please sign in to comment.