forked from SunLxy/md-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2459f09
commit e378721
Showing
9 changed files
with
98 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
"zh": "中文", | ||
"en": "英文", | ||
"docuemnt": "文档", | ||
"example": "案例" | ||
"example": "案例", | ||
"pkg-example": "PKG 示例" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React from 'react'; | ||
import { FC, PropsWithChildren, useRef } from 'react'; | ||
import MarkdownPreview from '@uiw/react-markdown-preview'; | ||
import { getMetaId, getURLParameters, CodeBlockData } from 'markdown-react-code-preview-loader'; | ||
|
||
import { Root, Element, RootContent } from 'hast'; | ||
import MDSource from 'pkg-example/README.md'; | ||
import CodeLayout from 'react-code-preview-layout'; | ||
|
||
const Preview = CodeLayout.Preview; | ||
const Code = CodeLayout.Code; | ||
const Toolbar = CodeLayout.Toolbar; | ||
|
||
interface CodePreviewProps { | ||
mdData?: CodeBlockData; | ||
'data-meta'?: string; | ||
} | ||
|
||
const CodePreview: FC<PropsWithChildren<CodePreviewProps>> = (props) => { | ||
const $dom = useRef<HTMLDivElement>(null); | ||
// @ts-ignore | ||
const { mdData, node, 'data-meta': meta, ...rest } = props; | ||
// @ts-ignore | ||
const line = node?.position?.start.line; | ||
const metaId = getMetaId(meta) || String(line); | ||
const Child = mdData?.components[`${metaId}`]; | ||
if (metaId && typeof Child === 'function') { | ||
const code = mdData?.data[metaId].value || ''; | ||
const param = getURLParameters(meta || ''); | ||
return ( | ||
<CodeLayout ref={$dom}> | ||
<Preview> | ||
<Child /> | ||
</Preview> | ||
<Toolbar text={code}>{param.title || 'Example'}</Toolbar> | ||
<Code> | ||
<pre {...rest} /> | ||
</Code> | ||
</CodeLayout> | ||
); | ||
} | ||
return <code {...rest} />; | ||
}; | ||
|
||
export function PkgExamplePage() { | ||
return ( | ||
<MarkdownPreview | ||
disableCopy={true} | ||
style={{ background: 'transparent' }} | ||
source={MDSource.source} | ||
rehypeRewrite={(node: Root | RootContent, index: number, parent: Root | Element) => { | ||
if (node.type === 'element' && parent && parent.type === 'root' && /h(1|2|3|4|5|6)/.test(node.tagName)) { | ||
const child = node.children && (node.children[0] as Element); | ||
if (child && child.properties && child.properties.ariaHidden === 'true') { | ||
child.children = []; | ||
} | ||
} | ||
}} | ||
components={{ | ||
code: (props) => <CodePreview {...props} mdData={MDSource} />, | ||
}} | ||
/> | ||
); | ||
} |