|
// adapted from https://forums.getdrafts.com/t/script-step-post-to-github-without-working-copy/3594 |
|
// post to writing inbox in Foam digital garden |
|
|
|
/* |
|
* edit these lines to suit your preferences |
|
*/ |
|
const inboxFolder = "inbox/"; // the folder in your Foam repo where notes are saved. MUST have trailing slash, except for root of repo use '' |
|
const requiredTags = ['inbox']; // all documents will have these added in addition to tags from the Drafts app |
|
const addLinkToInbox = true; // true = created note will have link to [[index]], false = no link |
|
const addTimeStamp = true; // true = add a note of capture date/time at foot of note |
|
|
|
/* |
|
* stop editing |
|
*/ |
|
|
|
const credential = Credential.create("GitHub garden repo", "The repo name, and its credentials, hosting your Foam notes"); |
|
credential.addTextField("username", "GitHub Username"); |
|
credential.addTextField('repo', 'Repo name'); |
|
credential.addPasswordField("key", "GitHub personal access token"); |
|
credential.addTextField('author', 'Author'); |
|
credential.authorize(); |
|
|
|
const githubKey = credential.getValue('key'); |
|
const githubUser = credential.getValue('username'); |
|
const repo = credential.getValue('repo'); |
|
const author = credential.getValue('author'); |
|
|
|
const http = HTTP.create(); // create HTTP object |
|
const base = 'https://api.github.com'; |
|
|
|
|
|
const posttime = new Date(); |
|
const title = draft.title; |
|
const txt = draft.processTemplate("[[line|3..]]"); |
|
const mergedTags = [...draft.tags, ...requiredTags]; |
|
const slugbase = title.toLowerCase().replace(/\s/g, "-"); |
|
|
|
const datestr = `${posttime.getFullYear()}-${pad(posttime.getMonth() + 1)}-${pad(posttime.getDate())}`; |
|
const timestr = `${pad(posttime.getHours())}:${pad(posttime.getMinutes())}:00`; |
|
const yr = `${posttime.getFullYear()}`; |
|
const pdOffset = posttime.getTimezoneOffset(); |
|
const offsetChar = pdOffset >= 0 ? '-' : '+'; |
|
var pdHours = Math.floor(pdOffset/60); |
|
console.log(pdHours); |
|
pdHours = pdHours >= 0 ? pdHours : pdHours * -1; |
|
console.log(pdHours); |
|
const tzString = `${offsetChar}${pad(pdHours)}:00`; |
|
const postdate = `${datestr}T${timestr}${tzString}`; |
|
|
|
|
|
const slug = `${slugbase}` |
|
const fn = `${slug}.md`; |
|
let preamble = `# ${title} \n\n`; |
|
|
|
mergedTags.forEach(function(item,index){ |
|
preamble += `#${item} `; |
|
} |
|
); |
|
|
|
if (addLinkToInbox) { |
|
preamble += "\n\n[[inbox]]\n"; |
|
} |
|
|
|
preamble += "\n\n"; |
|
|
|
var doc = `${preamble}${txt}`; |
|
|
|
if (addTimeStamp){ |
|
|
|
doc += `\n\nCaptured: ${postdate}\n` |
|
} |
|
|
|
const options = { |
|
url: `https://api.github.com/repos/${githubUser}/${repo}/contents/${inboxFolder}${fn}`, |
|
method: 'PUT', |
|
data: { |
|
message: `Inbox from Drafts ${datestr}`, |
|
content: Base64.encode(doc) |
|
}, |
|
headers: { |
|
'Authorization': `token ${githubKey}` |
|
} |
|
}; |
|
|
|
var response = http.request(options); |
|
|
|
if (response.success) { |
|
// yay |
|
} else { |
|
console.log(response.statusCode); |
|
console.log(response.error); |
|
} |
|
|
|
function pad(n) { |
|
let str = String(n); |
|
while (str.length < 2) { |
|
str = `0${str}`; |
|
} |
|
return str; |
|
} |