forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.mdx.mjs
96 lines (83 loc) · 2.93 KB
/
next.mdx.mjs
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
'use strict';
/// <reference types="remark-parse" />
/// <reference types="remark-stringify" />
/**
* @typedef {import('mdast').Root} Root
* @typedef {import('unified').Processor<Root>} Processor
*/
import * as remarkHeadings from '@vcarl/remark-headings';
import * as mdastAutoLink from 'mdast-util-gfm-autolink-literal';
import * as mdastTable from 'mdast-util-gfm-table';
import * as rehypeAutolinkHeadings from 'rehype-autolink-headings';
import * as rehypePrettyCode from 'rehype-pretty-code';
import * as rehypeRaw from 'rehype-raw';
import * as rehypeSlug from 'rehype-slug';
import { getHighlighter } from 'shiki';
import shikiNordTheme from 'shiki/themes/nord.json';
import { SUPPORTED_LANGUAGES } from './shiki.config.mjs';
/**
* This function is used to add individual `mdast` plugins to the unified/mdx
* processor with the intent of being able to customize plugins
*
* @returns {void}
*/
function nextMdastPlugins() {
const self = /** @type {Processor} */ (this);
const data = self.data();
const fromMarkdownExtensions =
data.fromMarkdownExtensions || (data.fromMarkdownExtensions = []);
const toMarkdownExtensions =
data.toMarkdownExtensions || (data.toMarkdownExtensions = []);
// Converts plain URLs on Markdown to HTML Anchor Tags
fromMarkdownExtensions.push(mdastAutoLink.gfmAutolinkLiteralFromMarkdown());
toMarkdownExtensions.push(mdastAutoLink.gfmAutolinkLiteralToMarkdown());
// Converts plain Markdown Tables (GFM) to HTML Tables
fromMarkdownExtensions.push(mdastTable.gfmTableFromMarkdown);
toMarkdownExtensions.push(mdastTable.gfmTableToMarkdown());
}
/**
* Provides all our Rehype Plugins that are used within MDX
*
* @param {'md' | 'mdx'} fileExtension
* @returns {import('unified').Plugin[]}
*/
export function nextRehypePlugins(fileExtension) {
const rehypePlugins = [
// Generates `id` attributes for headings (H1, ...)
rehypeSlug.default,
[
// Automatically add anchor links to headings (H1, ...)
rehypeAutolinkHeadings.default,
{
behaviour: 'append',
properties: { ariaHidden: true, tabIndex: -1, class: 'anchor' },
},
],
[
// Syntax Highlighter for Code Blocks
rehypePrettyCode.default,
{
theme: shikiNordTheme,
defaultLang: 'plaintext',
getHighlighter: options =>
getHighlighter({ ...options, langs: SUPPORTED_LANGUAGES }),
},
],
];
if (fileExtension === 'md') {
// We add this plugin at the top of the array as it is supposed to parse raw HTML
// before any other plugins (such as adding headings, etc)
// before any of the other plugins being applied
rehypePlugins.unshift(rehypeRaw.default);
}
return rehypePlugins;
}
/**
* Provides all our Remark Plugins that are used within MDX
*
* @param {'md' | 'mdx'} fileExtension
* @returns {import('unified').Plugin[]}
*/
export function nextRemarkPlugins() {
return [remarkHeadings.default, nextMdastPlugins];
}