forked from sawhney17/logseq-github-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
122 lines (109 loc) · 3.87 KB
/
index.ts
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
import '@logseq/libs';
import { SettingSchemaDesc } from '@logseq/libs/dist/LSPlugin.user';
// import 'octokit'
import { Octokit, App } from 'octokit'
let githubToken = process.env.GITHUBAUTHENTICATIONTOKEN
const octokit = new Octokit({ auth: `${githubToken}` });
//Inputs 5 numbered blocks when called
var blockArray
const pageName = "Github"
let settings: SettingSchemaDesc[] = [
{
key: "API Key",
type: "string",
title: "Enter github personal access token",
description: "Enter your personal access token here",
default: "user:sawhney17"
},
{
key: "SearchQuery",
type: "string",
title: "Enter github search query",
description: "Enter your desired search query here",
default: "user:sawhney17"
},
{
key: "TargetPage",
type: "string",
title: "Enter target page",
description: "Enter your desired page, to where the blocks will be inserted",
default: "Github Issues"
},
{
key: "Block1InsertionTemplate",
type: "string",
title: "Insertion template for block 1",
description: "Enter your desired template for the parent block, created by default for every return value of the query ",
default: "TODO [Title](URL)"
},
{
key: "Block2InsertionTemplate",
type: "string",
title: "Insertion template for block 2",
description: "Enter your desired template for the child block, created by default for every return value of the query ",
default: "{Body}"
}
]
logseq.useSettingsSchema(settings)
function updateSettings() {
blockArray = logseq.settings.blockTracker
}
function syncSettings() {
logseq.updateSettings({blockTracker: blockArray })
}
async function fetchGithubIssues(e) {
console.log('hi')
console.log()
octokit.request('GET /search/issues', {
q: `${logseq.settings.SearchQuery}`,
}).then((response) => { insertBlocks(response) })
}
function applyTemplate(response, inputString) {
var finalString = inputString
console.log("Repo")
finalString = finalString.replaceAll(/{Title}/gi, response.title)
finalString = finalString.replaceAll(/{URL}/gi, response.html_url)
finalString = finalString.replaceAll(/{Body}/gi, response.body)
console.log(response.repository_url.split("repo/"[1]))
// finalString = finalString.replaceAll(/{Repo}/gi, response)
return finalString
}
async function insertBlocks(response) {
updateSettings()
console.log(response.data.items)
for (const dataPoint in response.data.items) {
console.log(response.data.items[dataPoint].id)
syncSettings()
if (blockArray.indexOf(response.data.items[dataPoint].id) == -1) {
blockArray.push(response.data.items[dataPoint].id)
syncSettings()
console.log(dataPoint)
let page = await logseq.Editor.createPage(logseq.settings.TargetPage, {}, { redirect: true })
let block1Text = applyTemplate(response.data.items[dataPoint], logseq.settings.Block1InsertionTemplate)
// console.log(block1Text)
console.log(block1Text)
let block1 = await logseq.Editor.insertBlock(page.name, block1Text, { isPageBlock: true })
logseq.Editor.updateBlock(block1.uuid, `${block1.content}\nid:: ${block1.uuid}\ncollapsed:: true`)
if (logseq.settings.Block2InsertionTemplate != "") {
let block2Text = applyTemplate(response.data.items[dataPoint], logseq.settings.Block2InsertionTemplate)
let block2 = await logseq.Editor.insertBlock(block1.uuid, `${block2Text}`, { sibling: false })
}
}
}
logseq.App.pushState("page", {
name: logseq.settings.TargetPage,
})
}
const main = async () => {
console.log('plugin loaded');
logseq.onSettingsChanged(updateSettings)
logseq.Editor.registerSlashCommand('Github Sync', async (e) => {
fetchGithubIssues(e)
}
)
if (logseq.settings.blockTracker == undefined) {
logseq.updateSettings({ blockTracker: [] })
console.log(logseq.settings.blockTracker)
}
}
logseq.ready(main).catch(console.error);