Skip to content

Commit

Permalink
Better debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Nov 2, 2022
1 parent b745991 commit c9fc051
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
35 changes: 16 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,10 @@ const set = require('dset').dset
const { marked } = require('marked')
const expandWildcardKeypaths = require('./expand-wildcard-keypath')

/**
* Check if a `file` is markdown
* @param {String} filePath
* @return {Boolean}
*/
function isMarkdown(filePath) {
return /\.md$|\.markdown$/.test(extname(filePath))
}

function render(data, key, options, debug) {
function render(data, key, options) {
const value = get(data, key)
if (typeof value === 'string') {
set(data, key, marked(value, options))
debug('rendered "%s"', key.join ? key.join('.') : key)
}
}

Expand Down Expand Up @@ -45,29 +35,36 @@ function initMarkdown(options = defaultOptions) {
}

return function markdown(files, metalsmith, done) {
const debug = metalsmith.debug('@metalsmith/markdown')
setImmediate(done)

Object.keys(files).forEach(function (file) {
debug('checking file: %s', file)
if (!isMarkdown(file)) {
return
}
const debug = metalsmith.debug('@metalsmith/markdown')
const matches = metalsmith.match('**/*.{md,markdown}', Object.keys(files))

debug('Running with options: %O', options)
if (matches.length === 0) {
debug.warn('No markdown files found.')
} else {
debug('Processing %s markdown file(s)', matches.length)
}

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

debug('converting file: %s', file)
debug.info('Rendering file "%s" as "%s"', file, html)
const str = marked(data.contents.toString(), options)
data.contents = Buffer.from(str)

let keys = options.keys
if (options.wildcard) {
keys = expandWildcardKeypaths(data, options.keys, '*')
}
keys.forEach((k) => render(data, k, options, debug))
keys.forEach((k) => {
debug.info('Rendering key "%s" of file "%s"', k.join ? k.join('.') : k, file)
render(data, k, options)
})

delete files[file]
files[html] = data
Expand Down
27 changes: 13 additions & 14 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const markdown = require('..')
const expandWildcardKeypath = require('../lib/expand-wildcard-keypath')
const path = require('path')

function msCommon(dir) {
return Metalsmith(dir).env('DEBUG', process.env.DEBUG)
}

describe('@metalsmith/markdown', function () {
it('should export a named plugin function matching package.json name', function () {
const namechars = name.split('/')[1]
Expand All @@ -19,8 +23,7 @@ describe('@metalsmith/markdown', function () {
})

it('should not crash the metalsmith build when using default options', function (done) {
Metalsmith('test/fixtures/default')
.env('DEBUG', process.env.DEBUG)
msCommon('test/fixtures/default')
.use(markdown())
.build((err) => {
assert.strictEqual(err, null)
Expand All @@ -42,19 +45,19 @@ describe('@metalsmith/markdown', function () {
Promise.all([
new Promise((resolve) => {
const files = getFiles()
markdown(true)(files, Metalsmith(__dirname), () => {
markdown(true)(files, msCommon(__dirname), () => {
resolve(files)
})
}),
new Promise((resolve) => {
const files = getFiles()
markdown()(files, Metalsmith(__dirname), () => {
markdown()(files, msCommon(__dirname), () => {
resolve(files)
})
}),
new Promise((resolve) => {
const files = getFiles()
markdown({ smartypants: true })(files, Metalsmith(__dirname), () => {
markdown({ smartypants: true })(files, msCommon(__dirname), () => {
resolve(files)
})
})
Expand All @@ -70,8 +73,7 @@ describe('@metalsmith/markdown', function () {
})

it('should convert markdown files', function (done) {
Metalsmith('test/fixtures/basic')
.env('DEBUG', process.env.DEBUG)
msCommon('test/fixtures/basic')
.use(
markdown({
smartypants: true
Expand All @@ -86,15 +88,14 @@ describe('@metalsmith/markdown', function () {

it('should skip non-markdown files', function (done) {
const files = { 'index.css': {} }
markdown(true)(files, Metalsmith(__dirname), () => {
markdown(true)(files, msCommon(__dirname), () => {
assert.deepStrictEqual(files, { 'index.css': {} })
done()
})
})

it('should allow a "keys" option', function (done) {
Metalsmith('test/fixtures/keys')
.env('DEBUG', process.env.DEBUG)
msCommon('test/fixtures/keys')
.use(
markdown({
keys: ['custom'],
Expand All @@ -109,8 +110,7 @@ describe('@metalsmith/markdown', function () {
})

it('should parse nested key paths', function (done) {
Metalsmith('test/fixtures/nested-keys')
.env('DEBUG', process.env.DEBUG)
msCommon('test/fixtures/nested-keys')
.use(
markdown({
keys: ['custom', 'nested.key.path'],
Expand Down Expand Up @@ -143,8 +143,7 @@ describe('@metalsmith/markdown', function () {
})

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

0 comments on commit c9fc051

Please sign in to comment.