Copy chat message with it's markdown formatting in Web, Desktop apps

This commit is contained in:
Debanjum Singh Solanky
2024-04-16 17:36:10 +05:30
parent 91c8b137f1
commit 1f2ffce85b
2 changed files with 20 additions and 10 deletions

View File

@@ -13,9 +13,14 @@
<script> <script>
let chatOptions = []; let chatOptions = [];
function copyParentText(event) { function createCopyParentText(message) {
return function(event) {
copyParentText(event, message);
}
}
function copyParentText(event, message=null) {
const button = event.currentTarget; const button = event.currentTarget;
const textContent = button.parentNode.textContent.trim(); const textContent = message ?? button.parentNode.textContent.trim();
navigator.clipboard.writeText(textContent).then(() => { navigator.clipboard.writeText(textContent).then(() => {
button.firstChild.src = "./assets/icons/copy-button-success.svg"; button.firstChild.src = "./assets/icons/copy-button-success.svg";
setTimeout(() => { setTimeout(() => {
@@ -306,9 +311,9 @@
return renderMessage(message, by, dt, references, false, "return"); return renderMessage(message, by, dt, references, false, "return");
} }
function formatHTMLMessage(htmlMessage, raw=false, willReplace=true) { function formatHTMLMessage(message, raw=false, willReplace=true) {
var md = window.markdownit(); var md = window.markdownit();
let newHTML = htmlMessage; let newHTML = message;
// Remove any text between <s>[INST] and </s> tags. These are spurious instructions for the AI chat model. // Remove any text between <s>[INST] and </s> tags. These are spurious instructions for the AI chat model.
newHTML = newHTML.replace(/<s>\[INST\].+(<\/s>)?/g, ''); newHTML = newHTML.replace(/<s>\[INST\].+(<\/s>)?/g, '');
@@ -340,7 +345,7 @@
copyIcon.src = "./assets/icons/copy-button.svg"; copyIcon.src = "./assets/icons/copy-button.svg";
copyIcon.classList.add("copy-icon"); copyIcon.classList.add("copy-icon");
copyButton.appendChild(copyIcon); copyButton.appendChild(copyIcon);
copyButton.addEventListener('click', copyParentText); copyButton.addEventListener('click', createCopyParentText(message));
element.append(copyButton); element.append(copyButton);
} }

View File

@@ -30,9 +30,14 @@ To get started, just start typing below. You can also type / to see a list of co
const allowedExtensions = ['text/org', 'text/markdown', 'text/plain', 'text/html', 'application/pdf']; const allowedExtensions = ['text/org', 'text/markdown', 'text/plain', 'text/html', 'application/pdf'];
const allowedFileEndings = ['org', 'md', 'txt', 'html', 'pdf']; const allowedFileEndings = ['org', 'md', 'txt', 'html', 'pdf'];
let chatOptions = []; let chatOptions = [];
function copyParentText(event) { function createCopyParentText(message) {
return function(event) {
copyParentText(event, message);
}
}
function copyParentText(event, message=null) {
const button = event.currentTarget; const button = event.currentTarget;
const textContent = button.parentNode.textContent.trim(); const textContent = message ?? button.parentNode.textContent.trim();
navigator.clipboard.writeText(textContent).then(() => { navigator.clipboard.writeText(textContent).then(() => {
button.firstChild.src = "/static/assets/icons/copy-button-success.svg"; button.firstChild.src = "/static/assets/icons/copy-button-success.svg";
setTimeout(() => { setTimeout(() => {
@@ -332,9 +337,9 @@ To get started, just start typing below. You can also type / to see a list of co
return renderMessage(message, by, dt, references, false, "return"); return renderMessage(message, by, dt, references, false, "return");
} }
function formatHTMLMessage(htmlMessage, raw=false, willReplace=true) { function formatHTMLMessage(message, raw=false, willReplace=true) {
var md = window.markdownit(); var md = window.markdownit();
let newHTML = htmlMessage; let newHTML = message;
// Remove any text between <s>[INST] and </s> tags. These are spurious instructions for the AI chat model. // Remove any text between <s>[INST] and </s> tags. These are spurious instructions for the AI chat model.
newHTML = newHTML.replace(/<s>\[INST\].+(<\/s>)?/g, ''); newHTML = newHTML.replace(/<s>\[INST\].+(<\/s>)?/g, '');
@@ -366,7 +371,7 @@ To get started, just start typing below. You can also type / to see a list of co
copyIcon.src = "/static/assets/icons/copy-button.svg"; copyIcon.src = "/static/assets/icons/copy-button.svg";
copyIcon.classList.add("copy-icon"); copyIcon.classList.add("copy-icon");
copyButton.appendChild(copyIcon); copyButton.appendChild(copyIcon);
copyButton.addEventListener('click', copyParentText); copyButton.addEventListener('click', createCopyParentText(message));
element.append(copyButton); element.append(copyButton);
} }