Skip to content

Commit 2c97dd4

Browse files
Support individual header level selection.
Closes valeriangalliat#27.
1 parent 8aecd75 commit 2c97dd4

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
* Drop Babel. This drops support for Node.js versions that doesn't
77
support ES6.
88
* Support code in titles. ([#27])
9+
* Support individual header level selection. ([#27])
910

1011
## [3.0.0] - 2017-02-06
1112
* Use existing ID as slug if present. This drops the support for

README.md

+17-13
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ See a [demo as JSFiddle](https://jsfiddle.net/9ukc8dy6/).
1616

1717
The `opts` object can contain:
1818

19-
Name | Description | Default
20-
------------------|-----------------------------------------------|------------------------------------
21-
`level` | Minimum level to apply anchors on. | 1
22-
`slugify` | A custom slugification function. | [string.js' `slugify`][slugify]
23-
`permalink` | Whether to add permalinks next to titles. | `false`
24-
`renderPermalink` | A custom permalink rendering function. | See [`index.es6.js`](index.es6.js)
25-
`permalinkClass` | The class of the permalink anchor. | `header-anchor`
26-
`permalinkSymbol` | The symbol in the permalink anchor. | ``
27-
`permalinkBefore` | Place the permalink before the title. | `false`
28-
`permalinkHref` | A custom permalink `href` rendering function. | See [`index.es6.js`](index.es6.js)
29-
`callback` | Called with token and info after rendering. | `undefined`
19+
Name | Description | Default
20+
------------------|----------------------------------------------------------------|-----------------------------------
21+
`level` | Minimum level to apply anchors on or array of selected levels. | 1
22+
`slugify` | A custom slugification function. | [string.js' `slugify`][slugify]
23+
`permalink` | Whether to add permalinks next to titles. | `false`
24+
`renderPermalink` | A custom permalink rendering function. | See [`index.es6.js`](index.es6.js)
25+
`permalinkClass` | The class of the permalink anchor. | `header-anchor`
26+
`permalinkSymbol` | The symbol in the permalink anchor. | ``
27+
`permalinkBefore` | Place the permalink before the title. | `false`
28+
`permalinkHref` | A custom permalink `href` rendering function. | See [`index.es6.js`](index.es6.js)
29+
`callback` | Called with token and info after rendering. | `undefined`
3030

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

@@ -35,8 +35,12 @@ the above options, and then all the usual markdown-it rendering
3535
arguments.
3636

3737
All headers above `level` will then have an `id` attribute with a slug
38-
of their content, and if `permalink` is `true`, a `` symbol linking to
39-
the header itself.
38+
of their content. `level` can also be an array of headers levels to
39+
apply the anchor, like `[2, 3]` to have an anchor on only level 2 and
40+
3 headers.
41+
42+
If `permalink` is `true`, a `` symbol linking to the header itself will
43+
be added.
4044

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

index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,23 @@ const uniqueSlug = (slug, slugs) => {
4747
return slug + '-' + slugs[slug]
4848
}
4949

50+
const isLevelSelectedNumber = selection => level => level >= selection
51+
const isLevelSelectedArray = selection => level => selection.includes(level)
52+
5053
const anchor = (md, opts) => {
5154
opts = Object.assign({}, anchor.defaults, opts)
5255

5356
md.core.ruler.push('anchor', state => {
5457
const slugs = {}
5558
const tokens = state.tokens
5659

60+
const isLevelSelected = Array.isArray(opts.level)
61+
? isLevelSelectedArray(opts.level)
62+
: isLevelSelectedNumber(opts.level)
63+
5764
tokens
5865
.filter(token => token.type === 'heading_open')
59-
.filter(token => token.tag.substr(1) >= opts.level)
66+
.filter(token => isLevelSelected(Number(token.tag.substr(1))))
6067
.forEach(token => {
6168
// Aggregate the next token children text.
6269
const title = tokens[tokens.indexOf(token) + 1].children

test.js

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ equal(
1818
'<h1>H1</h1>\n<h2 id="h2">H2</h2>\n'
1919
)
2020

21+
equal(
22+
md().use(anchor, { level: [2, 4] }).render('# H1\n\n## H2\n\n### H3\n\n#### H4\n\n##### H5'),
23+
'<h1>H1</h1>\n<h2 id="h2">H2</h2>\n<h3>H3</h3>\n<h4 id="h4">H4</h4>\n<h5>H5</h5>\n'
24+
)
25+
2126
equal(
2227
md().use(anchor, { permalink: true }).render('# H1'),
2328
'<h1 id="h1">H1 <a class="header-anchor" href="#h1" aria-hidden="true">¶</a></h1>\n'

0 commit comments

Comments
 (0)