Skip to content

Commit 56d60b2

Browse files
authored
Add heading option to match custom headings
Closes GH-17.
1 parent 8f080d2 commit 56d60b2

6 files changed

Lines changed: 63 additions & 3 deletions

File tree

fixtures/match-heading/expected.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Example
2+
3+
## Mitwirkende
4+
5+
| Name | Website |
6+
| --------- | ----------------------- |
7+
| **Sara** | <https://example.com#1> |
8+
| **Alice** | <https://example.com#2> |

fixtures/match-heading/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Example
2+
3+
## Mitwirkende
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"contributors": [
3+
"Sara (https://example.com#1)",
4+
"Alice (https://example.com#2)"
5+
]
6+
}

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function contributors(options) {
1616
var align = settings.align || null
1717
var defaultContributors = settings.contributors
1818
var formatters = createFormatters(settings.formatters)
19+
var contributorsHeading = settings.heading || 'contributors'
1920

2021
return transform
2122

@@ -88,7 +89,7 @@ function contributors(options) {
8889
var table = createTable(contributors, formatters, align)
8990
var headingFound = false
9091

91-
heading(tree, 'contributors', onheading)
92+
heading(tree, contributorsHeading, onheading)
9293

9394
// Add the section if not found but with `appendIfMissing`.
9495
if (!headingFound && settings.appendIfMissing) {

readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ MIT
8484

8585
Inject a given list of contributors.
8686

87-
* Looks for the first heading containing `'Contributors'` (case insensitive)
87+
* Looks for the first heading matching `/^contributors$/i` or `options.heading`
8888
* If no heading is found and `appendIfMissing` is set, inject such a heading
8989
* Replaces the table in that section if there is one, or injects it otherwise
9090

@@ -107,6 +107,10 @@ default: `null`).
107107

108108
Inject the section if there is none (`boolean`, default: `false`).
109109

110+
###### `options.heading`
111+
112+
Heading to look for (`string` (case-insensitive) or `RegExp`, default: `'contributors'`).
113+
110114
###### `options.formatters`
111115

112116
Map of fields found in `contributors` to formatters (`Object.<Formatter>`).

test.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ try {
1616
test.onFinish(ondone)
1717

1818
test('remark-contributors', function (t) {
19-
t.plan(15)
19+
t.plan(17)
2020

2121
remark()
2222
.use(gfm)
@@ -393,6 +393,44 @@ test('remark-contributors', function (t) {
393393
)
394394
}
395395
)
396+
397+
remark()
398+
.use(gfm)
399+
.use(contributors, {
400+
heading: /^mitwirkende$/i
401+
})
402+
.process(
403+
read(path.join('fixtures', 'match-heading', 'index.md')),
404+
function (err, file) {
405+
t.deepEqual(
406+
[err, String(file)],
407+
[
408+
null,
409+
String(read(path.join('fixtures', 'match-heading', 'expected.md')))
410+
],
411+
'should match custom heading if `heading` option is passed a regex'
412+
)
413+
}
414+
)
415+
416+
remark()
417+
.use(gfm)
418+
.use(contributors, {
419+
heading: 'mitwirkende'
420+
})
421+
.process(
422+
read(path.join('fixtures', 'match-heading', 'index.md')),
423+
function (err, file) {
424+
t.deepEqual(
425+
[err, String(file)],
426+
[
427+
null,
428+
String(read(path.join('fixtures', 'match-heading', 'expected.md')))
429+
],
430+
'should match custom heading if `heading` option is passed a string'
431+
)
432+
}
433+
)
396434
})
397435

398436
function read(fp) {

0 commit comments

Comments
 (0)