Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: markdown formatting #55

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@
"ink": "^4.4.1",
"ink-link": "^3.0.0",
"ink-text-input": "^5.0.1",
"marked": "^12.0.2",
"marked-terminal": "^7.0.0",
"openai": "^4.42.0",
"prompts": "^2.4.2",
"react": "^18.2.0",
"redent": "^4.0.0",
"tiktoken": "^1.0.14",
"ts-regex-builder": "^1.7.1",
"update-notifier": "^7.0.0",
"yargs": "^17.7.2",
"zod": "^3.23.6",
Expand All @@ -77,6 +80,7 @@
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.3",
"ink-markdown": "^1.0.4",
"jest": "^29.7.0",
"mock-fs": "^5.2.0",
"prettier": "^3.2.5",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/chat/ui/AiResponseLoader.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { Text } from 'ink';
import { Markdown } from '../../../components/Markdown.js';
import { TextSpinner } from '../../../components/TextSpinner.js';
import { colors } from '../../../theme/colors.js';
import { texts } from '../texts.js';
Expand All @@ -14,7 +15,7 @@ export function AiResponseLoader({ text }: AiResponseLoaderProps) {
<Text color={colors.assistant}>{texts.assistantLabel}</Text>
{text ? (
<>
{text} <TextSpinner type="sand" />
<Markdown>{text}</Markdown> <TextSpinner type="sand" />
</>
) : (
<>
Expand Down
41 changes: 40 additions & 1 deletion src/commands/chat/ui/list/AiChatMessageItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react';
import { Text } from 'ink';
//import { any, buildRegExp, capture, negativeLookahead, zeroOrMore } from 'ts-regex-builder';
import { formatSpeed, formatTime } from '../../../../format.js';
import { Markdown } from '../../../../components/Markdown.js';
import { colors } from '../../../../theme/colors.js';
import { useChatState, type AiChatMessage } from '../../state/state.js';
import { texts } from '../../texts.js';
Expand All @@ -16,7 +18,7 @@ export function AiChatMessageItem({ message }: AiChatMessageItemProps) {
<>
<Text color={colors.assistant}>
<Text>{texts.assistantLabel}</Text>
<Text>{message.text}</Text>
<Markdown>{message.text}</Markdown>

{verbose && message.responseTime != null ? (
<Text color={colors.info}>
Expand All @@ -31,3 +33,40 @@ export function AiChatMessageItem({ message }: AiChatMessageItemProps) {
</>
);
}

// const asteriskNextToChar = /[^\s*]/;

// const boldRegex = buildRegExp(
// [
// '**',
// negativeLookahead('*'),
// capture([asteriskNextToChar, zeroOrMore(any, { greedy: false }), asteriskNextToChar]),
// '**',
// negativeLookahead('*'),
// ],
// { global: true },
// );

// const italicsRegex = buildRegExp(
// [
// '*',
// negativeLookahead('*'),
// capture([asteriskNextToChar, zeroOrMore(any, { greedy: false }), asteriskNextToChar]),
// '*',
// negativeLookahead('*'),
// ],
// { global: true },
// );

// function processMessage(message: string) {
// const result1 = message.split(boldRegex);

// text = text.replace(normalItalicsRegex, '_$1_');
// text = text.replace(normalBoldRegex, '*$1*');

// return (
// <>
// <AiChatMessageItem message={message} />
// </>
// );
// }
15 changes: 15 additions & 0 deletions src/components/Markdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import { Text } from 'ink';
import { parse, setOptions } from 'marked';
import TerminalRenderer, { type TerminalRendererOptions } from 'marked-terminal';

export type Props = TerminalRendererOptions & {
children: string;
};

export function Markdown({ children, ...options }: Props) {
// @ts-ignore
setOptions({ renderer: new TerminalRenderer(options) });
// @ts-ignore
return <Text>{parse(children).trim()}</Text>;
}
Loading