Skip to content

Commit e78e8b0

Browse files
committed
allow copy original text instead of renderend text
1 parent 07974a2 commit e78e8b0

File tree

3 files changed

+87
-12
lines changed

3 files changed

+87
-12
lines changed

shared_utils/advanced_markdown_format.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import os
44
import math
55
import html
6-
6+
import base64
7+
import gzip
78
from loguru import logger
89
from textwrap import dedent
910
from functools import lru_cache
@@ -325,6 +326,14 @@ def repl_fn(match):
325326
# cat them together
326327
return pre + convert_stage_5 + suf
327328

329+
def compress_string(s):
330+
compress_string = gzip.compress(s.encode('utf-8'))
331+
return base64.b64encode(compress_string).decode()
332+
333+
def decompress_string(s):
334+
decoded_string = base64.b64decode(s)
335+
return gzip.decompress(decoded_string).decode('utf-8')
336+
328337
@lru_cache(maxsize=128) # 使用 lru缓存 加快转换速度
329338
def markdown_convertion(txt):
330339
"""
@@ -336,6 +345,12 @@ def markdown_convertion(txt):
336345
# print('警告,输入了已经经过转化的字符串,二次转化可能出问题')
337346
return txt # 已经被转化过,不需要再次转化
338347

348+
# 在文本中插入一个base64编码的原始文本,以便在复制时能够获得原始文本
349+
raw_text_encoded = compress_string(txt)
350+
raw_text_node = f'<div class="raw_text">{raw_text_encoded}</div>'
351+
suf = raw_text_node + "</div>"
352+
353+
# 用于查找数学公式的正则表达式
339354
find_equation_pattern = r'<script type="math/tex(?:.*?)>(.*?)</script>'
340355

341356
txt = fix_markdown_indent(txt)
@@ -493,6 +508,7 @@ def simple_markdown_convertion(text):
493508
suf = "</div>"
494509
if text.startswith(pre) and text.endswith(suf):
495510
return text # 已经被转化过,不需要再次转化
511+
496512
text = compat_non_markdown_input(text) # 兼容非markdown输入
497513
text = markdown.markdown(
498514
text,

themes/common.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,7 @@
332332
text-wrap: wrap;
333333
opacity: 0.8;
334334
}
335+
336+
.raw_text {
337+
display: none;
338+
}

themes/common.js

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,24 @@ function cancel_loading_status() {
259259
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
260260
// 第 2 部分: 复制按钮
261261
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
262+
// 解压缩函数
263+
function decompressString(compressedString) {
264+
// 第1步:Base64解码
265+
const binaryString = atob(compressedString);
262266

267+
// 第2步:将二进制字符串转换为Uint8Array
268+
const bytes = new Uint8Array(binaryString.length);
269+
for (let i = 0; i < binaryString.length; i++) {
270+
bytes[i] = binaryString.charCodeAt(i);
271+
}
272+
273+
// 第3步:使用DecompressionStream (基于Web Streams API)进行gzip解压缩
274+
const ds = new DecompressionStream('gzip');
275+
const decompressedStream = new Response(new Blob([bytes])).body.pipeThrough(ds);
276+
277+
// 第4步:获取解压后的数据并转换为字符串
278+
return new Response(decompressedStream).text();
279+
}
263280

264281
var allow_auto_read_continously = true;
265282
var allow_auto_read_tts_flag = false;
@@ -283,19 +300,56 @@ function addCopyButton(botElement, index, is_last_in_arr) {
283300
return;
284301
}
285302

286-
var copyButton = document.createElement('button');
287-
copyButton.classList.add('copy-bot-btn');
288-
copyButton.setAttribute('aria-label', 'Copy');
289-
copyButton.innerHTML = copyIcon;
290-
copyButton.addEventListener('click', async () => {
291-
const textToCopy = botElement.innerText;
303+
// var copyButton = document.createElement('button');
304+
// copyButton.classList.add('copy-bot-btn');
305+
// copyButton.setAttribute('aria-label', 'Copy');
306+
// copyButton.innerHTML = copyIcon;
307+
// copyButton.addEventListener('click', async () => {
308+
// const textToCopy = botElement.innerText;
309+
// try {
310+
// // push_text_to_audio(textToCopy).catch(console.error);
311+
// if ("clipboard" in navigator) {
312+
// await navigator.clipboard.writeText(textToCopy);
313+
// copyButton.innerHTML = copiedIcon;
314+
// setTimeout(() => {
315+
// copyButton.innerHTML = copyIcon;
316+
// }, 1500);
317+
// } else {
318+
// const textArea = document.createElement("textarea");
319+
// textArea.value = textToCopy;
320+
// document.body.appendChild(textArea);
321+
// textArea.select();
322+
// try {
323+
// document.execCommand('copy');
324+
// copyButton.innerHTML = copiedIcon;
325+
// setTimeout(() => {
326+
// copyButton.innerHTML = copyIcon;
327+
// }, 1500);
328+
// } catch (error) {
329+
// console.error("Copy failed: ", error);
330+
// }
331+
// document.body.removeChild(textArea);
332+
// }
333+
// } catch (error) {
334+
// console.error("Copy failed: ", error);
335+
// }
336+
// });
337+
338+
// 原始文本拷贝
339+
var copyButtonOrig = document.createElement('button');
340+
copyButtonOrig.classList.add('copy-bot-btn');
341+
copyButtonOrig.setAttribute('aria-label', 'Copy');
342+
copyButtonOrig.innerHTML = copyIcon;
343+
copyButtonOrig.addEventListener('click', async () => {
292344
try {
345+
const base64gzipcode = botElement.getElementsByClassName('raw_text')[0].innerText;
346+
const textToCopy = await decompressString(base64gzipcode);
293347
// push_text_to_audio(textToCopy).catch(console.error);
294348
if ("clipboard" in navigator) {
295349
await navigator.clipboard.writeText(textToCopy);
296-
copyButton.innerHTML = copiedIcon;
350+
copyButtonOrig.innerHTML = copiedIcon;
297351
setTimeout(() => {
298-
copyButton.innerHTML = copyIcon;
352+
copyButtonOrig.innerHTML = copyIcon;
299353
}, 1500);
300354
} else {
301355
const textArea = document.createElement("textarea");
@@ -304,9 +358,9 @@ function addCopyButton(botElement, index, is_last_in_arr) {
304358
textArea.select();
305359
try {
306360
document.execCommand('copy');
307-
copyButton.innerHTML = copiedIcon;
361+
copyButtonOrig.innerHTML = copiedIcon;
308362
setTimeout(() => {
309-
copyButton.innerHTML = copyIcon;
363+
copyButtonOrig.innerHTML = copyIcon;
310364
}, 1500);
311365
} catch (error) {
312366
console.error("Copy failed: ", error);
@@ -345,7 +399,8 @@ function addCopyButton(botElement, index, is_last_in_arr) {
345399

346400
var messageBtnColumn = document.createElement('div');
347401
messageBtnColumn.classList.add('message-btn-row');
348-
messageBtnColumn.appendChild(copyButton);
402+
// messageBtnColumn.appendChild(copyButton);
403+
messageBtnColumn.appendChild(copyButtonOrig);
349404
if (enable_tts) {
350405
messageBtnColumn.appendChild(audioButton);
351406
}

0 commit comments

Comments
 (0)