[Feature] Better JSON Exporter #2692

[+] A view looks better
[+] auto minify json when click a copy in markdown and download

Co-Authored-By: wangwentong-lunaon <39506652+wangwentong-lunaon@users.noreply.github.com>
Co-Authored-By: Yifei Zhang <yidadaa@qq.com>
Co-Authored-By: B0zal <48602426+kfear1337@users.noreply.github.com>
This commit is contained in:
B0zal 2023-08-23 21:14:43 +07:00
parent 7562ab3c1c
commit 0113d4499b
No known key found for this signature in database
GPG Key ID: 2359195A49CAA696

View File

@ -565,21 +565,32 @@ export function MarkdownPreviewer(props: {
);
}
// modified by BackTrackZ now it's looks better
export function JsonPreviewer(props: {
messages: ChatMessage[];
topic: string;
}) {
const msgs = props.messages.map((m) => ({
const msgs = {
messages: [
{
role: "system",
content: "You are an assistant that " + props.topic,
},
...props.messages.map((m) => ({
role: m.role,
content: m.content,
}));
const mdText = "\n" + JSON.stringify(msgs, null, 2) + "\n";
})),
],
};
const mdText = "```json\n" + JSON.stringify(msgs, null, 2) + "\n```";
const minifiedJson = JSON.stringify(msgs);
const copy = () => {
copyToClipboard(JSON.stringify(msgs, null, 2));
copyToClipboard(minifiedJson);
};
const download = () => {
downloadAs(JSON.stringify(msgs, null, 2), `${props.topic}.json`);
downloadAs(JSON.stringify(msgs), `${props.topic}.json`);
};
return (
@ -587,11 +598,11 @@ export function JsonPreviewer(props: {
<PreviewActions
copy={copy}
download={download}
showCopy={true}
showCopy={false}
messages={props.messages}
/>
<div className="markdown-body">
<pre className={styles["export-content"]}>{mdText}</pre>
<div className="markdown-body" onClick={copy}>
<Markdown content={mdText} />
</div>
</>
);