@fullstackdatasolutions/articles 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,47 @@
1
+ // MDX files must use JSX prop syntax:
2
+ // Correct: <span style={{ color: '#3b82f6' }}>Text</span>
3
+ // Incorrect: <span style="color: #3b82f6">Text</span>
4
+ // Correct: className="..."
5
+ // Incorrect: class="..."
6
+ import React from 'react'
7
+ import type { ComponentType, ImgHTMLAttributes } from 'react'
8
+ import * as devRuntime from 'react/jsx-dev-runtime'
9
+ import * as runtime from 'react/jsx-runtime'
10
+ import { evaluate } from '@mdx-js/mdx'
11
+ import rehypePrism from 'rehype-prism-plus'
12
+ import rehypeSlug from 'rehype-slug'
13
+ import remarkGfm from 'remark-gfm'
14
+ import remarkGithubBlockquoteAlert from 'remark-github-blockquote-alert'
15
+ import { customRenderer } from './markdown'
16
+
17
+ type MdxContent = ComponentType<{
18
+ components?: Record<string, ComponentType<unknown>>
19
+ }>
20
+
21
+ function makeImgComponent(basePath: string) {
22
+ return function MdxImage({ src, alt, ...props }: ImgHTMLAttributes<HTMLImageElement>) {
23
+ const resolvedSrc =
24
+ src && !src.startsWith('http') && !src.startsWith('/') ? `${basePath}/${src}` : src
25
+ return React.createElement('img', { src: resolvedSrc, alt, ...props })
26
+ }
27
+ }
28
+
29
+ export async function renderMdxSource(source: string, basePath?: string) {
30
+ const isDevelopment = process.env.NODE_ENV === 'development'
31
+
32
+ const mdxModule = await evaluate(source, {
33
+ ...(isDevelopment ? devRuntime : runtime),
34
+ development: isDevelopment,
35
+ remarkPlugins: [remarkGfm, remarkGithubBlockquoteAlert],
36
+ rehypePlugins: [
37
+ customRenderer,
38
+ rehypeSlug,
39
+ // @ts-ignore
40
+ rehypePrism,
41
+ ],
42
+ })
43
+
44
+ const Content = mdxModule.default as MdxContent
45
+ const components = basePath ? { img: makeImgComponent(basePath) } : undefined
46
+ return <Content components={components as Record<string, ComponentType<unknown>>} />
47
+ }