Skip to content

Commit

Permalink
Support Github flavored markdown with new NPM useMarkdown hook
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjarling committed Jun 13, 2024
1 parent a968069 commit bee7bba
Show file tree
Hide file tree
Showing 4 changed files with 494 additions and 179 deletions.
24 changes: 18 additions & 6 deletions components/Chat/Response/StreamedAnswer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from "react";
import { StyledStreamedAnswer } from "@/components/Chat/Response/Response.styled";
import useMarkdown from "@/hooks/useMarkdown";
import useMarkdown from "@nulib/use-markdown";

const cursor = "<!--PLACEHOLDER_CURSOR-->";

const ResponseStreamedAnswer = ({
isStreamingComplete,
Expand All @@ -9,12 +11,22 @@ const ResponseStreamedAnswer = ({
isStreamingComplete: boolean;
streamedAnswer: string;
}) => {
const { jsx: content } = useMarkdown({
hasCursor: !isStreamingComplete,
markdown: streamedAnswer,
});
const preparedMarkdown = !isStreamingComplete
? streamedAnswer + cursor
: streamedAnswer;

const { html } = useMarkdown(preparedMarkdown);

const cursorRegex = new RegExp(cursor, "g");
const updatedHtml = !isStreamingComplete
? html.replace(cursorRegex, `<span class="markdown-cursor"></span>`)
: html;

return <StyledStreamedAnswer>{content}</StyledStreamedAnswer>;
return (
<StyledStreamedAnswer>
<div dangerouslySetInnerHTML={{ __html: updatedHtml }} />
</StyledStreamedAnswer>
);
};

export default ResponseStreamedAnswer;
53 changes: 0 additions & 53 deletions hooks/useMarkdown.tsx

This file was deleted.

Loading

0 comments on commit bee7bba

Please sign in to comment.