如何正确渲染 stream-diffs 代码块? #595
Replies: 4 comments 2 replies
|
你现在的配置里使用了 mode="chat"。chat 模式默认会优先使用普通的 |
|
补充说明:如果你的目标只是使用 playground 里展示的 stream-diffs 增强代码块,最简单的做法是直接去掉 mode="chat"。docs 模式默认会使用增强的 stream-diffs surface: <MarkdownRender
:content="content"
:final="!isStreaming"
:smooth-streaming="isStreaming ? 'auto' : false"
:fade="!isStreaming"
:typewriter="isStreaming"
:max-live-nodes="isStreaming ? 0 : undefined"
html-policy="safe"
code-block-stream
:render-code-blocks-as-pre="false"
/>只有在确实需要 chat 模式的默认渲染策略时,才保留 mode="chat",并显式加上 code-renderer="monaco"。这里的 monaco 是历史兼容名称,实际使用的是已安装的 stream-diffs runtime,不是 Monaco Editor。 |
|
@Simon-He95 你好再请教一下,为什么单独使用
<template>
<div>
<CodeBlockNode
class="chat-code-block"
:node="codeBlockNode"
:show-header="false"
:is-dark="true"
theme="github-dark"
:monaco-options="monacoOptions"
/>
</div>
</template>
<script setup lang="tsx">
import type { CodeBlockMonacoOptions, CodeBlockNodeProps } from 'markstream-vue'
import { CodeBlockNode } from 'markstream-vue'
type CodeBlockNode = CodeBlockNodeProps['node']
const props = defineProps<{
text: string
node?: Omit<CodeBlockNode, 'type' | 'code'>
}>()
const monacoOptions = {
theme: 'github-dark',
themes: ['github-dark', 'github-light'],
languages: ['typescript', 'vue', 'txt'],
MAX_HEIGHT: 400,
} satisfies CodeBlockMonacoOptions
const codeBlockNode = computed<CodeBlockNode>(() => {
return Object.assign(
{
type: 'code_block',
language: 'txt',
} as const,
{
code: props.text,
raw: props.node?.raw ?? props.text,
},
props.node,
)
})
</script> |
|
单独使用 CodeBlockNode 时,需要自己补上 MarkdownRender 自动处理的两部分:
静态/最终态代码块可以这样写: <script setup lang="ts">
import type { CodeBlockNodeProps } from 'markstream-vue'
import { CodeBlockNode } from 'markstream-vue'
const node = {
type: 'code_block',
language: 'typescript',
code: props.text,
raw: props.text,
} satisfies CodeBlockNodeProps['node']
</script>
<template>
<div class="markstream-vue">
<CodeBlockNode
:node="node"
:loading="false"
:stream="false"
:show-header="false"
:is-dark="true"
/>
</div>
</template>如果是流式代码块,保持 stream=true,并把当前是否仍在输出传给 loading;流结束时一定要变为 false: <div class="markstream-vue">
<CodeBlockNode
:node="node"
:stream="true"
:loading="isStreaming"
:is-dark="true"
/>
</div>其中 node.code 需要随着内容更新,isStreaming 从 true 变为 false 后,组件才会从流式 fallback 切换到 stream-diffs 的 diffs-container。还要确认已安装 stream-diffs,并导入 markstream-vue/index.css。 参考文档:https://markstream.simonhe.me/zh/guide/code-block-node |





补充说明:如果你的目标只是使用 playground 里展示的 stream-diffs 增强代码块,最简单的做法是直接去掉 mode="chat"。docs 模式默认会使用增强的 stream-diffs surface:
只有在确实需要 chat 模式的默认渲染策略时,才保留 mode="chat",并显式加上 code-renderer="monaco"。这里的 monaco 是历史兼容名称,实际使用的是已安装的 stream-diffs runtime,不是 Monaco Editor。