-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.ts
166 lines (133 loc) · 4.88 KB
/
index.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import {bold} from 'chalk';
import GithubSlugger from 'github-slugger';
import {headingInfo} from '../../utils';
import {CUSTOM_ID_REGEXP, CUSTOM_ID_EXCEPTION} from './constants';
import StateCore from 'markdown-it/lib/rules_core/state_core';
import Token from 'markdown-it/lib/token';
import {escapeHtml} from 'markdown-it/lib/common/utils';
import {MarkdownItPluginCb} from '../typings';
const slugify: (str: string, opts: {}) => string = require('slugify');
function createLinkTokens(state: StateCore, id: string, title: string, setId = false) {
const open = new state.Token('link_open', 'a', 1);
const close = new state.Token('link_close', 'a', -1);
if (setId) {
open.attrSet('id', id);
}
open.attrSet('href', '#' + id);
open.attrSet('class', 'yfm-anchor');
open.attrSet('aria-hidden', 'true');
// SEO: render invisible heading title because link must have text content.
const hiddenDesc = new state.Token('anchor_hidden_desc', '', 0);
hiddenDesc.content = title;
return [open, hiddenDesc, close];
}
const getCustomIds = (content: string) => {
const ids: string[] = [];
content.replace(CUSTOM_ID_REGEXP, (match, customId) => {
if (match !== CUSTOM_ID_EXCEPTION) {
ids.push(customId);
}
return '';
});
return ids.length ? ids : null;
};
const removeCustomId = (content: string) => {
if (CUSTOM_ID_REGEXP.test(content)) {
return content
.replace(CUSTOM_ID_REGEXP, (match) => {
if (match === CUSTOM_ID_EXCEPTION) {
return match;
}
return '';
})
.trim();
}
return content;
};
const removeCustomIds = (token: Token) => {
token.content = removeCustomId(token.content);
token.children?.forEach((child) => {
child.content = removeCustomId(child.content);
});
};
interface Options {
extractTitle?: boolean;
supportGithubAnchors?: boolean;
}
const index: MarkdownItPluginCb<Options> = (
md,
{extractTitle, path, log, supportGithubAnchors},
) => {
const plugin = (state: StateCore) => {
/* Do not use the plugin if it is included in the file */
if (state.env.includes && state.env.includes.length) {
return;
}
const ids: Record<string, number> = {};
const tokens = state.tokens;
let i = 0;
const slugger = new GithubSlugger();
while (i < tokens.length) {
const token = tokens[i];
const isHeading = token.type === 'heading_open';
if (isHeading) {
const {title, level} = headingInfo(tokens, i);
const inlineToken = tokens[i + 1];
let id = token.attrGet('id');
let ghId: string;
if (!title) {
log.warn(`Header without title${path ? ` in ${bold(path)}` : ''}`);
}
if (level < 2 && extractTitle) {
i += 3;
continue;
}
const customIds = getCustomIds(inlineToken.content);
if (customIds) {
id = customIds[0];
removeCustomIds(tokens[i + 1]);
} else {
id = slugify(title, {
lower: true,
remove: /[*+~.()'"!:@`ь]/g,
});
ghId = slugger.slug(title);
}
token.attrSet('id', id);
if (ids[id]) {
id = id + ids[id]++;
token.attrSet('id', id);
} else {
ids[id] = 1;
}
const allAnchorIds = customIds ? customIds : [id];
const anchorTitle = removeCustomId(title).replace(/`/g, '');
allAnchorIds.forEach((customId) => {
const setId = id !== customId;
const linkTokens = createLinkTokens(state, customId, anchorTitle, setId);
inlineToken.children?.unshift(...linkTokens);
if (supportGithubAnchors) {
const ghLinkTokens = createLinkTokens(state, ghId, anchorTitle, true);
inlineToken.children?.unshift(...ghLinkTokens);
}
});
i += 3;
continue;
}
i++;
}
};
try {
md.core.ruler.after('includes', 'anchors', plugin);
} catch {
try {
md.core.ruler.after('curly_attributes', 'anchors', plugin);
} catch {
md.core.ruler.push('anchors', plugin);
}
}
md.renderer.rules.anchor_hidden_desc = function (tokens, index) {
return '<span class="visually-hidden">' + escapeHtml(tokens[index].content) + '</span>';
};
};
export = index;