Skip to content

Commit

Permalink
update demo
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Feb 1, 2025
1 parent bfef51c commit fc1fcb1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Markdown/demos/thinking/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,5 @@ export const ollama = `<think>
5. **无大气情况**:如果地球没有大气层,阳光会直接到达地面,天空可能呈现深灰色或黑色。
总结来说,天空呈现蓝色主要是因为蓝色光能够穿透大气层并被散射到我们眼中。`;

export const inlineMode = `<think>inline</think>`;
7 changes: 4 additions & 3 deletions src/Markdown/demos/thinking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useTheme } from 'antd-style';
import { PropsWithChildren, useState } from 'react';
import { Flexbox } from 'react-layout-kit';

import { fullThinking, ollama, partialThinking } from './content';
import { remarkCaptureThink } from './remarkPlugin';
import { fullThinking, inlineMode, ollama, partialThinking } from './content';
import { normalizeThinkTags, remarkCaptureThink } from './remarkPlugin';

const Think = ({ children }: PropsWithChildren) => {
const theme = useTheme();
Expand All @@ -25,9 +25,10 @@ export default () => {
<Button onClick={() => setContent(fullThinking)}>完整</Button>
<Button onClick={() => setContent(partialThinking)}>部分</Button>
<Button onClick={() => setContent(ollama)}>未换行</Button>
<Button onClick={() => setContent(inlineMode)}>inline</Button>
</Flexbox>
<Markdown components={{ think: Think }} remarkPlugins={[remarkCaptureThink]}>
{displayContent}
{normalizeThinkTags(displayContent)}
</Markdown>
</div>
);
Expand Down
18 changes: 16 additions & 2 deletions src/Markdown/demos/thinking/remarkPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { toMarkdown } from 'mdast-util-to-markdown';
import { SKIP, visit } from 'unist-util-visit';

// 预处理函数:确保 think 标签前后有两个换行符
export const normalizeThinkTags = (markdown: string) => {
return (
markdown
// 确保 <think> 标签前后有两个换行符
.replaceAll(/([^\n])\s*<think>/g, '$1\n\n<think>')
.replaceAll(/<think>\s*([^\n])/g, '<think>\n\n$1')
// 确保 </think> 标签前后有两个换行符
.replaceAll(/([^\n])\s*<\/think>/g, '$1\n\n</think>')
.replaceAll(/<\/think>\s*([^\n])/g, '</think>\n\n$1')
// 处理可能产生的多余换行符
.replaceAll(/\n{3,}/g, '\n\n')
);
};

export const remarkCaptureThink = () => {
return (tree: any) => {
console.log(tree);
return (tree: any, file: any) => {
visit(tree, 'html', (node, index, parent) => {
if (node.value === '<think>') {
const startIndex = index as number;
Expand Down

0 comments on commit fc1fcb1

Please sign in to comment.