Skip to content

Commit

Permalink
type: fix markdown preview type error.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Nov 23, 2023
1 parent 6ef0f51 commit 4459ac3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
22 changes: 13 additions & 9 deletions core/src/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { getMetaId, isMeta, getURLParameters } from 'markdown-react-code-preview
import MarkdownPreview from '@uiw/react-markdown-preview';
import type { MarkdownPreviewProps } from '@uiw/react-markdown-preview';
import type { CodeBlockData } from 'markdown-react-code-preview-loader';
import { type Node } from 'unist';
import styled from 'styled-components';
import rehypeIgnore from 'rehype-ignore';
import { Root, Element, RootContent } from 'hast';
import { CodeProps } from 'react-markdown/lib/ast-to-react';
import type { FC } from 'react';
import type { MarkdownPreviewExampleProps } from './';
import { FC } from 'react';

const Preview = CodeLayout.Preview;
const Code = CodeLayout.Code;
Expand All @@ -27,22 +27,25 @@ const MarkdownStyle = styled(MarkdownPreview)`
border-radius: 0.55rem;
`;

type CodePreviewProps = CodeProps & {
type CodePreviewProps = React.HTMLAttributes<HTMLDivElement> & {
node?: Node;
components: MarkdownPreviewExampleProps['components'];
data: MarkdownPreviewExampleProps['data'];
'data-meta'?: string;
'data-md'?: string;
};

const CodePreview: FC<CodePreviewProps> = ({ inline, node, components, data, ...props }) => {
const { 'data-meta': meta, 'data-md': metaData, ...rest } = props as any;
if (inline || !isMeta(metaData)) {
return <code {...props} />;
const CodePreview: FC<CodePreviewProps> = ({ components, data, node, ...props }) => {
const { 'data-meta': meta, 'data-md': metaData, ...rest } = props;
if (!isMeta(metaData)) {
return <div {...props} />;
}
const line = node.position?.start.line;
const line = node?.position?.start.line;
const metaId = getMetaId(metaData) || String(line);
const Child = components[`${metaId}`];
if (metaId && typeof Child === 'function') {
const code = data[metaId].value || '';
const { title, boreder = 1, checkered = 1, code: codeNum = 1, toolbar = 1 } = getURLParameters(metaData);
const { title, boreder = 1, checkered = 1, code: codeNum = 1, toolbar = 1 } = getURLParameters(metaData || '');
return (
<CodeLayout bordered={!!Number(boreder)} disableCheckered={!Number(checkered)} style={{ marginBottom: 16 }}>
<Preview>
Expand Down Expand Up @@ -78,6 +81,7 @@ export default function Markdown(props: MarkdownProps) {
rehypeRewrite={(node: Root | RootContent, index: number, parent: Root | Element) => {
if (node.type === 'element' && node.tagName === 'pre' && /(pre|code)/.test(node.tagName) && node.children[0]) {
const child = node.children[0] as Element;
// @ts-ignore
const meta = (child.data?.meta || child.properties?.dataMeta) as string;
if (isMeta(meta)) {
node.tagName = 'div';
Expand Down
5 changes: 5 additions & 0 deletions www/.kktrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export default (conf: WebpackConfiguration, env: 'production' | 'development', o
VERSION: JSON.stringify(pkg.version),
}),
);
conf.ignoreWarnings = [
{
module: /node_modules[\\/]parse5[\\/]/,
},
];
conf.module!.exprContextCritical = false;
if (env === 'production') {
conf.output = { ...conf.output, publicPath: './' };
Expand Down

0 comments on commit 4459ac3

Please sign in to comment.