Skip to content

Commit 554086a

Browse files
authored
fix: fixed table rendering (html and gfm) and reorganised mdx plugins (#6039)
* fix: fixed table rendering (html and gfm) and reorganised mdx plugins * Update next.mdx.mjs Signed-off-by: Claudio W <cwunder@gnome.org> --------- Signed-off-by: Claudio W <cwunder@gnome.org>
1 parent 38c177d commit 554086a

File tree

5 files changed

+1370
-1343
lines changed

5 files changed

+1370
-1343
lines changed

next.dynamic.mjs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,13 @@
33
import { readFileSync } from 'node:fs';
44
import { join, normalize, sep } from 'node:path';
55

6-
import remarkHeadings from '@vcarl/remark-headings';
76
import { serialize } from 'next-mdx-remote/serialize';
8-
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
9-
import rehypePrettyCode from 'rehype-pretty-code';
10-
import rehypeSlug from 'rehype-slug';
11-
import { getHighlighter } from 'shiki';
12-
import shikiNordTheme from 'shiki/themes/nord.json';
137
import { VFile } from 'vfile';
148

159
import { DEFAULT_LOCALE_CODE, MD_EXTENSION_REGEX } from './next.constants.mjs';
1610
import { getMarkdownFiles } from './next.helpers.mjs';
1711
import { availableLocales } from './next.locales.mjs';
18-
import { remarkAutoLinkLiteral } from './next.mdast.mjs';
19-
import { SUPPORTED_LANGUAGES } from './shiki.config.mjs';
12+
import { nextRehypePlugins, nextRemarkPlugins } from './next.mdx.mjs';
2013

2114
// allows us to run a glob to get markdown files based on a language folder
2215
const getPathsByLanguage = async (locale = DEFAULT_LOCALE_CODE, ignored = []) =>
@@ -152,34 +145,19 @@ export const generateStaticProps = async (source = '', filename = '') => {
152145
// data post serialization (compilation) of the source Markdown into MDX
153146
const sourceAsVirtualFile = new VFile(source);
154147

148+
// Gets the file extension of the file, to determine which parser and plugins to use
149+
const fileExtension = filename.endsWith('.mdx') ? 'mdx' : 'md';
150+
155151
// This act as a MDX "compiler" but, lightweight. It parses the Markdown
156152
// string source into a React Component tree, and then it serializes it
157153
// it also supports Remark plugins, and MDX components
158154
// Note.: We use the filename extension to define the mode of execution
159155
const { compiledSource } = await serialize(sourceAsVirtualFile, {
160156
parseFrontmatter: true,
161157
mdxOptions: {
162-
rehypePlugins: [
163-
rehypeSlug,
164-
[
165-
rehypeAutolinkHeadings,
166-
{
167-
behaviour: 'append',
168-
properties: { ariaHidden: true, tabIndex: -1, class: 'anchor' },
169-
},
170-
],
171-
[
172-
rehypePrettyCode,
173-
{
174-
theme: shikiNordTheme,
175-
defaultLang: 'plaintext',
176-
getHighlighter: options =>
177-
getHighlighter({ ...options, langs: SUPPORTED_LANGUAGES }),
178-
},
179-
],
180-
],
181-
remarkPlugins: [remarkHeadings, remarkAutoLinkLiteral],
182-
format: filename.includes('.mdx') ? 'mdx' : 'md',
158+
rehypePlugins: nextRehypePlugins(fileExtension),
159+
remarkPlugins: nextRemarkPlugins(fileExtension),
160+
format: fileExtension,
183161
},
184162
});
185163

next.mdast.mjs

Lines changed: 0 additions & 25 deletions
This file was deleted.

next.mdx.mjs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)