-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (37 loc) · 1.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
var GithubSlugger = require('github-slugger'),
slugger = new GithubSlugger();
// Store used anchors here
// {
// 'anchor1': 1
// 'anchor2': 2 // there are two the same anchors used on a page, we will add '-2' suffix to second
// }
// var anchors = {};
module.exports = function(text, level) {
// looking for {#anchor} in heading
var anchorInText = text.match(/(.*)( {#(.+)})/);
var anchor;
if (anchorInText) {
// text without anchor
text = anchorInText[1];
// anchor
anchor = anchorInText[3];
} else {
// if {#anchor} not found — constructing automatic anchor from the text
// anchor = text.replace(/[ .,:!#]+/g, '-').replace(/[-\?]*$/, '').replace(/([A-Z])+/g, function(s) { return s.toLowerCase() });
anchor = slugger.slug(text);
}
// TODO: process anchors duplicates
/*
if (anchors.hasOwnProperty(anchor)) {
anchor += '-' + anchors[anchor]++;
} else {
anchors[anchor] = 1;
}
*/
var options = this.options,
headingClass = options.headingClassName && ' class="' + options.headingClassName + level + '"',
headingAnchorClass = options.headingAnchorClassName && ' class="' + options.headingAnchorClassName + '"';
return '<h' + level + headingClass + ' id="' + anchor + '">' +
'<a href="#' + anchor + '"' + headingAnchorClass + '></a>' + text + '</h' + level + '>';
}