Skip to content

Commit 26afb81

Browse files
authored
feat: run Node.js markdown formatter on markdown output (#98)
To increase the probability of producing markdown that will pass the Node.js linter, use the Node.js linter to format markdown output.
1 parent eaeb938 commit 26afb81

File tree

4 files changed

+12008
-592
lines changed

4 files changed

+12008
-592
lines changed

changelog-maker.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const pkg = require('./package.json')
1818
const debug = require('debug')(pkg.name)
1919
const argv = require('minimist')(process.argv.slice(2))
2020

21+
// Skip on formatting on Node.js 10.
22+
const formatMarkdown = process.versions.node.startsWith('10.') ? false : import('./format.mjs')
23+
2124
const quiet = argv.quiet || argv.q
2225
const help = argv.h || argv.help
2326
const commitUrl = argv['commit-url'] || 'https://github.com/{ghUser}/{ghRepo}/commit/{ref}'
@@ -102,14 +105,13 @@ function organiseCommits (list) {
102105
})
103106
}
104107

105-
function printCommits (list) {
106-
let out = `${list.join('\n')}\n`
107-
108-
if (!process.stdout.isTTY) {
109-
out = stripAnsi(out)
108+
async function printCommits (list) {
109+
for await (let commit of list) {
110+
if (!process.stdout.isTTY) {
111+
commit = stripAnsi(commit)
112+
}
113+
process.stdout.write(commit)
110114
}
111-
112-
process.stdout.write(out)
113115
}
114116

115117
function onCommitList (err, list) {
@@ -142,10 +144,20 @@ function onCommitList (err, list) {
142144
formatted.push(commitToOutput(commit, formatType.PLAINTEXT, ghId, commitUrl))
143145
}
144146

145-
list = formatted
147+
list = formatted.map((line) => `${line}\n`)
146148
} else {
147-
list = list.map((commit) => {
148-
return commitToOutput(commit, format, ghId, commitUrl)
149+
list = list.map(async (commit) => {
150+
let output = commitToOutput(commit, format, ghId, commitUrl)
151+
if (format === formatType.MARKDOWN) {
152+
if (!process.stdout.isTTY) {
153+
output = stripAnsi(output)
154+
}
155+
if (process.versions.node.startsWith('10.')) {
156+
return `${output}\n`
157+
}
158+
return formatMarkdown.then((module) => module.default(output))
159+
}
160+
return `${output}\n`
149161
})
150162
}
151163

format.mjs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { remark } from 'remark'
2+
import remarkParse from 'remark-parse'
3+
import remarkStringify from 'remark-stringify'
4+
import presetLintNode from 'remark-preset-lint-node'
5+
6+
const formatter = remark()
7+
.use(remarkParse)
8+
.use(presetLintNode)
9+
.use(remarkStringify)
10+
11+
const format = async (markdown) => {
12+
const result = await formatter.process(markdown)
13+
return result.toString()
14+
}
15+
16+
export default format

0 commit comments

Comments
 (0)