Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | import { remarkCodeHike } from '@code-hike/mdx';
import remarkGfm from 'remark-gfm';
import theme from 'shiki/themes/one-dark-pro.json' with { type: 'json' };
import { serialize as sz } from 'next-mdx-remote/serialize';
import path from 'path';
import fs from 'fs';
/**
* @description Serialize MDX file.
*
* @example
* serializePath('/src/posts', 'first-post.mdx');
* returns { content: '...', meta: {...} }
*
* @param {string} route - Route of the MDX file.
* @param {string} fileName - Name of the MDX file.
* @returns {Object} - Object with MDX content and meta data.
*/
export const serializePath = (route: string, fileName: string) => {
const filePath = path.join(route, fileName);
const mdx = fs.readFileSync(filePath, 'utf8');
return sz(mdx, {
// MDX content is first-party (authored in this repo), so JS expressions
// are trusted. next-mdx-remote v6 blocks them by default, which breaks
// Code Hike's compiled output; re-enable JS while keeping the guard
// against dangerous globals (eval/Function/require/...).
blockJS: false,
blockDangerousJS: true,
mdxOptions: {
// GFM tables/autolinks are not CommonMark — without remark-gfm the pipes
// render as plain text. singleTilde off so "~1M"-style approximations in
// prose can never pair up into accidental strikethrough.
remarkPlugins: [[remarkGfm, { singleTilde: false }], [remarkCodeHike, { autoImport: false, theme }]],
useDynamicImport: true,
},
});
};
/**
* @description Serialize MDX file with Code Hike.
*
* @example
* serialize('# Hello World');
* returns { content: '...', meta: {...} }
*
* @param {string} mdx - MDX file.
* @returns {Object} - Object with MDX content and meta data.
*/
export const serialize = (mdx: string) =>
sz(mdx, {
// See serializePath: trusted first-party MDX, so allow JS expressions
// (required by Code Hike) while blocking dangerous globals.
blockJS: false,
blockDangerousJS: true,
mdxOptions: {
// See serializePath: GFM support with singleTilde disabled.
remarkPlugins: [[remarkGfm, { singleTilde: false }], [remarkCodeHike, { autoImport: false, theme }]],
useDynamicImport: true,
},
});
|