-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathheading.js
More file actions
34 lines (30 loc) · 1.36 KB
/
heading.js
File metadata and controls
34 lines (30 loc) · 1.36 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
import {
getAndRemoveConfig,
removeAtag,
getAndRemoveDocsifyIgnoreConfig,
} from '../utils.js';
import { slugify } from '../slugify.js';
import { stripUrlExceptId } from '../../router/util.js';
export const headingCompiler = ({ renderer, router, compiler }) =>
(renderer.heading = function ({ tokens, depth, text }) {
const parsedText = this.parser.parseInline(tokens);
let { str, config } = getAndRemoveConfig(parsedText);
const nextToc = { depth, title: str };
const { content, ignoreAllSubs, ignoreSubHeading } =
getAndRemoveDocsifyIgnoreConfig(str);
str = content.trim();
nextToc.title = removeAtag(str);
nextToc.ignoreAllSubs = ignoreAllSubs;
nextToc.ignoreSubHeading = ignoreSubHeading;
const slug = slugify(config.id || text);
const url = router.toURL(router.getCurrentPath(), { id: slug });
nextToc.slug = stripUrlExceptId(url);
if (!compiler.blockquoteDepth) {
compiler.toc.push(nextToc);
}
// Note: tabindex="-1" allows programmatically focusing on heading
// elements after navigation. This is preferred over focusing on the link
// within the heading because it matches the focus behavior of screen
// readers when navigating page content.
return `<h${depth} id="${slug}" tabindex="-1"><a href="${url}" data-id="${slug}" class="anchor"><span>${str}</span></a></h${depth}>`;
});