Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Add behavior option and fallback to behaviour
Browse files Browse the repository at this point in the history
Closes GH-46.
Closes GH-47.

Reviewed-by: Christian Murphy <[email protected]>
Reviewed-by: Titus Wormer <[email protected]>
  • Loading branch information
missmatsuko authored and wooorm committed Apr 19, 2019
1 parent e3cf54f commit 73a977f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 18 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
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',
tagName: 'span',
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
Expand All @@ -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,
Expand Down

0 comments on commit 73a977f

Please sign in to comment.