Skip to content

Commit c9fc051

Browse files
committed
Better debug logging
1 parent b745991 commit c9fc051

File tree

2 files changed

+29
-33
lines changed

2 files changed

+29
-33
lines changed

lib/index.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,10 @@ const set = require('dset').dset
44
const { marked } = require('marked')
55
const expandWildcardKeypaths = require('./expand-wildcard-keypath')
66

7-
/**
8-
* Check if a `file` is markdown
9-
* @param {String} filePath
10-
* @return {Boolean}
11-
*/
12-
function isMarkdown(filePath) {
13-
return /\.md$|\.markdown$/.test(extname(filePath))
14-
}
15-
16-
function render(data, key, options, debug) {
7+
function render(data, key, options) {
178
const value = get(data, key)
189
if (typeof value === 'string') {
1910
set(data, key, marked(value, options))
20-
debug('rendered "%s"', key.join ? key.join('.') : key)
2111
}
2212
}
2313

@@ -45,29 +35,36 @@ function initMarkdown(options = defaultOptions) {
4535
}
4636

4737
return function markdown(files, metalsmith, done) {
48-
const debug = metalsmith.debug('@metalsmith/markdown')
4938
setImmediate(done)
5039

51-
Object.keys(files).forEach(function (file) {
52-
debug('checking file: %s', file)
53-
if (!isMarkdown(file)) {
54-
return
55-
}
40+
const debug = metalsmith.debug('@metalsmith/markdown')
41+
const matches = metalsmith.match('**/*.{md,markdown}', Object.keys(files))
42+
43+
debug('Running with options: %O', options)
44+
if (matches.length === 0) {
45+
debug.warn('No markdown files found.')
46+
} else {
47+
debug('Processing %s markdown file(s)', matches.length)
48+
}
5649

50+
matches.forEach(function (file) {
5751
const data = files[file]
5852
const dir = dirname(file)
5953
let html = basename(file, extname(file)) + '.html'
6054
if ('.' != dir) html = join(dir, html)
6155

62-
debug('converting file: %s', file)
56+
debug.info('Rendering file "%s" as "%s"', file, html)
6357
const str = marked(data.contents.toString(), options)
6458
data.contents = Buffer.from(str)
6559

6660
let keys = options.keys
6761
if (options.wildcard) {
6862
keys = expandWildcardKeypaths(data, options.keys, '*')
6963
}
70-
keys.forEach((k) => render(data, k, options, debug))
64+
keys.forEach((k) => {
65+
debug.info('Rendering key "%s" of file "%s"', k.join ? k.join('.') : k, file)
66+
render(data, k, options)
67+
})
7168

7269
delete files[file]
7370
files[html] = data

test/index.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const markdown = require('..')
88
const expandWildcardKeypath = require('../lib/expand-wildcard-keypath')
99
const path = require('path')
1010

11+
function msCommon(dir) {
12+
return Metalsmith(dir).env('DEBUG', process.env.DEBUG)
13+
}
14+
1115
describe('@metalsmith/markdown', function () {
1216
it('should export a named plugin function matching package.json name', function () {
1317
const namechars = name.split('/')[1]
@@ -19,8 +23,7 @@ describe('@metalsmith/markdown', function () {
1923
})
2024

2125
it('should not crash the metalsmith build when using default options', function (done) {
22-
Metalsmith('test/fixtures/default')
23-
.env('DEBUG', process.env.DEBUG)
26+
msCommon('test/fixtures/default')
2427
.use(markdown())
2528
.build((err) => {
2629
assert.strictEqual(err, null)
@@ -42,19 +45,19 @@ describe('@metalsmith/markdown', function () {
4245
Promise.all([
4346
new Promise((resolve) => {
4447
const files = getFiles()
45-
markdown(true)(files, Metalsmith(__dirname), () => {
48+
markdown(true)(files, msCommon(__dirname), () => {
4649
resolve(files)
4750
})
4851
}),
4952
new Promise((resolve) => {
5053
const files = getFiles()
51-
markdown()(files, Metalsmith(__dirname), () => {
54+
markdown()(files, msCommon(__dirname), () => {
5255
resolve(files)
5356
})
5457
}),
5558
new Promise((resolve) => {
5659
const files = getFiles()
57-
markdown({ smartypants: true })(files, Metalsmith(__dirname), () => {
60+
markdown({ smartypants: true })(files, msCommon(__dirname), () => {
5861
resolve(files)
5962
})
6063
})
@@ -70,8 +73,7 @@ describe('@metalsmith/markdown', function () {
7073
})
7174

7275
it('should convert markdown files', function (done) {
73-
Metalsmith('test/fixtures/basic')
74-
.env('DEBUG', process.env.DEBUG)
76+
msCommon('test/fixtures/basic')
7577
.use(
7678
markdown({
7779
smartypants: true
@@ -86,15 +88,14 @@ describe('@metalsmith/markdown', function () {
8688

8789
it('should skip non-markdown files', function (done) {
8890
const files = { 'index.css': {} }
89-
markdown(true)(files, Metalsmith(__dirname), () => {
91+
markdown(true)(files, msCommon(__dirname), () => {
9092
assert.deepStrictEqual(files, { 'index.css': {} })
9193
done()
9294
})
9395
})
9496

9597
it('should allow a "keys" option', function (done) {
96-
Metalsmith('test/fixtures/keys')
97-
.env('DEBUG', process.env.DEBUG)
98+
msCommon('test/fixtures/keys')
9899
.use(
99100
markdown({
100101
keys: ['custom'],
@@ -109,8 +110,7 @@ describe('@metalsmith/markdown', function () {
109110
})
110111

111112
it('should parse nested key paths', function (done) {
112-
Metalsmith('test/fixtures/nested-keys')
113-
.env('DEBUG', process.env.DEBUG)
113+
msCommon('test/fixtures/nested-keys')
114114
.use(
115115
markdown({
116116
keys: ['custom', 'nested.key.path'],
@@ -143,8 +143,7 @@ describe('@metalsmith/markdown', function () {
143143
})
144144

145145
it('should recognize a keys option loop placeholder', function (done) {
146-
Metalsmith('test/fixtures/array-index-keys')
147-
.env('DEBUG', process.env.DEBUG)
146+
msCommon('test/fixtures/array-index-keys')
148147
.use(
149148
markdown({
150149
keys: ['arr.*', 'objarr.*.prop', 'wildcard.faq.*.*', 'wildcard.titles.*'],

0 commit comments

Comments
 (0)