Skip to content

Commit 4459ac3

Browse files
committed
type: fix markdown preview type error.
1 parent 6ef0f51 commit 4459ac3

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

core/src/Markdown.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { getMetaId, isMeta, getURLParameters } from 'markdown-react-code-preview
33
import MarkdownPreview from '@uiw/react-markdown-preview';
44
import type { MarkdownPreviewProps } from '@uiw/react-markdown-preview';
55
import type { CodeBlockData } from 'markdown-react-code-preview-loader';
6+
import { type Node } from 'unist';
67
import styled from 'styled-components';
78
import rehypeIgnore from 'rehype-ignore';
89
import { Root, Element, RootContent } from 'hast';
9-
import { CodeProps } from 'react-markdown/lib/ast-to-react';
10+
import type { FC } from 'react';
1011
import type { MarkdownPreviewExampleProps } from './';
11-
import { FC } from 'react';
1212

1313
const Preview = CodeLayout.Preview;
1414
const Code = CodeLayout.Code;
@@ -27,22 +27,25 @@ const MarkdownStyle = styled(MarkdownPreview)`
2727
border-radius: 0.55rem;
2828
`;
2929

30-
type CodePreviewProps = CodeProps & {
30+
type CodePreviewProps = React.HTMLAttributes<HTMLDivElement> & {
31+
node?: Node;
3132
components: MarkdownPreviewExampleProps['components'];
3233
data: MarkdownPreviewExampleProps['data'];
34+
'data-meta'?: string;
35+
'data-md'?: string;
3336
};
3437

35-
const CodePreview: FC<CodePreviewProps> = ({ inline, node, components, data, ...props }) => {
36-
const { 'data-meta': meta, 'data-md': metaData, ...rest } = props as any;
37-
if (inline || !isMeta(metaData)) {
38-
return <code {...props} />;
38+
const CodePreview: FC<CodePreviewProps> = ({ components, data, node, ...props }) => {
39+
const { 'data-meta': meta, 'data-md': metaData, ...rest } = props;
40+
if (!isMeta(metaData)) {
41+
return <div {...props} />;
3942
}
40-
const line = node.position?.start.line;
43+
const line = node?.position?.start.line;
4144
const metaId = getMetaId(metaData) || String(line);
4245
const Child = components[`${metaId}`];
4346
if (metaId && typeof Child === 'function') {
4447
const code = data[metaId].value || '';
45-
const { title, boreder = 1, checkered = 1, code: codeNum = 1, toolbar = 1 } = getURLParameters(metaData);
48+
const { title, boreder = 1, checkered = 1, code: codeNum = 1, toolbar = 1 } = getURLParameters(metaData || '');
4649
return (
4750
<CodeLayout bordered={!!Number(boreder)} disableCheckered={!Number(checkered)} style={{ marginBottom: 16 }}>
4851
<Preview>
@@ -78,6 +81,7 @@ export default function Markdown(props: MarkdownProps) {
7881
rehypeRewrite={(node: Root | RootContent, index: number, parent: Root | Element) => {
7982
if (node.type === 'element' && node.tagName === 'pre' && /(pre|code)/.test(node.tagName) && node.children[0]) {
8083
const child = node.children[0] as Element;
84+
// @ts-ignore
8185
const meta = (child.data?.meta || child.properties?.dataMeta) as string;
8286
if (isMeta(meta)) {
8387
node.tagName = 'div';

www/.kktrc.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export default (conf: WebpackConfiguration, env: 'production' | 'development', o
1919
VERSION: JSON.stringify(pkg.version),
2020
}),
2121
);
22+
conf.ignoreWarnings = [
23+
{
24+
module: /node_modules[\\/]parse5[\\/]/,
25+
},
26+
];
2227
conf.module!.exprContextCritical = false;
2328
if (env === 'production') {
2429
conf.output = { ...conf.output, publicPath: './' };

0 commit comments

Comments
 (0)