Skip to content

Commit

Permalink
Let claude generate markdown, by using its 'copy' button. Idea and co…
Browse files Browse the repository at this point in the history
…de thanks to @7shi in ryanschiang#5
  • Loading branch information
kmptkp committed Jul 21, 2024
1 parent 19820ac commit bf52f67
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/exportMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@ const getTimestamp = require("./util/getTimestamp");
const getContents = require("./util/getContents");
const { parse_ol } = require("./util/parse");

function mdFromCopyButton(el, md) {
async function mdFromCopyButton(topEl, md) {
var clip = navigator.clipboard;
if (!clip._writeText) clip._writeText = navigator.clipboard.writeText;
for (var copy of Array.from(topEl.nextSibling.getElementsByTagName("button")).filter(b => b.innerText == "Copy")) {
await new Promise((resolve, reject) => {
navigator.clipboard.writeText = async arg => {
md += arg.trimEnd() + "\n";
resolve();
};
try {
copy.click();
} catch (e) {
reject(e);
}
});
}
navigator.clipboard.writeText = navigator.clipboard._writeText;
return md
}

Expand Down Expand Up @@ -120,7 +136,7 @@ function mdFromDOM(topEl, md) {
return md
}

(function exportMarkdown() {
(async function exportMarkdown() {
var markdown = "";
// var elements = document.querySelectorAll("[class*='min-h-[20px]']");

Expand All @@ -146,10 +162,11 @@ function mdFromDOM(topEl, md) {
// Claude reponses have a different DOM structure than prompts
if (ele.classList.contains("font-claude-message")) {
markdown += `## Claude:\n`;
if (firstChild.tagName == 'DIV' && firstChild.childNodes.length == 1 && !!firstChild.classList) {
firstChild = firstChild.firstChild
}
markdown = mdFromDOM(firstChild, markdown)
markdown = await mdFromCopyButton(ele, markdown)
// if (firstChild.tagName == 'DIV' && firstChild.childNodes.length == 1 && !!firstChild.classList) {
// firstChild = firstChild.firstChild
// }
// markdown = mdFromDOM(firstChild, markdown)
} else {
markdown += `## Me:\n`;
markdown = mdFromDOM(ele, markdown)
Expand Down

0 comments on commit bf52f67

Please sign in to comment.