Commit 1286052a authored by Kantz's avatar Kantz
Browse files

Formel anzeige mit zwei malpunkten

parent c0d93773
import { useEffect, useRef, useState } from "react"; import { useEffect, useMemo, useRef, useState } from "react";
import ReactMarkdown, { defaultUrlTransform } from "react-markdown"; import ReactMarkdown, { defaultUrlTransform } from "react-markdown";
import type { RetrievedDoc } from "../Retrieval/DocPanel"; import type { RetrievedDoc } from "../Retrieval/DocPanel";
import { t } from "../../i18n"; import { t } from "../../i18n";
import { escapeAsterisksInsideMath } from "../../utils/mathMarkdown";
type MessageBubbleProps = { type MessageBubbleProps = {
role: "user" | "assistant"; role: "user" | "assistant";
...@@ -28,6 +29,7 @@ export default function MessageBubble({ ...@@ -28,6 +29,7 @@ export default function MessageBubble({
}: MessageBubbleProps) { }: MessageBubbleProps) {
const bubbleRef = useRef<HTMLDivElement | null>(null); const bubbleRef = useRef<HTMLDivElement | null>(null);
const [isRawView, setIsRawView] = useState(false); const [isRawView, setIsRawView] = useState(false);
const renderedText = useMemo(() => escapeAsterisksInsideMath(text), [text]);
useEffect(() => { useEffect(() => {
if (isRawView) { if (isRawView) {
...@@ -146,7 +148,7 @@ export default function MessageBubble({ ...@@ -146,7 +148,7 @@ export default function MessageBubble({
}, },
}} }}
> >
{text} {renderedText}
</ReactMarkdown> </ReactMarkdown>
)} )}
</div> </div>
......
import { useEffect, useRef } from "react"; import { useEffect, useMemo, useRef } from "react";
import ReactMarkdown from "react-markdown"; import ReactMarkdown from "react-markdown";
import { t } from "../../i18n"; import { t } from "../../i18n";
import { escapeAsterisksInsideMath } from "../../utils/mathMarkdown";
declare global { declare global {
interface Window { interface Window {
...@@ -30,6 +31,10 @@ export default function DocCard({ ...@@ -30,6 +31,10 @@ export default function DocCard({
defaultOpen = false, defaultOpen = false,
}: DocCardProps) { }: DocCardProps) {
const contentRef = useRef<HTMLDivElement | null>(null); const contentRef = useRef<HTMLDivElement | null>(null);
const renderedSnippet = useMemo(
() => escapeAsterisksInsideMath(snippetMarkdown),
[snippetMarkdown]
);
useEffect(() => { useEffect(() => {
const typeset = () => { const typeset = () => {
...@@ -60,7 +65,7 @@ export default function DocCard({ ...@@ -60,7 +65,7 @@ export default function DocCard({
</summary> </summary>
{subtitle ? <div className="doc-subtitle">{subtitle}</div> : null} {subtitle ? <div className="doc-subtitle">{subtitle}</div> : null}
<div className="doc-snippet" ref={contentRef}> <div className="doc-snippet" ref={contentRef}>
<ReactMarkdown>{snippetMarkdown}</ReactMarkdown> <ReactMarkdown>{renderedSnippet}</ReactMarkdown>
</div> </div>
<div className="doc-actions"> <div className="doc-actions">
<button className="btn small" type="button" onClick={onInspect}> <button className="btn small" type="button" onClick={onInspect}>
......
const MATH_SEGMENT_PATTERN =
/(\$\$[\s\S]*?\$\$|\\\[[\s\S]*?\\\]|\\\([\s\S]*?\\\)|\$(?:\\.|[^$\\\n])+\$)/g;
const escapeAsterisks = (value: string) => value.replace(/\*/g, "\\*");
export const escapeAsterisksInsideMath = (input: string): string => {
return input.replace(MATH_SEGMENT_PATTERN, (segment) => {
if (segment.startsWith("$$") && segment.endsWith("$$")) {
const inner = segment.slice(2, -2);
return `$$${escapeAsterisks(inner)}$$`;
}
if (segment.startsWith("\\[") && segment.endsWith("\\]")) {
const inner = segment.slice(2, -2);
return `\\[${escapeAsterisks(inner)}\\]`;
}
if (segment.startsWith("\\(") && segment.endsWith("\\)")) {
const inner = segment.slice(2, -2);
return `\\(${escapeAsterisks(inner)}\\)`;
}
if (segment.startsWith("$") && segment.endsWith("$")) {
const inner = segment.slice(1, -1);
return `$${escapeAsterisks(inner)}$`;
}
return segment;
});
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment