|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/// <reference types="remark-parse" /> |
| 4 | +/// <reference types="remark-stringify" /> |
| 5 | + |
| 6 | +/** |
| 7 | + * @typedef {import('mdast').Root} Root |
| 8 | + * @typedef {import('unified').Processor<Root>} Processor |
| 9 | + */ |
| 10 | + |
| 11 | +import * as remarkHeadings from '@vcarl/remark-headings'; |
| 12 | +import * as mdastAutoLink from 'mdast-util-gfm-autolink-literal'; |
| 13 | +import * as mdastTable from 'mdast-util-gfm-table'; |
| 14 | +import * as rehypeAutolinkHeadings from 'rehype-autolink-headings'; |
| 15 | +import * as rehypePrettyCode from 'rehype-pretty-code'; |
| 16 | +import * as rehypeRaw from 'rehype-raw'; |
| 17 | +import * as rehypeSlug from 'rehype-slug'; |
| 18 | +import { getHighlighter } from 'shiki'; |
| 19 | +import shikiNordTheme from 'shiki/themes/nord.json'; |
| 20 | + |
| 21 | +import { SUPPORTED_LANGUAGES } from './shiki.config.mjs'; |
| 22 | + |
| 23 | +/** |
| 24 | + * This function is used to add individual `mdast` plugins to the unified/mdx |
| 25 | + * processor with the intent of being able to customize plugins |
| 26 | + * |
| 27 | + * @returns {void} |
| 28 | + */ |
| 29 | +function nextMdastPlugins() { |
| 30 | + const self = /** @type {Processor} */ (this); |
| 31 | + const data = self.data(); |
| 32 | + |
| 33 | + const fromMarkdownExtensions = |
| 34 | + data.fromMarkdownExtensions || (data.fromMarkdownExtensions = []); |
| 35 | + |
| 36 | + const toMarkdownExtensions = |
| 37 | + data.toMarkdownExtensions || (data.toMarkdownExtensions = []); |
| 38 | + |
| 39 | + // Converts plain URLs on Markdown to HTML Anchor Tags |
| 40 | + fromMarkdownExtensions.push(mdastAutoLink.gfmAutolinkLiteralFromMarkdown()); |
| 41 | + toMarkdownExtensions.push(mdastAutoLink.gfmAutolinkLiteralToMarkdown()); |
| 42 | + |
| 43 | + // Converts plain Markdown Tables (GFM) to HTML Tables |
| 44 | + fromMarkdownExtensions.push(mdastTable.gfmTableFromMarkdown); |
| 45 | + toMarkdownExtensions.push(mdastTable.gfmTableToMarkdown()); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Provides all our Rehype Plugins that are used within MDX |
| 50 | + * |
| 51 | + * @param {'md' | 'mdx'} fileExtension |
| 52 | + * @returns {import('unified').Plugin[]} |
| 53 | + */ |
| 54 | +export function nextRehypePlugins(fileExtension) { |
| 55 | + const rehypePlugins = [ |
| 56 | + // Generates `id` attributes for headings (H1, ...) |
| 57 | + rehypeSlug.default, |
| 58 | + [ |
| 59 | + // Automatically add anchor links to headings (H1, ...) |
| 60 | + rehypeAutolinkHeadings.default, |
| 61 | + { |
| 62 | + behaviour: 'append', |
| 63 | + properties: { ariaHidden: true, tabIndex: -1, class: 'anchor' }, |
| 64 | + }, |
| 65 | + ], |
| 66 | + [ |
| 67 | + // Syntax Highlighter for Code Blocks |
| 68 | + rehypePrettyCode.default, |
| 69 | + { |
| 70 | + theme: shikiNordTheme, |
| 71 | + defaultLang: 'plaintext', |
| 72 | + getHighlighter: options => |
| 73 | + getHighlighter({ ...options, langs: SUPPORTED_LANGUAGES }), |
| 74 | + }, |
| 75 | + ], |
| 76 | + ]; |
| 77 | + |
| 78 | + if (fileExtension === 'md') { |
| 79 | + // We add this plugin at the top of the array as it is supposed to parse raw HTML |
| 80 | + // before any other plugins (such as adding headings, etc) |
| 81 | + // before any of the other plugins being applied |
| 82 | + rehypePlugins.unshift(rehypeRaw.default); |
| 83 | + } |
| 84 | + |
| 85 | + return rehypePlugins; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Provides all our Remark Plugins that are used within MDX |
| 90 | + * |
| 91 | + * @param {'md' | 'mdx'} fileExtension |
| 92 | + * @returns {import('unified').Plugin[]} |
| 93 | + */ |
| 94 | +export function nextRemarkPlugins() { |
| 95 | + return [remarkHeadings.default, nextMdastPlugins]; |
| 96 | +} |
0 commit comments