48 lines
1.9 KiB
JavaScript
48 lines
1.9 KiB
JavaScript
function captureFrontMatterAndMarkdown() {
|
|
console.log(location.pathname.split('/public')[0])
|
|
const formData = new FormData(document.querySelector("form"))
|
|
let frontmatter = "---\n"
|
|
for (const k of formData.keys()) {
|
|
// draft, iscjklanguage are default implicit (false) params in hugo
|
|
if (["draft", "iscjklanguage"].includes(k)){
|
|
if (formData.get(k) === "true") {
|
|
frontmatter += `${k}: true\n`
|
|
}
|
|
} else if (k.endsWith('[]')) {
|
|
formData.set(k, formData.getAll(k))
|
|
var s = JSON.stringify(formData.getAll(k)[0].split(" ☒").join("").split(","))
|
|
frontmatter += `${k.slice(0, -2)}: ${s}\n`
|
|
} else if (k == "relpath") {
|
|
filepath = `${location.pathname.split('/public')[0]}/content/${formData.get(k)}`
|
|
gitpath = `${location.pathname.split('/public')[0]}`
|
|
} else if (k == "relpermalink") {
|
|
publishpath = `${location.pathname.split('/public')[0]}/public${formData.get(k)}index.html`
|
|
} else if (["protocol", "offline", "ignore"].includes(k)) {
|
|
continue
|
|
} else {
|
|
var s = JSON.stringify(formData.get(k))
|
|
frontmatter += `${k}: ${s}\n`
|
|
}
|
|
}
|
|
frontmatter += "---\n"
|
|
const content = document.querySelector('textarea').value
|
|
let doc = frontmatter + "\n" + content
|
|
let publish = formData.get("publish") == "on" ? true : false
|
|
let offline = formData.get("offline") == "on" ? true : false
|
|
return {"hugopage": doc, "filepath": filepath, "gitpath": gitpath, "publishpath": publishpath, "publish": publish, "offline": offline, "protocol": document.location.protocol.substr(0,4)}
|
|
}
|
|
|
|
let sb = document.getElementById('sandpointsButton')
|
|
if (sb) {
|
|
sb.addEventListener(
|
|
"click", (e)=> {
|
|
e.preventDefault()
|
|
pageDict = captureFrontMatterAndMarkdown()
|
|
chrome.runtime.sendMessage(pageDict)
|
|
});
|
|
}
|
|
|
|
chrome.runtime.onMessage.addListener((m, n)=> {
|
|
window.location.replace(m.msg)
|
|
})
|