From 122f79779e21e20a71d48fc9fa8cbbac425e4eab Mon Sep 17 00:00:00 2001 From: Trey Moore Date: Wed, 3 Feb 2021 18:52:09 -0700 Subject: [PATCH] Create a variable for tracking notebooks filtered out of the graph. #13 --- src/data.ts | 58 ++++++++++++++++++++++++++++++++++++ src/index.ts | 84 ++++++++++++++++------------------------------------ 2 files changed, 83 insertions(+), 59 deletions(-) create mode 100644 src/data.ts diff --git a/src/data.ts b/src/data.ts new file mode 100644 index 0000000..6498011 --- /dev/null +++ b/src/data.ts @@ -0,0 +1,58 @@ +import joplin from 'api'; + +export async function getNotebooks(): Promise> { + var allNotebooks = [] + var page_num = 1; + do { + var notebooks = await joplin.data.get(['folders'], { + fields: ['id', 'title'], + page: page_num, + }); + allNotebooks.push(...notebooks.items); + page_num++; + } while (notebooks.has_more) + + return allNotebooks; +} + +// Fetches every note. +export async function getNotes(): Promise> { + var allNotes = [] + var page_num = 1; + const maxNotes = await joplin.settings.value("maxNodesOnGraph") + do { + // `parent_id` is the ID of the notebook containing the note. + var notes = await joplin.data.get(['notes'], { + fields: ['id', 'parent_id', 'title', 'body'], + order_by: 'updated_time', + order_dir: 'DESC', + limit: maxNotes < 100 ? maxNotes : 100, + page: page_num, + }); + allNotes.push(...notes.items); + page_num++; + } while (notes.has_more && allNotes.length < maxNotes) + + const noteMap = new Map(); + for (const note of allNotes) { + var links = getAllLinksForNote(note.body); + noteMap.set(note.id, {title: note.title, parent_id: note.parent_id, links: links}) + } + return noteMap; +} + +function getAllLinksForNote(noteBody:string) { + const links = []; + // TODO: needs to handle resource links vs note links. see 4. Tips note for + // webclipper screenshot. + // https://stackoverflow.com/questions/37462126/regex-match-markdown-link + const linkRegexp = (/\[\]|\[.*?\]\(:\/(.*?)\)/g); + var match = linkRegexp.exec(noteBody); + while (match != null) { + if (match[1] !== undefined) { + links.push(match[1]); + } + match = linkRegexp.exec(noteBody); + } + return links; +} diff --git a/src/index.ts b/src/index.ts index 1a8cc74..9a2a4b8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,50 +1,10 @@ import joplin from 'api'; +import * as joplinData from './data'; import { SettingItemType, ToolbarButtonLocation } from 'api/types'; var deepEqual = require('deep-equal') const DEFAULT_MAX_NOTES = 700; -function getAllLinksForNote(noteBody:string) { - const links = []; - // TODO: needs to handle resource links vs note links. see 4. Tips note for - // webclipper screenshot. - // https://stackoverflow.com/questions/37462126/regex-match-markdown-link - const linkRegexp = (/\[\]|\[.*?\]\(:\/(.*?)\)/g); - var match = linkRegexp.exec(noteBody); - while (match != null) { - if (match[1] !== undefined) { - links.push(match[1]); - } - match = linkRegexp.exec(noteBody); - } - return links; -} - -// Fetches every note. -async function getNotes(): Promise> { - var allNotes = [] - var page_num = 1; - const maxNotes = await joplin.settings.value("maxNodesOnGraph") - do { - var notes = await joplin.data.get(['notes'], { - fields: ['id', 'title', 'body'], - order_by: 'updated_time', - order_dir: 'DESC', - limit: maxNotes < 100 ? maxNotes : 100, - page: page_num, - }); - allNotes.push(...notes.items); - page_num++; - } while (notes.has_more && allNotes.length < maxNotes) - - const noteMap = new Map(); - for (const note of allNotes) { - var links = getAllLinksForNote(note.body); - noteMap.set(note.id, {title: note.title, links: links}) - } - return noteMap; -} - async function createSettings() { await joplin.settings.registerSection('graph-ui.settings', { label: 'Graph UI', @@ -63,29 +23,35 @@ async function createSettings() { } +// Set of notebook IDs to filter out of the graph view. +var filteredNotebooks = new Set; async function fetchData() { - const note = await joplin.workspace.selectedNote(); - const notes = await getNotes() + const selectedNote = await joplin.workspace.selectedNote(); + const notes = await joplinData.getNotes(); + const notebooks = await joplinData.getNotebooks(); const data = { "nodes": [], "edges": [], - "currentNoteID": note.id, - } + "currentNoteID": selectedNote.id, + }; + + notes.forEach(function(note, id) { + if (!filteredNotebooks.has(note.parent_id)) { + data.nodes.push({ + "id": id, + "title": note.title, + }) - notes.forEach(function(value, id) { - data.nodes.push({ - "id": id, - "title": value.title, - }) - var links = value["links"] - if (links.length > 0) { - for (const link of links) { - // Ignore links that don't link to notes. - if (notes.has(link)) { - data.edges.push({ - "source": id, - "target": link, - }); + var links = note["links"] + if (links.length > 0) { + for (const link of links) { + var linkDestExists = notes.has(link); + if (linkDestExists) { + data.edges.push({ + "source": id, + "target": link, + }); + } } } }