Skip to content

Commit

Permalink
Merge pull request #66 from animalnots/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
animalnots authored Sep 23, 2024
2 parents 931601e + dcdec6d commit 51175f1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "better-chatgpt",
"private": true,
"version": "1.9.0",
"version": "1.10.0",
"type": "module",
"homepage": "./",
"main": "electron/index.cjs",
Expand All @@ -22,6 +22,10 @@
{
"name": "yaner-here <Yaner>",
"url": "https://github.com/yaner-here"
},
{
"name": "nm723",
"url": "https://github.com/nm723"
}
],
"description": "Play and chat smarter with BetterChatGPT - an amazing open-source web app with a better UI for exploring OpenAI's ChatGPT API!",
Expand Down
5 changes: 1 addition & 4 deletions src/components/Chat/ChatContent/Message/View/EditView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,7 @@ const EditView = ({
(content) => content.type === 'image_url'
);

if (
(!hasTextContent && !hasImageContent) ||
useStore.getState().generating
) {
if (useStore.getState().generating) {
return;
}

Expand Down
45 changes: 38 additions & 7 deletions src/utils/chat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import html2canvas from 'html2canvas';
import { ChatInterface } from '@type/chat';
import { ChatInterface, ContentInterface, isTextContent } from '@type/chat';

export const formatNumber = (num: number): string => {
return new Intl.NumberFormat('en-US', {
Expand Down Expand Up @@ -33,15 +33,46 @@ export const downloadImg = (imgData: string, fileName: string) => {
link.remove();
};

// Function to convert a chat object to markdown format
export const chatToMarkdown = (chat: ChatInterface) => {
export const chatToMarkdown = (chat: ChatInterface): string => {
let markdown = `# ${chat.title}\n\n`;
chat.messages.forEach((message) => {
markdown += `### **${message.role}**:\n\n${message.content}\n\n---\n\n`;
});
let i = 0;

while (i < chat.messages.length) {
let message = chat.messages[i];
let messageContent = contentToMarkdown(message.content);

while (hasUnclosedCodeBlock(messageContent) && i + 1 < chat.messages.length && chat.messages[i + 1].role === message.role) {
i++;
messageContent += contentToMarkdown(chat.messages[i].content);
}

if (hasUnclosedCodeBlock(messageContent)) {
// Close unclosed code block
messageContent += '\n```\n';
}

markdown += `### **${message.role}**:\n\n${messageContent}---\n\n`;
i++;
}

return markdown;
};

const contentToMarkdown = (contents: ContentInterface[]): string => {
let text = '';
contents.forEach((content) => {
text += isTextContent(content) ? content.text : `![image](${content.image_url.url})`;
text += "\n\n";
});
return text;
};

export const hasUnclosedCodeBlock = (text: string): boolean => {
const codeBlockPattern = /```/g;
const matches = text.match(codeBlockPattern);
return matches ? matches.length % 2 !== 0 : false;
};

// Function to download the markdown content as a file
export const downloadMarkdown = (markdown: string, fileName: string) => {
const link = document.createElement('a');
Expand All @@ -65,4 +96,4 @@ export const preprocessLaTeX = (content: string) => {
(_, equation) => `$${equation}$`
);
return inlineProcessedContent;
};
};

0 comments on commit 51175f1

Please sign in to comment.