-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
141 lines (96 loc) · 3.34 KB
/
popup.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
let revealQuarto = document.getElementById("revealQuarto")
revealQuarto.addEventListener("click", async () => {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true })
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: quartize,
args: [ tab.url ]
})
})
function quartize(tabUrl) {
function shquote(s) {
if (s === '') { return '\'\'' }
const unsafeRe = /[^\w@%\-+=:,./]/
if (!unsafeRe.test(s)) { return s }
return ('\'' + s.replace(/('+)/g, '\'"$1"\'') + '\'').replace(/^''|''$/g, '')
}
ohq_json = JSON.parse(document.getElementById("__NEXT_DATA__").innerText)
nb = ohq_json.props.pageProps.initialNotebook
nodes = nb.nodes
title = nb.title
authors = nb.authors.map(d => d.name).join(", ")
quoted_title = shquote(title)
qmd = "---\n"
qmd += `title: ${quoted_title}\n`
qmd += `author: "${authors}"\n`
qmd += `format: html\n`
qmd += "echo: false\n"
qmd += `observable: "${tabUrl}"\n`
qmd += "---\n\n"
qmd += nodes.map(node => {
cell = "```{ojs}\n"
if (node.mode == "md") {
cell += "md`" + node.value + "`\n"
} else if (node.mode == "html") {
cell += "html`" + node.value + "`\n"
} else {
cell += node.value + "\n"
}
cell += "```\n"
return(cell)
}).join("\n")
margin = "5%"
var element = document.querySelector('html');
element.remove()
var div = document.createElement('div')
div.style.marginLeft = margin
div.style.marginRight = margin
div.style.marginTop = margin / 2
doc = "<style>p, a { font-family: sans-serif; } a { font-size: 14pt }</style>"
file_html = ""
const qmd_blob = new Blob([ qmd ], { type: 'text/markdown' });
const qmd_url = URL.createObjectURL(qmd_blob);
const title_quoted = shquote(title)
const yaml_blob = new Blob([ "project:\n" + ` title: ${title_quoted}` ], { type: 'text/yaml' });
const yaml_url = URL.createObjectURL(yaml_blob);
var zip = new JSZip()
zip.file(`${nb.slug}/_quarto.yml`, yaml_blob);
zip.file(`${nb.slug}/${nb.slug}.qmd`, qmd_blob);
if (nb.files.length > 0) {
Promise.all(
nb.files.map(file => fetch(file.download_url).then(response => {
return({ name: file.name, blob: response.blob() })
}))
).then(files => {
files.forEach(file => {
zip.file(`${nb.slug}/${file.name}`, file.blob);
})
zip.generateAsync({ type: "blob" })
.then(function (content) {
saveAs(content, `${nb.slug}.zip`)
}, function (err) {
console.log(err)
})
})
file_html = "<p>File Attachments:</p>\n" + "<ul>" + nb.files.map(file =>
`<li><a href="${file.download_url}" download="${file.name}"><code>${file.name}</code></a></li>`
).join("\n") + "</ul>\n"
} else {
zip.generateAsync({ type: "blob" })
.then(function (content) {
saveAs(content, `${nb.slug}.zip`)
}, function (err) {
console.log(err)
});
}
console.log(zip)
div.innerHTML = doc + file_html +
`<p>Quarto Document: <a download="${nb.slug}.qmd" href="${qmd_url}"><code>${nb.slug}.qmd</code></a></p>`
var pre = document.createElement('pre')
pre.style.padding = "11pt"
pre.style.whiteSpace = "pre-wrap"
pre.style.border = "0.5px solid black"
pre.innerText = qmd
div.append(pre)
document.getRootNode().appendChild(div)
}