gpt4 book ai didi

reactjs - Next.JS - Gist 嵌入在通过危险的 SetInnerHTML 呈现的 Markdown 中,仅在整个页面加载后显示,而不是 React 路由加载

转载 作者:行者123 更新时间:2023-12-04 11:33:25 26 4
gpt4 key购买 nike

我有一系列存储在 MD 文件中的博客文章,其中一些包含脚本标签形式的多个 Gist 嵌入。
MD 内容通过危险的SetInnerHTML 呈现到页面,当页面被直接导航到时一切正常。但是,当使用应用程序的路由且未刷新整页时,脚本标记包含在标记中,但不会执行。
以下示例在此处使用默认的 Next.JS 博客示例:https://github.com/vercel/next-learn-starter/tree/master/basics-final .
Markdown :

---
title: "Example of GIST embedding"
date: "2020-02-20"
---
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut id arcu at arcu pretium porta. Nam feugiat est ut lectus imperdiet venenatis. Ut tempus vitae lectus id vestibulum. Sed tristique est metus. Ut pretium malesuada risus. Maecenas eget diam tristique, sagittis velit ac, efficitur nisi. Quisque lectus lorem, vehicula at mi vitae, dapibus volutpat augue. Sed dignissim pharetra ligula a efficitur. In ultrices imperdiet libero. Quisque ornare erat eu elit ullamcorper faucibus. Maecenas mattis sem a mauris posuere iaculis.

<script src="https://gist.github.com/robearlam/aec15c65aaffbd5ec00a826c5cbe57ad.js"></script>

Etiam sed interdum ligula, nec tincidunt justo. Aliquam erat volutpat. Fusce in scelerisque nisl. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin venenatis lectus at ligula mollis dapibus. Praesent condimentum metus fringilla, commodo enim non, fringilla dui. Vivamus nec ligula lacinia ante semper rhoncus eu sed nisi. In ac dolor vel lorem tincidunt lacinia. Praesent quis mattis mi, at finibus velit. Etiam auctor, magna fermentum tincidunt interdum, nulla augue porttitor enim, ac lobortis felis eros id dui. Suspendisse dignissim, dui sit amet pulvinar iaculis, nisi tellus rhoncus dolor, eu gravida risus massa accumsan magna.
import Layout from '../../components/layout'
import { getAllPostIds, getPostData } from '../../lib/posts'
import Head from 'next/head'
import Date from '../../components/date'
import utilStyles from '../../styles/utils.module.css'

export default function Post({ postData }) {
return (
<Layout>
<Head>
<title>{postData.title}</title>
</Head>
<article>
<h1 className={utilStyles.headingXl}>{postData.title}</h1>
<div className={utilStyles.lightText}>
<Date dateString={postData.date} />
</div>
<div dangerouslySetInnerHTML={{ __html: postData.contentHtml }} />
</article>
</Layout>
)
}

export async function getStaticPaths() {
const paths = getAllPostIds()
return {
paths,
fallback: false
}
}

export async function getStaticProps({ params }) {
const postData = await getPostData(params.id)
return {
props: {
postData
}
}
}

库函数
export async function getPostData(id) {
const fullPath = path.join(postsDirectory, `${id}.md`)
const fileContents = fs.readFileSync(fullPath, 'utf8')

// Use gray-matter to parse the post metadata section
const matterResult = matter(fileContents)

// Use remark to convert markdown into HTML string
const processedContent = await remark()
.use(html)
.process(matterResult.content)
const contentHtml = processedContent.toString()

// Combine the data with the id and contentHtml
return {
id,
contentHtml,
...matterResult.data
}
我还有一个 repo 在这里显示了这个问题: https://github.com/robearlam/gist-embedding-issue
干杯!

最佳答案

我遇到了同样的问题,I found this post to be very helpful .
基本上,您创建了一个自定义代码块,用于内联格式化您的代码标签。
请使用<ReactMarkdown>组件来呈现您的 Markdown (而不是备注),然后您在自定义代码块组件中使用 react-syntax-highlighter 库。
渲染 Markdown 时使用:

import CodeBlock from "../../components/codeblock"

<ReactMarkdown components={CodeBlock}>{your markdown data here}</ReactMarkdown>
为 Markdown 处理器导入自定义代码块组件。
Tom 的自定义代码块示例如下:
// components/codeblock.js
import React from "react"
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"
import {dracula} from 'react-syntax-highlighter/dist/cjs/styles/prism';

const CodeBlock = {
code({node, inline, className, children, ...props}) {
const match = /language-(\w+)/.exec(className || '')
return !inline && match ? (
<SyntaxHighlighter
style={dracula}
language={match[1]}
PreTag="div" {...props}>
{String(children).replace(/\n$/, '')}
</SyntaxHighlighter>

) : (
<code className={className} {...props}>
{children}
</code>
)
}
}

export default CodeBlock

关于reactjs - Next.JS - Gist 嵌入在通过危险的 SetInnerHTML 呈现的 Markdown 中,仅在整个页面加载后显示,而不是 React 路由加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66299304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com