diff --git a/README.md b/README.md index 862e878..e4f352f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/index.es6.js b/index.es6.js index e6be528..c9a9095 100644 --- a/index.es6.js +++ b/index.es6.js @@ -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}) + } }) }) diff --git a/test.js b/test.js index 49e3572..82b1010 100644 --- a/test.js +++ b/test.js @@ -46,3 +46,16 @@ equal( md().use(anchor, { level: 2, permalink: true }).render('# H1\n\n## H2'), '