Skip to content

Instantly share code, notes, and snippets.

@cshanejennings
Last active February 17, 2023 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cshanejennings/f52bc772c49a095e8838db09d88e9826 to your computer and use it in GitHub Desktop.
Save cshanejennings/f52bc772c49a095e8838db09d88e9826 to your computer and use it in GitHub Desktop.
Save ChatGPT script as Markdown file
(function (CONFIG) {
const make_headlines = (toc, q) => [...toc, q.replace(/----\n###(.*)\n----/g,'* [[#$1]]')];
const post_process = (str) => {
const with_headers = str.replace(/\*\*You\*\*:(.*)(\n\n\*\*ChatGPT)/g, "----\n###$1\n----$2")
.replace(/\*\*ChatGPT\*\*\:[\s]*/g, "#unconfirmed-chatgpt\n");
const body_text = with_headers.split(/----\n### .*\n----/g).reduce((body, ans) => (ans)
? body.replace(ans, ans + '\n[[#Questions asked|Back to Questions]]\n\n')
: body, with_headers);
const toc = with_headers.match(/----\n\#\#\#.*\n----/g).reduce(make_headlines, ['## Questions asked to GPT', '----']).join('\n');
return [toc, body_text].join('\n\n').trim();
}
function h(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();}(() => {
const e = document.querySelectorAll(".text-base");let t = "";for (const s of e) s.querySelector(".whitespace-pre-wrap") &&(t += `**${s.querySelector("img") ? "You" : "ChatGPT"}**: ${h(s.querySelector(".whitespace-pre-wrap").innerHTML)}\n\n`);
const body = (CONFIG.encodeHTMLCHARS)? post_process(t.replace(/\&gt\;/g, ">").replace(/\&lt\;/g, "<")): post_process(t);
const o = document.createElement("a"); (o.download = CONFIG.DOC_TITLE + ".md"),(o.href = URL.createObjectURL(new Blob([body]))),(o.style.display = "none"),document.body.appendChild(o),o.click();
})();
})({
DOC_TITLE: "ChatGPT: Laravel Queue and jobs",
encodeHTMLCHARS: false,
});
@cshanejennings
Copy link
Author

configuration is set through the CONFIG object.
set DOC_TITLE to the desired name of the .md file
set encodeHTMLCHARS to true If you want < & > to display as < + >

@cshanejennings
Copy link
Author

This version creates a linked TOC at the top that will produce a list of direct links to user questions (to aid findability) when pasted into Obsidian.

@cshanejennings
Copy link
Author

Now adds a link back to the TOC via [[#Questions asked|Back to Questions]]

@cshanejennings
Copy link
Author

ChatGPT: is replaced with #unconfirmed-chatgpt for confirmation tracking via tagging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment