Skip to content

Commit

Permalink
Merge pull request #16 from tschaub/callback
Browse files Browse the repository at this point in the history
Add a callback option
  • Loading branch information
valeriangalliat committed Feb 12, 2016
2 parents f93ae8b + b2bc1ee commit 47a652d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ See a [demo as JSFiddle](https://jsfiddle.net/9ukc8dy6/).

The `opts` object can contain:

Name | Description | Default
------------------|-------------------------------------------|------------------------------------
`level` | Minimum level to apply anchors on. | 1
`slugify` | A custom slugification function. | [string.js' `slugify`][slugify]
`permalink` | Whether to add permalinks next to titles. | `false`
`renderPermalink` | A custom permalink rendering function. | See [`index.es6.js`](index.es6.js)
`permalinkClass` | The class of the permalink anchor. | `header-anchor`
`permalinkSymbol` | The symbol in the permalink anchor. | ``
`permalinkBefore` | Place the permalink before the title. | `false`
Name | Description | Default
------------------|---------------------------------------------|------------------------------------
`level` | Minimum level to apply anchors on. | 1
`slugify` | A custom slugification function. | [string.js' `slugify`][slugify]
`permalink` | Whether to add permalinks next to titles. | `false`
`renderPermalink` | A custom permalink rendering function. | See [`index.es6.js`](index.es6.js)
`permalinkClass` | The class of the permalink anchor. | `header-anchor`
`permalinkSymbol` | The symbol in the permalink anchor. | ``
`permalinkBefore` | Place the permalink before the title. | `false`
`callback` | Called with token and info after rendering. | `undefined`

[slugify]: http://stringjs.com/#methods/slugify

Expand All @@ -38,3 +39,5 @@ the header itself.

You may want to use the [link symbol](http://graphemica.com/🔗) as
`permalinkSymbol`, or a symbol from your favorite web font.

The `callback` option is a function that will be called at the end of rendering with the `token` and an `info` object. The `info` object has `title` and `slug` properties with the token content and the slug used for the identifier.
4 changes: 4 additions & 0 deletions index.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ const anchor = (md, opts) => {
if (opts.permalink) {
opts.renderPermalink(slug, opts, state, tokens.indexOf(token))
}

if (opts.callback) {
opts.callback(token, {slug, title})
}
})
})

Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,16 @@ equal(
md().use(anchor, { level: 2, permalink: true }).render('# H1\n\n## H2'),
'<h1>H1</h1>\n<h2 id="h2">H2 <a class="header-anchor" href="#h2" aria-hidden="true">¶</a></h2>\n'
)

const calls = []
equal(
md().use(anchor, { callback: (token, info) => { calls.push({ token, info }) } }).render('# First Heading\n\n## Second Heading'),
'<h1 id="first-heading">First Heading</h1>\n<h2 id="second-heading">Second Heading</h2>\n'
)
equal(calls.length, 2)
equal(calls[0].token.tag, 'h1')
equal(calls[0].info.title, 'First Heading')
equal(calls[0].info.slug, 'first-heading')
equal(calls[1].token.tag, 'h2')
equal(calls[1].info.title, 'Second Heading')
equal(calls[1].info.slug, 'second-heading')

0 comments on commit 47a652d

Please sign in to comment.