aboutsummaryrefslogtreecommitdiff
path: root/index.ts
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2023-11-02 23:55:13 +0200
committeromagdy7 <omar.professional8777@gmail.com>2023-11-02 23:55:13 +0200
commitc38d70c1752234bfc041c7fd5d762eec83cfbb9b (patch)
tree3a94ec905db50dc2f217d0390c718dc17272d2ba /index.ts
downloadollama-logseq-c38d70c1752234bfc041c7fd5d762eec83cfbb9b.tar.xz
ollama-logseq-c38d70c1752234bfc041c7fd5d762eec83cfbb9b.zip
First commit
Diffstat (limited to 'index.ts')
-rw-r--r--index.ts89
1 files changed, 89 insertions, 0 deletions
diff --git a/index.ts b/index.ts
new file mode 100644
index 0000000..a6ec1b1
--- /dev/null
+++ b/index.ts
@@ -0,0 +1,89 @@
+import '@logseq/libs'
+import { LSPluginBaseInfo } from '@logseq/libs/dist/libs'
+
+const delay = (t = 100) => new Promise(r => setTimeout(r, t))
+
+async function loadRedditData () {
+ const endpoint = 'https://www.reddit.com/r/logseq/hot.json'
+
+ const { data: { children } } = await fetch(endpoint).then(res => res.json())
+ const ret = children || []
+
+ return ret.map(({ data }, i) => {
+ const { title, selftext, url, ups, downs, num_comments } = data
+
+ return `${i}. [${title}](${url}) [:small.opacity-50 "🔥 ${ups} 💬 ${num_comments}"]
+collapsed:: true
+> ${selftext}`
+ })
+}
+
+/**
+ * main entry
+ * @param baseInfo
+ */
+function main (baseInfo: LSPluginBaseInfo) {
+ let loading = false
+
+ logseq.provideModel({
+ async loadReddits () {
+
+ const info = await logseq.App.getUserConfigs()
+ if (loading) return
+
+ const pageName = 'reddit-logseq-hots-news'
+ const blockTitle = (new Date()).toLocaleString()
+
+ logseq.App.pushState('page', { name: pageName })
+
+ await delay(300)
+
+ loading = true
+
+ try {
+ const currentPage = await logseq.Editor.getCurrentPage()
+ if (currentPage?.originalName !== pageName) throw new Error('page error')
+
+ const pageBlocksTree = await logseq.Editor.getCurrentPageBlocksTree()
+ let targetBlock = pageBlocksTree[0]!
+
+ targetBlock = await logseq.Editor.insertBlock(targetBlock.uuid, '🚀 Fetching r/logseq ...', { before: true })
+
+ let blocks = await loadRedditData()
+
+ blocks = blocks.map(it => ({ content: it }))
+
+ await logseq.Editor.insertBatchBlock(targetBlock.uuid, blocks, {
+ sibling: false
+ })
+
+ await logseq.Editor.updateBlock(targetBlock.uuid, `## 🔖 r/logseq - ${blockTitle}`)
+ } catch (e) {
+ logseq.App.showMsg(e.toString(), 'warning')
+ console.error(e)
+ } finally {
+ loading = false
+ }
+ }
+ })
+
+ logseq.App.registerUIItem('toolbar', {
+ key: 'logseq-reddit',
+ template: `
+ <a data-on-click="loadReddits"
+ class="button">
+ <i class="ti ti-brand-reddit"></i>
+ </a>
+ `
+ })
+
+ logseq.provideStyle(`
+ [data-injected-ui=logseq-reddit-${baseInfo.id}] {
+ display: flex;
+ align-items: center;
+ }
+ `)
+}
+
+// bootstrap
+logseq.ready(main).catch(console.error)