From b444171ef3bf4412bc770a54525eaeb8623ecb48 Mon Sep 17 00:00:00 2001 From: timothycarambat Date: Tue, 12 Dec 2023 16:25:22 -0800 Subject: [PATCH] fix: Update copy code button on markdown to work with new highlightjs lib implementation --- frontend/src/utils/chat/markdown.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/frontend/src/utils/chat/markdown.js b/frontend/src/utils/chat/markdown.js index fdff9ab3..53b6804f 100644 --- a/frontend/src/utils/chat/markdown.js +++ b/frontend/src/utils/chat/markdown.js @@ -2,15 +2,17 @@ import { encode as HTMLEncode } from "he"; import markdownIt from "markdown-it"; import hljs from "highlight.js"; import "highlight.js/styles/github-dark-dimmed.min.css"; +import { v4 } from "uuid"; const markdown = markdownIt({ html: true, typographer: true, highlight: function (str, lang) { + const uuid = v4(); if (lang && hljs.getLanguage(lang)) { try { return ( - `
` +
+          `
` +
           hljs.highlight(lang, str, true).value +
           "
" ); @@ -18,19 +20,19 @@ const markdown = markdownIt({ } return ( - `
` +
+      `
` +
       HTMLEncode(str) +
       "
" ); }, }); -window.copySnippet = function () { - if (!event?.target) return false; - - const target = event?.target; +window.copySnippet = function (uuid = "") { + const target = document.getElementById(`code-${uuid}`); const markdown = - target.parentElement?.parentElement?.querySelector(".markdown")?.innerText; + target.parentElement?.parentElement?.querySelector( + "pre:first-of-type" + )?.innerText; if (!markdown) return false; window.navigator.clipboard.writeText(markdown);