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

Commit

Permalink
Add support for a content function
Browse files Browse the repository at this point in the history
Related to GH-49.
  • Loading branch information
wooorm committed Mar 23, 2020
1 parent 2f56170 commit 56dbca9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
23 changes: 20 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const html = require('remark-html')
const slug = require('remark-slug')
const headings = require('remark-autolink-headings')

const contents = unified()
const doc = unified()
.use(markdown)
.use(slug)
// Note that this module must be included after `remark-slug`.
Expand All @@ -54,7 +54,7 @@ const contents = unified()
.processSync(fs.readFileSync('example.md'))
.toString()

console.log(contents)
console.log(doc)
```

Now, running `node example` yields:
Expand Down Expand Up @@ -86,7 +86,7 @@ option.

###### `options.content`

[**hast**][hast] nodes to insert in the link (`Node|Children`).
[**hast**][hast] nodes to insert in the link (`Function|Node|Children`).
By default, the following is used:

```js
Expand All @@ -97,6 +97,23 @@ By default, the following is used:
}
```

If `content` is a function, it’s called with the current heading (`Node`) and
should return one or more nodes:

```js
const toString = require('mdast-util-to-string')
const h = require('hastscript')

//

function content(node) {
return [
h('span.visually-hidden', 'Read the “', toString(node), '” section'),
h('span.icon.icon-link', {ariaHidden: true})
]
}
```

###### `options.linkProperties`

Extra properties to set on the link (`Object?`).
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ test('should accept custom content as an array', (t) => {
)
})

test('should accept custom content as a function', (t) => {
t.is(
remark()
.use(slug)
.use(html)
.use(headings, {content, linkProperties: {}})
.processSync('# method')
.toString(),
'<h1 id="method"><a href="#method">Read the “method” section</a>method</h1>\n'
)

function content(node) {
return {
type: 'text',
value: 'Read the “' + node.children[0].value + '” section'
}
}
})

test('should accept link properties', (t) => {
t.is(
remark()
Expand Down
16 changes: 10 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ let deprecationWarningIssued = false
export default function attacher(options = {}) {
let {linkProperties, behavior, content} = {...defaults, ...options}
let method
let hChildren

// NOTE: Remove in next major version
if (options.behaviour !== undefined) {
Expand All @@ -34,7 +33,6 @@ export default function attacher(options = {}) {
method = wrap
} else {
method = inject
hChildren = Array.isArray(content) ? content : [content]

if (!linkProperties) {
linkProperties = {ariaHidden: 'true', tabIndex: -1}
Expand All @@ -53,15 +51,21 @@ export default function attacher(options = {}) {
}

function inject(node, url) {
const hProperties = extend(true, {}, linkProperties)
let hChildren = typeof content === 'function' ? content(node) : content

hChildren = Array.isArray(hChildren) ? hChildren : [hChildren]

if (typeof content !== 'function') {
hChildren = extend(true, [], hChildren)
}

node.children[behaviors[behavior]]({
type: 'link',
url,
title: null,
children: [],
data: {
hProperties: extend(true, {}, linkProperties),
hChildren: extend(true, [], hChildren)
}
data: {hProperties, hChildren}
})
}

Expand Down

0 comments on commit 56dbca9

Please sign in to comment.