forked from aziontech/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrehype-i18n-autolink-headings.ts
40 lines (34 loc) · 1.36 KB
/
rehype-i18n-autolink-headings.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
import type { Root } from 'hast';
import path from 'path';
import type { Transformer } from 'unified';
import { visit } from 'unist-util-visit';
import type { UILanguageKeys } from '../src/i18n/translation-checkers';
import { useTranslationsForLang } from '../src/i18n/util';
import { getLanguageCodeFromPathname, mdFilePathToUrl } from './remark-fallback-lang';
/**
* Rehype plugin to translate the headings' anchors according to the currently selected language.
*/
export function rehypei18nAutolinkHeadings() {
const pageSourceDir = path.resolve('./src/content/docs');
const baseUrl = 'https://www.azion.com/';
const transformer: Transformer<Root> = (tree, file) => {
const pageUrl = mdFilePathToUrl(file.path, pageSourceDir, baseUrl);
const pageLang = getLanguageCodeFromPathname(pageUrl.pathname);
const englishText = useTranslationsForLang('en')('a11y.sectionLink');
// Find anchor links
visit(tree, 'element', (node) => {
if (node.tagName === 'a' && node.properties?.class === 'anchor-link') {
// Find a11y text labels
visit(node, 'text', (text) => {
const heading = text.value.replace(englishText!, '');
const t = useTranslationsForLang(pageLang as UILanguageKeys);
const title = t('a11y.sectionLink') || englishText;
text.value = title + heading;
});
}
});
};
return function attacher() {
return transformer;
};
}