diff --git a/readme.md b/readme.md index 8a0e622..6ca40b8 100644 --- a/readme.md +++ b/readme.md @@ -71,7 +71,7 @@ Now, running `node example` yields: Add links to headings. -#### `options.behaviour` +#### `options.behavior` How to create links (`string`, default: `'prepend'`). Pass `'prepend'` to inject the link before the heading text, `'append'` for a diff --git a/src/__tests__/index.js b/src/__tests__/index.js index d27a55e..5c47c4c 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -7,11 +7,25 @@ import html from 'remark-html' import headings from '..' const base = file => read(join(__dirname, 'fixtures', file), 'utf-8') +const behaviors = ['append', 'prepend', 'wrap'] test('should autolink headings', t => { - const behaviours = ['append', 'prepend', 'wrap'] + behaviors.forEach(b => { + t.is( + remark() + .use(slug) + .use(html) + .use(headings, {behavior: b}) + .processSync(base('input.md')) + .toString(), + base(`output.${b}.html`), + `should ${b} headings` + ) + }) +}) - behaviours.forEach(b => { +test('should autolink headings with deprecated option', t => { + behaviors.forEach(b => { t.is( remark() .use(slug) diff --git a/src/index.js b/src/index.js index bd8edc0..a89133d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ import visit from 'unist-util-visit' import extend from 'extend' -const behaviours = {prepend: 'unshift', append: 'push'} +const behaviors = {prepend: 'unshift', append: 'push'} const contentDefaults = { type: 'element', @@ -9,14 +9,27 @@ const contentDefaults = { properties: {className: ['icon', 'icon-link']} } -const defaults = {behaviour: 'prepend', content: contentDefaults} +const defaults = {behavior: 'prepend', content: contentDefaults} + +let deprecationWarningIssued = false export default function attacher(opts = {}) { - let {linkProperties, behaviour, content} = {...defaults, ...opts} + let {linkProperties, behavior, content} = {...defaults, ...opts} let method let hChildren - if (behaviour === 'wrap') { + // NOTE: Remove in next major version + if (opts.behaviour !== undefined) { + if (!deprecationWarningIssued) { + deprecationWarningIssued = true + console.warn( + 'Deprecation Warning: `behaviour` is a nonstandard option. Use `behavior` instead.' + ) + } + behavior = opts.behaviour + } + + if (behavior === 'wrap') { method = wrap } else { method = inject @@ -39,7 +52,7 @@ export default function attacher(opts = {}) { } function inject(node, url) { - node.children[behaviours[behaviour]]({ + node.children[behaviors[behavior]]({ type: 'link', url, title: null,