【前端】解决: Property 'inline' does not exist on type 'ClassAttributes<HTMLElement> & HTMLAttribut...

简介: 【前端】解决: Property 'inline' does not exist on type 'ClassAttributes<HTMLElement> & HTMLAttribut...

一、背景

七镜在升级react-markdown v8.0.4react-markdown v9.0.1 之后,启动前端代码时,出现如下错误:

[tsl] ERROR in D:\v3_workspace_personal\blog_client_v5\src\component\markdown\i-markdown\i-markdown.tsx(71,30)
      TS2339: Property 'inline' does not exist on type 'ClassAttributes<HTMLElement> & HTMLAttributes<HTMLElement> & ExtraProps'.
ts-loader-default_e3b0c44298fc1c14
 @ ./src/app/app-example/app-example.tsx 4:0-71 24:58-67
 @ ./src/app/app.tsx 26:0-51 62:46-56
 @ ./src/index.tsx 4:0-28 6:94-97
webpack 5.89.0 compiled with 1 error in 761 ms

出现报错的部分代码如下所示:

                code:({node, inline, className, children, ...props})=> {
                    const match = /language-(\w+)/.exec(className || '')
                    return !inline && match ? (
                        <SyntaxHighlighter
                            showLineNumbers={true}
                            showInlineLineNumbers={true}
                            children={String(children).replace(/\n$/, '')}
                            // @ts-ignore
                            style={dark}
                            language={match[1]}
                            PreTag="div"
                            {...props}
                        />
                    ) : !inline && !match ? (
                        <SyntaxHighlighter
                            showLineNumbers={true}
                            children={String(children).replace(/\n$/, '')}
                            // @ts-ignore
                            style={dark}
                            language={'js'}
                            PreTag="div"
                            {...props}
                        />
                    ) : inline? (
                        <code className={"md-code-inline"} {...props}>
                            {children}
                        </code>
                    ) : (
                        <code className={className} {...props}>
                            {children}
                        </code>
                    )
                },

显而易见,新版本已经不支持传递 inline 了。

react-markdown 更新日志中也能找到这项更改:inline on code — create a plugin or use pre for the block

二、解决方案

在 pre 中增加如下代码:

            pre: ({children,node}: any) => {
                // 新增代码
                if(node.children&&node.children[0]) {
                    if(node.children[0].tagName == "code") {
                        node.children[0].properties.inline = false
                    }
                }
                // 新增代码
                return <Fragment>
                                                    <pre className="qijing-pre-element">
                                                        <CodeCopyBtn>{children}</CodeCopyBtn>
                                                        {children}
                                                    </pre>
                </Fragment>
            },

在 node 中判断如下:

            code: ({children, className, node, ...rest}) => {
                console.log("rest->",rest)
                console.log("node->",node?.properties)
                if(node?.properties.inline === false) {
                    console.log("")
                }
                // console.log(String(children).replace(/\n$/, ""))
                const match = /language-(\w+)/.exec(className || '')
                return node?.properties.inline === false ?match? (
                    <SyntaxHighlighter
                        style={dark}
                        language={match[1]}
                        PreTag="div"
                        showLineNumbers={true}
                        children={String(children).replace(/\n$/, "")}
                    />
                ) : <SyntaxHighlighter
                    showLineNumbers={true}
                    children={String(children).replace(/\n$/, '')}
                    style={dark}
                    language={'js'}
                    PreTag="div"
                />:(<code className={"md-code-inline"} >
                    {children}
                </code>)
            },


目录
相关文章
|
5月前
|
前端开发
前端知识笔记(一)———button的type属性
前端知识笔记(一)———button的type属性
106 0
|
前端开发 JavaScript
前端vue:解决Invalid prop: type check failed for prop “model“. Expected Object, got Array问题
前端vue:解决Invalid prop: type check failed for prop “model“. Expected Object, got Array问题
1188 0
前端vue:解决Invalid prop: type check failed for prop “model“. Expected Object, got Array问题
|
9月前
|
Web App开发 前端开发 JavaScript
前端学习笔记202307学习笔记第五十七天-模拟面试笔记react-fiber解决了什么问题
前端学习笔记202307学习笔记第五十七天-模拟面试笔记react-fiber解决了什么问题
98 0
|
9月前
|
前端开发 定位技术
前端学习笔记202305学习笔记第二十三天-地图单线程配置
前端学习笔记202305学习笔记第二十三天-地图单线程配置
66 0
前端学习笔记202305学习笔记第二十三天-地图单线程配置
|
9月前
|
前端开发 API
前端学习笔记202307学习笔记第五十七天-模拟面试笔记react-react-redux的工作流程
前端学习笔记202307学习笔记第五十七天-模拟面试笔记react-react-redux的工作流程
55 0
|
9月前
|
前端开发
前端学习笔记202306学习笔记第五十一天-工厂模式4
前端学习笔记202306学习笔记第五十一天-工厂模式
34 0
|
9月前
|
前端开发
前端学习笔记202305学习笔记第二十八天-数组结构之列表拖拽改变顺序4
前端学习笔记202305学习笔记第二十八天-数组结构之列表拖拽改变顺序4
32 0
|
5月前
|
前端开发 JavaScript
《Webpack5 核心原理与应用实践》学习笔记-> 构建微前端应用
《Webpack5 核心原理与应用实践》学习笔记-> 构建微前端应用
41 1
|
9月前
|
JavaScript 前端开发 调度
前端学习笔记202307学习笔记第五十七天-模拟面试笔记react-fiber和虚拟dom关系
前端学习笔记202307学习笔记第五十七天-模拟面试笔记react-fiber和虚拟dom关系
59 0
|
9月前
|
域名解析 缓存 网络协议
前端学习笔记202307学习笔记第五十七天-模拟面试笔记http-DNs解析ip地址
前端学习笔记202307学习笔记第五十七天-模拟面试笔记http-DNs解析ip地址
72 1