-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
gpt2md.js
36 lines (30 loc) · 1.22 KB
/
gpt2md.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const TurndownService = require('turndown');
const ts = new TurndownService();
// Clone to not modify the actual `document.body` in the code that follows
const body = document.body.cloneNode(true);
// Remove code box headers
body.querySelectorAll('pre .text-xs').forEach(n => n.parentNode?.removeChild(n));
// Remove prompt/response numbers
body.querySelectorAll('div .text-xs.gap-1').forEach(n => n.parentNode?.removeChild(n));
// Remove footer
body.querySelector('.absolute.bottom-0').remove()
// Iterate through main text containers and create text to export
let text = `# ${document.title}\n\n`;
body.querySelectorAll('.text-message').forEach((n, i) => {
const num = Math.trunc(i / 2) + 1;
const prose = n.querySelector('.prose');
if (prose) {
// Only convert response markup to markdown
text += `## RESPONSE ${num}\n\n${ts.turndown(prose.innerHTML)}\n\n`;
} else {
// Keep prompt text as it was entered
text += `## PROMPT ${num}\n\n${n.querySelector('div').innerText}\n\n`;
}
});
// Download
const a = document.createElement('a');
a.download = `${document.title}.md`;
a.href = URL.createObjectURL(new Blob([text]));
a.style.display = 'none';
document.body.appendChild(a);
a.click();