Skip to content

Commit

Permalink
fix: improve note sync efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
hegerdes committed Jun 13, 2023
1 parent d127891 commit a6c5bd4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/notes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import joplin from "api";
import {http_options, note_marker, owner_repo, sync_note_regex} from "./const";
import { getAllNotes, isValidUrl, patchMDLinks } from "./util";
import { getAllNotes, isValidUrl, patchMDLinks, sleep } from "./util";
import { makeRequest } from "./requests";

const md_pic_links_regex = /!\[[^\]]*\]\((.*?)\s*("(?:.*[^"])")?\s*\)/g;
Expand Down Expand Up @@ -49,7 +49,7 @@ export const createTitle = async (body: string): Promise<string> => {
// Generate body
export const createBody = async (orgBody: string, url: string): Promise<string> => {
// add marker and check body for relative links
let remote_sync_footer = "\n_" + note_marker + url + "_\n";
let remote_sync_footer = "\n*" + note_marker + url + "*\n";
let body = orgBody + remote_sync_footer;
let base_url = url.substring(0, url.lastIndexOf("/")) + "/";
let matches = [];
Expand All @@ -70,10 +70,10 @@ export const updateNote = async (note: JoplinNote, batch = false) => {

// Keep in mind that it can be `null` if nothing is currently selected!
if (note) {
console.debug(`Updating note: ${note.id} - ${note.title}`);
const matches = note.body.match(sync_note_regex);
if (matches && matches.length > 0 && isValidUrl(note.source_url)) {
console.debug(`Trying to update note: ${note.title}`);
console.debug(`Updating note: ${note.id} - ${note.title}`);
console.debug("Regex matches, tying to update note ", note.title, matches);
let res = (await makeRequest(note.source_url, http_options)).toString();
let body = await createBody(res, note.source_url);

Expand All @@ -85,7 +85,7 @@ export const updateNote = async (note: JoplinNote, batch = false) => {
if (!batch) alert(`Note ${note.title} updated!`);
} else {
console.info("Update Note: Not a sync Note");
alert("This is not a RemoteSynced note!");
if (!batch) alert("This is not a RemoteSynced note!");
}
} else {
console.warn("NoteUpdate: No note is selected");
Expand All @@ -104,11 +104,12 @@ export const updateNotes = async () => {
const all_notes = await getAllNotes();

// Go through all notes and search for note_marker
console.debug(`Will iterate through ${all_notes.size} notes and update every sync note`)
all_notes.forEach(async (note: JoplinNote, _key: string) => {
const matches = note.body.match(sync_note_regex);
console.debug("Regex matches for note ", note.title, matches);
if (matches) {
await updateNote(note, true);
}
});
if(isValidUrl(note.source_url)){
await updateNote(note, true);
await sleep(5);
}
});
console.debug("Update done")
};
1 change: 1 addition & 0 deletions src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const createSettings = async () => {
if (await joplin.settings.value("joplin_md_pull_sync_enabled")) {
joplin_md_pull_sync_interval = setInterval(updateNotes, interval * min_to_ms);
}
console.debug(`Intervall set to ${interval} mins`)
};

export const createButtons = async () => {
Expand Down
4 changes: 4 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,7 @@ export const isValidUrl = (urlString: string): boolean => {
return false;
}
};

export const sleep = (sek: number) => {
return new Promise(resolve => setTimeout(resolve, sek * 1000));
}

0 comments on commit a6c5bd4

Please sign in to comment.