Skip to content

Instantly share code, notes, and snippets.

@jasoncarr95
Last active July 12, 2023 15:09
Show Gist options
  • Select an option

  • Save jasoncarr95/d7dc2a14949cf9233da94dacb469023a to your computer and use it in GitHub Desktop.

Select an option

Save jasoncarr95/d7dc2a14949cf9233da94dacb469023a to your computer and use it in GitHub Desktop.
Export ChatGPT Conversation To Markdown.
/**
* Export ChatGPT Conversation to Markdown
* @version 1.1.0
* @Updated 2023-07-02
*/
function getFileName() {
const now = new Date();
const year = now.getFullYear();
const month = (now.getMonth() + 1).toString().padStart(2, "0");
const day = now.getDate().toString().padStart(2, "0");
const dateString = `${year}-${month}-${day}`;
// Get the page title and date for the file name.
let pageTitle = document.title;
// remove special characters and spaces from the page title
pageTitle = pageTitle.replace(/[^a-zA-Z0-9-]/g, "-");
// Make sure page title doesn't start with a dash
if (pageTitle.startsWith("-")) {
pageTitle = pageTitle.slice(1);
}
let fileName = `${pageTitle}-${dateString}.md`;
// Remove any double dashes "--" from the file name
fileName = fileName.replace(/--/g, "-");
// Remove any trailing dashes from the file name
fileName = fileName.replace(/-$/, "");
return fileName;
}
function convertHtmlToMarkdown(html) {
return html
.replace(/<p>/g, "\n\n")
.replace(/<\/p>/g, "")
.replace(/<b>/g, "**")
.replace(/<\/b>/g, "**")
.replace(/<i>/g, "_")
.replace(/<\/i>/g, "_")
.replace(/<code[^>]*>/g, (match) => {
const lm = match.match(/class="[^"]*language-([^"]*)"/);
return lm ? "\n```" + lm[1] + "\n" : "```";
})
.replace(/<\/code[^>]*>/g, "```")
.replace(/<[^>]*>/g, "")
.replace(/Copy code/g, "")
.replace(
/This content may violate our content policy. If you believe this to be in error, please submit your feedback — your input will aid our research in this area./g,
""
)
.trim();
}
function getChatMessages() {
// Each chatMessage is a <div class="text-base"> element.
return Array.from(document.querySelectorAll(".text-base"));
}
function getMessageText(message) {
// The message text is in a <span class="whitespace-pre-wrap"> element.
const messageTextElement = message.querySelector(".whitespace-pre-wrap");
return messageTextElement ? messageTextElement.innerHTML : "";
}
function getMessageSender(message) {
// Get the name of the person who sent the message.
const senderImageElements = message.querySelectorAll("img");
return senderImageElements.length > 1 ? senderImageElements[1].alt : "ChatGPT";
}
function createMarkdownContent(chatMessages) {
let markdownContent = "";
for (const message of chatMessages) {
const messageText = getMessageText(message);
if (messageText) {
const messageSender = getMessageSender(message);
markdownContent += markdownContent == "" ? "" : "--------\n";
markdownContent += `**${messageSender}:** ${convertHtmlToMarkdown(messageText)}\n\n`;
}
}
return markdownContent;
}
function downloadMarkdownFile(fileName, markdownContent) {
// Create a new <a> element with the download attribute set to the file name
// and the href attribute set to a URL representing the markdown content
const downloadLink = document.createElement("a");
downloadLink.download = fileName;
downloadLink.href = URL.createObjectURL(new Blob([markdownContent], { type: "text/markdown" }));
// Append the <a> element to the document body and click it to initiate the download
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
// document.body.removeChild(link);
// Return the length of the markdown content
return markdownContent.length;
}
function exportChatLog() {
const fileName = getFileName();
const chatMessages = getChatMessages();
const markdownContent = createMarkdownContent(chatMessages);
const length = downloadMarkdownFile(fileName, markdownContent);
// Display a message to the user
console.log(`Exported chat log with ${chatMessages.length} messages (${length} characters)`);
console.log(`Saved as: ${fileName}`);
}
exportChatLog();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment