diff --git a/scripts/export.js b/scripts/export.js index a6ff720..587848e 100644 --- a/scripts/export.js +++ b/scripts/export.js @@ -156,22 +156,42 @@ async function exportInit() { actionsArea.appendChild(refreshButton); } - async function exportMarkdown() { - const content = Array.from(document.querySelectorAll('main div.group')) - .map((i) => { - let j = i.cloneNode(true); - if (/dark\:bg-gray-800/.test(i.getAttribute('class'))) { - j.innerHTML = `
${i.innerHTML}
`; - } - return j.innerHTML; - }) - .join(''); - const data = ExportMD.turndown(content); - const { id, filename } = getName(); - await invoke('save_file', { name: `notes/${id}.md`, content: data }); - await invoke('download_list', { pathname: 'chat.notes.json', filename, id, dir: 'notes' }); + const SELECTOR = 'main div.group'; + const USER_INPUT_SELECTOR = 'div.empty\\:hidden'; + + function processNode(node, replaceInUserInput = false) { + let j = node.cloneNode(true); + if (/dark\:bg-gray-800/.test(node.getAttribute('class'))) { + j.innerHTML = `
${node.innerHTML}
`; + } + + if (replaceInUserInput) { + const userInputBlocks = j.querySelectorAll(USER_INPUT_SELECTOR); + userInputBlocks.forEach((block) => { + block.innerHTML = block.innerHTML.replace(/\n/g, '
').replace(/ /g, ' '); + }); + } + + return j.innerHTML; } + async function exportMarkdown() { + const nodes = Array.from(document.querySelectorAll(SELECTOR)); + + const content = nodes.map(i => processNode(i)).join(''); + const updatedContent = nodes.map(i => processNode(i, true)).join(''); + + const data = ExportMD.turndown(updatedContent); + const { id, filename } = getName(); + final_filename = `${filename}`; //`${filename}_${id}`; + + //await invoke('save_file', { name: `notes/${final_filename}_raw.txt`, content: content }); + await invoke('save_file', { name: `notes/${id}.md`, content: data }); + await invoke('download_list', { pathname: 'chat.notes.json', final_filename, id, dir: 'notes' }); + //await invoke('download_list', { pathname: 'chat.notes.json', final_filename, final_filename, dir: 'notes' }); + } + + async function downloadThread({ as = Format.PNG } = {}) { const { startLoading, stopLoading } = new window.__LoadingMask('Exporting in progress...'); startLoading(); @@ -313,11 +333,32 @@ async function exportInit() { return formattedDateTime; } + function sanitizeFilename(filename) { + if (!filename || filename === '') return ''; + + // Replace whitespaces with underscores + let sanitizedFilename = filename.replace(/\s/g, '_'); + + // Replace invalid filename characters with # + const invalidCharsRegex = /[<>:"/\\|?*\x00-\x1F]/g; + sanitizedFilename = sanitizedFilename.replace(invalidCharsRegex, '#'); + + // Check for filenames ending with period or space (Windows) + if (sanitizedFilename && /[\s.]$/.test(sanitizedFilename)) { + sanitizedFilename = sanitizedFilename.slice(0, -1) + '#'; + } + //console.log(sanitizedFilename); + return sanitizedFilename; + } + function getName() { const id = window.crypto.getRandomValues(new Uint32Array(1))[0].toString(36); - const name = + const name = document.querySelector('nav .overflow-y-auto a.hover\\:bg-gray-800')?.innerText?.trim() || ''; - return { filename: name ? name : id, id, pathname: 'chat.download.json' }; + clean_name = sanitizeFilename(name); + return { filename: name ? name : id, + id, + pathname: 'chat.download.json' }; } }