forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheaderAnchor.js
More file actions
executable file
·99 lines (73 loc) · 2.69 KB
/
Copy pathheaderAnchor.js
File metadata and controls
executable file
·99 lines (73 loc) · 2.69 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
/**
* Reads ## Heading [#anchor]
* Transliterates heading text if no anchor
* writes that to `headingToken.anchor`
*
* Renders as link if `options.linkHeaderTag` or header id
*/
const parseAttrs = require('../utils/parseAttrs');
const makeAnchor = require('textUtil/makeAnchor');
const t = require('i18n');
t.requirePhrase('markit', 'error');
// add headingToken.achor
// not "id" attr, because rendering uses `.anchor` for the extra link OR id
function readHeadingAnchor(state) {
let env = state.env;
if (!env.headingsMap) env.headingsMap = new Map();
let tokens = state.tokens;
for (let idx = 0; idx < state.tokens.length; idx++) {
let headingToken = state.tokens[idx];
if (headingToken.type !== 'heading_open') continue;
idx++;
let inlineToken = tokens[idx];
if (inlineToken.type != 'inline') continue;
let anchorReg = /\s+\[#(.*?)\]$/;
let anchor;
if (inlineToken.content.match(anchorReg)) {
anchor = inlineToken.content.match(anchorReg)[1];
// strip [#...] from token content
inlineToken.content = inlineToken.content.replace(anchorReg, '');
let lastTextToken = inlineToken.children[inlineToken.children.length - 1];
lastTextToken.content = inlineToken.content.replace(anchorReg, '');
if (env.headingsMap.has(anchor)) {
// fixed anchor, cannot change, add -1 -2
lastTextToken.type = 'markdown_error_inline';
lastTextToken.content = t('markit.error.anchor_exists', {anchor: 'anchor'});
} else {
env.headingsMap.set(anchor, 1);
}
} else {
anchor = makeAnchor(inlineToken.content, state.md.options.translitAnchors);
if (env.headingsMap.has(anchor)) {
// иначе просто добавляю -2, -3 ...
env.headingsMap.set(anchor, env.headingsMap.get(anchor) + 1);
anchor = anchor + '-' + env.headingsMap.get(anchor);
} else {
env.headingsMap.set(anchor, 1);
}
}
headingToken.anchor = anchor;
}
}
module.exports = function(md) {
md.core.ruler.push('read_heading_anchor', readHeadingAnchor);
md.renderer.rules.heading_open = function(tokens, idx, options, env, slf) {
let token = tokens[idx];
let anchor = token.anchor;
if (options.linkHeaderTag) {
return `<${token.tag}${slf.renderAttrs(token)}><a class="main__anchor" name="${anchor}" href="#${anchor}">`;
} else {
// for ebook need id
return `<${token.tag} id="${anchor}">`;
}
};
md.renderer.rules.heading_close = function(tokens, idx, options, env, slf) {
let token = tokens[idx];
if (options.linkHeaderTag) {
return `</a></${token.tag}>`;
} else {
return `</${token.tag}>`;
}
};
};