Skip to content

Commit 5652c26

Browse files
committed
Resolves #62, deprecates markdown options.<option> in favor of options.engineOptions.<option>
1 parent d90437f commit 5652c26

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

lib/index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { marked } from 'marked';
22
import Metalsmith from 'metalsmith';
33

44
export default markdown;
5-
export interface Options extends marked.MarkedOptions {
5+
export interface Options {
66
/**
77
* - Key names of file metadata to render to HTML - can be nested
88
*/
@@ -11,6 +11,10 @@ export interface Options extends marked.MarkedOptions {
1111
* - Expand `*` wildcards in keypaths
1212
*/
1313
wildcard?: boolean;
14+
/**
15+
* Marked markdown [options](https://marked.js.org/using_advanced#options)
16+
*/
17+
engineOptions?: marked.MarkedOptions
1418
};
1519
/**
1620
* A Metalsmith plugin to render markdown files to HTML

src/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ function render(data, key, options) {
1515
* @typedef Options
1616
* @property {string[]} [keys] - Key names of file metadata to render to HTML - can be nested
1717
* @property {boolean} [wildcard=false] - Expand `*` wildcards in keypaths
18+
* @property {Object} [engineOptions] Options to pass to the markdown engine (default [marked](https://github.com/markedjs/marked))
1819
**/
1920

2021
const defaultOptions = {
2122
keys: [],
22-
wildcard: false
23+
wildcard: false,
24+
engineOptions: {}
2325
}
2426

2527
/**
@@ -40,6 +42,15 @@ function markdown(options = defaultOptions) {
4042
const debug = metalsmith.debug('@metalsmith/markdown')
4143
const matches = metalsmith.match('**/*.{md,markdown}', Object.keys(files))
4244

45+
const legacyEngineOptions = Object.keys(options).filter((opt) => !Object.keys(defaultOptions).includes(opt))
46+
if (legacyEngineOptions.length) {
47+
debug.warn('Starting from version 2.0 marked engine options will need to be specified as options.engineOptions')
48+
legacyEngineOptions.forEach((opt) => {
49+
options.engineOptions[opt] = options[opt]
50+
})
51+
debug.warn('Moved engine options %s to options.engineOptions', legacyEngineOptions.join(', '))
52+
}
53+
4354
debug('Running with options: %O', options)
4455
if (matches.length === 0) {
4556
debug.warn('No markdown files found.')
@@ -54,7 +65,7 @@ function markdown(options = defaultOptions) {
5465
if ('.' != dir) html = join(dir, html)
5566

5667
debug.info('Rendering file "%s" as "%s"', file, html)
57-
const str = marked(data.contents.toString(), options)
68+
const str = marked(data.contents.toString(), options.engineOptions)
5869
data.contents = Buffer.from(str)
5970

6071
let keys = options.keys
@@ -63,7 +74,7 @@ function markdown(options = defaultOptions) {
6374
}
6475
keys.forEach((k) => {
6576
debug.info('Rendering key "%s" of file "%s"', k.join ? k.join('.') : k, file)
66-
render(data, k, options)
77+
render(data, k, options.engineOptions)
6778
})
6879

6980
delete files[file]

test/index.cjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,40 @@ describe('@metalsmith/markdown', function () {
131131
})
132132
})
133133

134+
it('< v2.0.0 should move legacy engine options in object root to options.engineOptions', done => {
135+
const ms = msCommon('test/fixtures/basic')
136+
const output = []
137+
const Debugger = (...args) => { output.push(['log', ...args])}
138+
Object.assign(Debugger, {
139+
info: (...args) => { output.push(['info', ...args]) },
140+
warn: (...args) => { output.push(['warn', ...args]) },
141+
error: (...args) => { output.push(['error', ...args]) }
142+
})
143+
144+
ms
145+
.use(() => {
146+
ms.debug = () => Debugger
147+
})
148+
.use(markdown({
149+
gfm: true,
150+
smartypants: false,
151+
engineOptions: {}
152+
}))
153+
.process((err) => {
154+
if (err) done(err)
155+
try {
156+
assert.deepStrictEqual(output.slice(0,2), [
157+
['warn', 'Starting from version 2.0 marked engine options will need to be specified as options.engineOptions'],
158+
['warn', 'Moved engine options %s to options.engineOptions', 'gfm, smartypants']
159+
])
160+
done()
161+
} catch (err) {
162+
done(err)
163+
}
164+
})
165+
166+
})
167+
134168
it('expandWildCardKeyPath should throw if root is not an object', function () {
135169
try {
136170
expandWildcardKeypath(null, [], '*')

0 commit comments

Comments
 (0)