Skip to content

Commit 563c3c1

Browse files
committed
feat: layout refactor
1 parent 2573c9d commit 563c3c1

File tree

6 files changed

+35
-44
lines changed

6 files changed

+35
-44
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { MarkdownPreview } from './markdown-preview'

packages/markdown-editor/src/layouts/Body.tsx renamed to packages/markdown-editor/src/components/markdown-preview/markdown-preview.tsx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,31 @@ import { useMounted } from '@/nextra/hooks'
44
import type { PageTheme } from '@/nextra/normalize-pages'
55
import type { ReactNode, ReactElement } from 'react'
66
import { renderComponent } from '@/utils'
7+
import { MDXRemote } from 'next-mdx-remote'
8+
import { getComponents } from '@/mdx-components'
9+
import { useMarkdownEditor } from '@/contexts/markdown-editor'
710

8-
interface BodyProps {
11+
interface MarkdownPreviewProps {
912
themeContext: PageTheme
1013
breadcrumb: ReactNode
1114
timestamp?: number
1215
navigation: ReactNode
13-
children: ReactNode
16+
children?: ReactNode
1417
}
1518

1619
const classes = {
1720
toc: cn('nextra-toc nx-order-last nx-hidden nx-w-64 nx-shrink-0 xl:nx-block print:nx-hidden'),
1821
main: cn('nextra-scrollbar nx-break-words nx-h-screen nx-overflow-y-auto'),
1922
}
2023

21-
export const Body = ({ themeContext, navigation, children }: BodyProps): ReactElement => {
24+
export const MarkdownPreview = ({
25+
themeContext,
26+
navigation,
27+
children,
28+
}: MarkdownPreviewProps): ReactElement => {
2229
const config = useConfig()
2330
const isMount = useMounted()
31+
const { mdxSource } = useMarkdownEditor()
2432

2533
if (themeContext.layout === 'raw') {
2634
return <div className={classes.main}>{children}</div>
@@ -38,9 +46,21 @@ export const Body = ({ themeContext, navigation, children }: BodyProps): ReactEl
3846
<div className="nx-mt-16" />
3947
)
4048

49+
if (!mdxSource) {
50+
return <div>Mdx source Loading...</div>
51+
}
52+
4153
const content = (
4254
<>
43-
{children}
55+
<MDXRemote
56+
compiledSource={mdxSource.compiledSource}
57+
frontmatter={mdxSource.frontmatter}
58+
scope={mdxSource.scope}
59+
components={getComponents({
60+
isRawLayout: false,
61+
components: config.components,
62+
})}
63+
/>
4464
{gitTimestampEl}
4565
{navigation}
4666
</>

packages/markdown-editor/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type {
1313
PageMapItem,
1414
PageOpts,
1515
}
16+
export { Layout as MarkdownEditor } from './layouts'
1617
export const nextraCustomEventName: Record<keyof CustomEventDetail, string> = {
1718
createItemEvent: 'createItemEvent',
1819
updateItemEvent: 'updateItemEvent',
@@ -21,4 +22,3 @@ export const nextraCustomEventName: Record<keyof CustomEventDetail, string> = {
2122
deployEndEvent: 'deployEndEvent',
2223
deleteItemEvent: 'deleteItemEvent',
2324
}
24-
export { MarkdownEditor } from './layouts'
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export { Body } from './Body'
2-
export { InnerLayout } from './InnerLayout'
3-
export { MarkdownEditor } from './MarkdownEditor'
1+
export { InnerLayout } from './lnner-layout'
2+
export { Layout } from './layout'

packages/markdown-editor/src/layouts/MarkdownEditor.tsx renamed to packages/markdown-editor/src/layouts/layout.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@ import { MarkdownEditorProvider } from '@/contexts/markdown-editor'
33
import { SidebarProvider } from '@/contexts/sidebar'
44
import type { NextraThemeLayoutProps } from '@/nextra/types'
55
import type { ReactElement } from 'react'
6-
import { InnerLayout } from './InnerLayout'
6+
import { InnerLayout } from './lnner-layout'
77
import { ModalProvider } from '@/contexts/modal'
88
import { Potals } from '@/components/potals'
99

1010
type NextraDocLayoutProps = NextraThemeLayoutProps & {
1111
editorValue: string
1212
}
1313

14-
export function MarkdownEditor({
15-
children,
16-
editorValue,
17-
...context
18-
}: NextraDocLayoutProps): ReactElement {
14+
export function Layout({ children, editorValue, ...context }: NextraDocLayoutProps): ReactElement {
1915
return (
2016
<ConfigProvider value={context}>
2117
<MarkdownEditorProvider value={{ editorValue }}>

packages/markdown-editor/src/layouts/InnerLayout.tsx renamed to packages/markdown-editor/src/layouts/lnner-layout.tsx

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
11
import cn from 'clsx'
22
import { DEFAULT_LOCALE } from '@/constants'
3-
import { ActiveAnchorProvider, useConfig } from '@/contexts'
4-
import { useMarkdownEditor } from '@/contexts/markdown-editor'
3+
import { ActiveAnchorProvider } from '@/contexts'
54
import { useFSRoute } from '@/nextra/hooks'
65
import { normalizePages } from '@/nextra/normalize-pages'
76
import type { PageOpts } from '@/nextra/types'
87
import { useMemo, type ReactElement, type ReactNode } from 'react'
98
import { Banner, Breadcrumb, Head, Header, NavLinks, Sidebar } from '@/components'
9+
import { MarkdownPreview } from '@/components/markdown-preview'
1010
import { MarkdownEditor } from '@/components/markdown-editor'
11-
import { Body } from './Body'
12-
import { MDXRemote } from 'next-mdx-remote'
13-
import { getComponents } from '@/mdx-components'
1411

1512
type InnerLayoutProps = PageOpts & {
1613
children: ReactNode
1714
}
1815

19-
export const InnerLayout = ({
20-
frontMatter,
21-
headings,
22-
children,
23-
pageMap,
24-
}: InnerLayoutProps): ReactElement => {
25-
const config = useConfig()
16+
export const InnerLayout = ({ frontMatter, headings, pageMap }: InnerLayoutProps): ReactElement => {
2617
const fsPath = useFSRoute()
27-
const { mdxSource } = useMarkdownEditor()
2818

2919
const {
3020
activeType,
@@ -50,10 +40,6 @@ export const InnerLayout = ({
5040
const themeContext = { ...activeThemeContext, ...frontMatter }
5141
const direction = 'ltr'
5242

53-
if (!mdxSource) {
54-
return <div>Mdx source Loading...</div>
55-
}
56-
5743
return (
5844
<div dir={direction} className={cn('nx-relative nx-flex nx-flex-wrap')}>
5945
<script
@@ -85,7 +71,7 @@ export const InnerLayout = ({
8571
<MarkdownEditor />
8672
</div>
8773
<div className={cn('nextra-preview-container nx-w-1/2')}>
88-
<Body
74+
<MarkdownPreview
8975
themeContext={themeContext}
9076
breadcrumb={<Breadcrumb activePath={activePath} />}
9177
timestamp={new Date().getTime()}
@@ -94,18 +80,7 @@ export const InnerLayout = ({
9480
<NavLinks flatDirectories={flatDocsDirectories} currentIndex={activeIndex} />
9581
) : null
9682
}
97-
>
98-
<MDXRemote
99-
compiledSource={mdxSource.compiledSource}
100-
frontmatter={mdxSource.frontmatter}
101-
scope={mdxSource.scope}
102-
components={getComponents({
103-
isRawLayout: themeContext.layout === 'raw',
104-
components: config.components,
105-
})}
106-
/>
107-
{children}
108-
</Body>
83+
></MarkdownPreview>
10984
</div>
11085
</div>
11186
</ActiveAnchorProvider>

0 commit comments

Comments
 (0)