/
content.tsx
40 lines (35 loc) · 1.37 KB
/
content.tsx
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
import { set as _set, get as _get } from "lodash";
import path from "path";
import * as runtime from 'react/jsx-runtime'
import { useRouter } from 'next/router'
function importAll(webpackContext: __WebpackModuleApi.RequireContext) {
const content = {}
webpackContext.keys().forEach((fileUrl) => {
const body = webpackContext(fileUrl);
const file = body.default
const contentPath = path.relative('./', fileUrl).replace(".mdx", "").replace("/", ".")
for (const key in file) {
if (Object.prototype.hasOwnProperty.call(file, key)) {
const element = file[key](runtime);
_set(content, contentPath + "." + key, element)
}
}
})
return content
}
const contentFiles = importAll(require.context('./', true, /\.mdx$/));
export function useContent(contentId: string) {
const router = useRouter()
const locale = router.locale ?? "en"
const p = `${locale}.${contentId}`.replace(/\.+/, ".")
const value = _get(contentFiles, p)
return value
}
type RenderMDXContentProps = {
contentId: string
}
export default function RenderMDXContent({contentId}: RenderMDXContentProps) {
const Component = useContent(contentId)
if(!Component) throw Error("There might be something off with your content. Please double check contentId " + contentId + ".")
return <Component/>
}