Skip to content

Commit

Permalink
fix: second attempt to fix #58
Browse files Browse the repository at this point in the history
Adds SyncronizationSafeLogseq Wrapper for accessing logseq api in order to fix #58
  • Loading branch information
debanjandhar12 committed May 14, 2022
1 parent 5029f6c commit 0e4f750
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 43 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"dependencies": {
"@logseq/libs": "0.0.6",
"cheerio": "1.0.0-rc.10",
"highlight.js": "^11.3.1",
"highlight.js": "11.5.1",
"lodash": "^4.17.21",
"mldoc": "1.3.2",
"mldoc": "1.3.3",
"ohm-js": "16.3.4",
"string-replace-async": "^3.0.2",
"localforage": "^1.10.0",
Expand Down
44 changes: 44 additions & 0 deletions src/SyncronizationSafeLogseq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { BlockEntity, BlockIdentity, EntityID, PageIdentity } from "@logseq/libs/dist/LSPlugin";
import AwaitLock from "await-lock";
/***
* Syncronization-safe logseq api wrapper for the Logseq plugin
* */

let getLogseqLock = new AwaitLock();
export namespace SyncronizationSafeLogseq {
export class Editor {
static async getBlock(srcBlock: BlockIdentity | EntityID, opts?: Partial<{ includeChildren: boolean; }>): Promise<BlockEntity | null> {
let block = null;
await getLogseqLock.acquireAsync();
try {block = await logseq.Editor.getBlock(srcBlock, opts);}
catch(e) { console.error(e); }
finally { getLogseqLock.release(); }
return block;
}

static async getPage(srcPage: PageIdentity | EntityID): Promise<PageIdentity | null> {
let page = null;
await getLogseqLock.acquireAsync();
try {page = await logseq.Editor.getPage(srcPage);}
catch(e) { console.error(e); }
finally { getLogseqLock.release(); }
return page;
}

static async getPageBlocksTree(srcPage: PageIdentity): Promise<Array<BlockEntity>> {
let pageBlockTree = null;
await getLogseqLock.acquireAsync();
try {pageBlockTree = await logseq.Editor.getPageBlocksTree(srcPage);}
catch(e) { console.error(e); }
finally { getLogseqLock.release(); }
return pageBlockTree;
}

static async upsertBlockProperty(block: BlockIdentity, key: string, value: any): Promise<void> {
await getLogseqLock.acquireAsync();
try {await logseq.Editor.upsertBlockProperty(block, key, value);}
catch(e) { console.error(e); }
finally { getLogseqLock.release(); }
}
}
}
9 changes: 5 additions & 4 deletions src/converter/Converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { decodeHTMLEntities, getRandomUnicodeString, safeReplace, safeReplaceAsy
import _ from 'lodash';
import { Mldoc } from 'mldoc';
import { ANKI_CLOZE_REGEXP, LOGSEQ_BLOCK_REF_REGEXP, LOGSEQ_EMBDED_BLOCK_REGEXP, LOGSEQ_EMBDED_PAGE_REGEXP, LOGSEQ_PAGE_REF_REGEXP, LOGSEQ_RENAMED_BLOCK_REF_REGEXP, MD_MATH_BLOCK_REGEXP, MD_PROPERTIES_REGEXP, ORG_MATH_BLOCK_REGEXP, ORG_PROPERTIES_REGEXP } from "../constants";
import { SyncronizationSafeLogseq } from "../SyncronizationSafeLogseq";

let mldocsOptions = {
"toc": false,
Expand Down Expand Up @@ -142,7 +143,7 @@ async function processEmbeds(htmlFile: HTMLFile, format: string = "markdown"): P

resultContent = await safeReplaceAsync(resultContent, LOGSEQ_EMBDED_PAGE_REGEXP, async (match, g1) => { // Convert block embed
let block_content = "";
try { let block = await logseq.Editor.getBlock(g1); block_content = _.get(block, "content").replace(ANKI_CLOZE_REGEXP, "$3").replace(/(?<!{{embed [^}\n]*?)}}/g, "} } ") || ""; } catch (e) { console.warn(e); }
try { let block = await SyncronizationSafeLogseq.Editor.getBlock(g1); block_content = _.get(block, "content").replace(ANKI_CLOZE_REGEXP, "$3").replace(/(?<!{{embed [^}\n]*?)}}/g, "} } ") || ""; } catch (e) { console.warn(e); }
return `<div class="embed-block">
<ul class="children-list"><li class="children">
${await (async () => {
Expand Down Expand Up @@ -177,7 +178,7 @@ async function processEmbeds(htmlFile: HTMLFile, format: string = "markdown"): P
result += `</ul>`;
return result;
}
try { pageTree = await logseq.Editor.getPageBlocksTree(pageName); } catch (e) { console.warn(e); }
try { pageTree = await SyncronizationSafeLogseq.Editor.getPageBlocksTree(pageName); } catch (e) { console.warn(e); }

return `<div class="embed-page">
<a href="logseq://graph/${encodeURIComponent(_.get(await logseq.App.getCurrentGraph(), 'name'))}?page=${encodeURIComponent(pageName)}" class="embed-header">${pageName}</a>
Expand All @@ -193,10 +194,10 @@ async function processEmbeds(htmlFile: HTMLFile, format: string = "markdown"): P
}); // Convert block ref link
resultContent = await safeReplaceAsync(resultContent, LOGSEQ_BLOCK_REF_REGEXP, async (match, blockUUID) => { // Convert block refs
let block;
try { block = await logseq.Editor.getBlock(blockUUID); }
try { block = await SyncronizationSafeLogseq.Editor.getBlock(blockUUID); }
catch (e) { console.warn(e); }
if (_.get(block, "properties.lsType") == "annotation" && _.get(block, "properties.hlType") == "area") { // Pdf area ref
let page = await logseq.Editor.getPage(block.page.id);
let page = await SyncronizationSafeLogseq.Editor.getPage(block.page.id);
let hls_img_loc = `../assets/${_.get(page, "originalName", "").replace("hls__", "")}/${_.get(block, "properties.hlPage")}_${blockUUID}_${_.get(block, "properties.hlStamp")}.png`;
resultAssets.add(hls_img_loc);
let img_html = `<img src="${encodeURIComponent(hls_img_loc)}" />`
Expand Down
10 changes: 3 additions & 7 deletions src/notes/ClozeNote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { string_to_arr, get_math_inside_md, safeReplace } from '../utils';
import _ from 'lodash';
import { MD_PROPERTIES_REGEXP, ORG_PROPERTIES_REGEXP } from "../constants";
import AwaitLock from 'await-lock';
import { SyncronizationSafeLogseq } from "../SyncronizationSafeLogseq";

export class ClozeNote extends Note {
public type: string = "cloze";
Expand Down Expand Up @@ -96,17 +97,12 @@ export class ClozeNote extends Note {
[(re-find ?regex ?content)]
]`);
let blocks: any = [...logseqCloze_blocks, ...replaceCloze_blocks, ...orgCloze_blocks];
let getBlockLock = new AwaitLock();
blocks = await Promise.all(blocks.map(async (block) => {
let uuid = block[0].uuid["$uuid$"] || block[0].uuid.Wd;
getBlockLock.acquireAsync();
let page = (block[0].page) ? await logseq.Editor.getPage(block[0].page.id) : {};
getBlockLock.release();
let page = (block[0].page) ? await SyncronizationSafeLogseq.Editor.getPage(block[0].page.id) : {};
block = block[0];
if(!block.content) {
getBlockLock.acquireAsync();
block = await logseq.Editor.getBlock(uuid);
getBlockLock.release();
block = await SyncronizationSafeLogseq.Editor.getBlock(uuid);
}
if(block)
return new ClozeNote(uuid, block.content, block.format, block.properties || {}, page);
Expand Down
17 changes: 5 additions & 12 deletions src/notes/MultilineCardNote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { convertToHTMLFile, HTMLFile } from '../converter/CachedConverter';
import { safeReplace } from '../utils';
import { ANKI_CLOZE_REGEXP, MD_PROPERTIES_REGEXP } from "../constants";
import AwaitLock from 'await-lock';
import { SyncronizationSafeLogseq } from "../SyncronizationSafeLogseq";

export class MultilineCardNote extends Note {
public type: string = "multiline_card";
Expand Down Expand Up @@ -121,25 +122,17 @@ export class MultilineCardNote extends Note {
[?b :block/refs ?p]
]`);
let blocks: any = [...logseqCard_blocks, ...flashCard_blocks];
let getBlockLock = new AwaitLock();

blocks = await Promise.all(blocks.map(async (block) => {
let uuid = block[0].uuid["$uuid$"] || block[0].uuid.Wd;
getBlockLock.acquireAsync();
let page = (block[0].page) ? await logseq.Editor.getPage(block[0].page.id) : {};
getBlockLock.release();
getBlockLock.acquireAsync();
block = await logseq.Editor.getBlock(uuid, { includeChildren: true });
getBlockLock.release();
let page = (block[0].page) ? await SyncronizationSafeLogseq.Editor.getPage(block[0].page.id) : {};
block = await SyncronizationSafeLogseq.Editor.getBlock(uuid, { includeChildren: true });
if (block) {
let tags = await Promise.all(_.map(block.refs, async page => {
getBlockLock.acquireAsync();
let tagPage = await logseq.Editor.getPage(page.id);
getBlockLock.release();
let tagPage = await SyncronizationSafeLogseq.Editor.getPage(page.id);
return _.get(tagPage, 'name')
}));
getBlockLock.acquireAsync();
let children = await this.augmentChildrenArray(block.children);
getBlockLock.release();
return new MultilineCardNote(uuid, block.content, block.format, block.properties || {}, page, tags, children);
} else {
return null;
Expand Down
15 changes: 8 additions & 7 deletions src/syncLogseqToAnki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import { MultilineCardNote } from './notes/MultilineCardNote';
import _ from 'lodash';
import { get_better_error_msg, confirm } from './utils';
import path from 'path';
import { MD_PROPERTIES_REGEXP } from './constants';
import { ANKI_CLOZE_REGEXP, MD_PROPERTIES_REGEXP } from './constants';
import { convertToHTMLFile } from './converter/CachedConverter';
import { SyncronizationSafeLogseq } from './SyncronizationSafeLogseq';

export class LogseqToAnkiSync {
static isSyncing: boolean;
Expand Down Expand Up @@ -49,7 +50,7 @@ export class LogseqToAnkiSync {
// -- Get the notes that are to be synced from logseq --
let notes : Array<Note> = [...(await ClozeNote.getNotesFromLogseqBlocks()), ...(await MultilineCardNote.getNotesFromLogseqBlocks())];
for (let note of notes) { // Force persistance of note's logseq block uuid accross re-index by adding id property to block in logseq
if (!note.properties["id"]) { try { logseq.Editor.upsertBlockProperty(note.uuid, "id", note.uuid); } catch (e) { console.error(e); } }
if (!note.properties["id"]) { try { SyncronizationSafeLogseq.Editor.upsertBlockProperty(note.uuid, "id", note.uuid); } catch (e) { console.error(e); } }
}
console.log("Notes:", notes);

Expand Down Expand Up @@ -190,9 +191,9 @@ export class LogseqToAnkiSync {
if(logseq.settings.includeParentContent) {
let newHtml = "";
let parentBlocks = [];
let parentID = (await logseq.Editor.getBlock(note.uuid)).parent.id;
let parentID = (await SyncronizationSafeLogseq.Editor.getBlock(note.uuid)).parent.id;
let parent;
while ((parent = await logseq.Editor.getBlock(parentID)) != null) {
while ((parent = await SyncronizationSafeLogseq.Editor.getBlock(parentID)) != null) {
parentBlocks.push({content:parent.content.replaceAll(MD_PROPERTIES_REGEXP, "").replaceAll(ANKI_CLOZE_REGEXP, "$3"), format:parent.format, uuid:parent.uuid});
parentID = parent.parent.id;
}
Expand All @@ -211,7 +212,7 @@ export class LogseqToAnkiSync {
try {
let parentID = note.uuid;
let parent;
while ((parent = await logseq.App.getBlock(parentID)) != null) {
while ((parent = await SyncronizationSafeLogseq.Editor.getBlock(parentID)) != null) {
if(_.get(parent, 'properties.deck') != null){
deck = _.get(parent, 'properties.deck');
break;
Expand All @@ -230,9 +231,9 @@ export class LogseqToAnkiSync {
if(logseq.settings.breadcrumbDisplay == "Show Page name and parent blocks context") {
try {
let parentBlocks = [];
let parentID = (await logseq.App.getBlock(note.uuid)).parent.id;
let parentID = (await SyncronizationSafeLogseq.Editor.getBlock(note.uuid)).parent.id;
let parent;
while ((parent = await logseq.App.getBlock(parentID)) != null) {
while ((parent = await SyncronizationSafeLogseq.Editor.getBlock(parentID)) != null) {
parentBlocks.push({content:parent.content.replaceAll(MD_PROPERTIES_REGEXP, "").replaceAll(ANKI_CLOZE_REGEXP, "$3"), uuid:parent.uuid});
parentID = parent.parent.id;
}
Expand Down
6 changes: 3 additions & 3 deletions src/templates/_logseq_anki_sync.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ h5 {
flex-direction: column;
padding-top: 2px;
list-style: none;
padding-left: 20px;
padding-left: 1.5rem;
}

.children {
Expand All @@ -89,7 +89,7 @@ h5 {
color: #d4d5d6;
cursor: pointer;
position: absolute;
left: -25px;
left: -2.1rem;
top: -1px;
font-size: 22px;
}
Expand All @@ -101,7 +101,7 @@ h5 {

:not(.embed-page):not(.embed-block) > .children-list > .children::after {
content: ' ';
left: -42px;
left: -3.3rem;
background-color: #e9ecef;
height: 100%;
width: 2px;
Expand Down
21 changes: 13 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
chalk "^2.0.0"
js-tokens "^4.0.0"

"@broxus/await-semaphore@0.1.5":
version "0.1.5"
resolved "https://registry.yarnpkg.com/@broxus/await-semaphore/-/await-semaphore-0.1.5.tgz#e5fcf641dfa71ffc29210b1138e2157a32b422d9"
integrity sha512-g464HtHHKxk6r6bkZc+lET1PuSgcFxlLHVb7EAqfxTlx6DRcYkjPRIQQhRHB3BQ6zRpfMrYkppL0tBtRED+HNA==

"@lezer/common@^0.15.0", "@lezer/common@^0.15.7":
version "0.15.12"
resolved "https://registry.yarnpkg.com/@lezer/common/-/common-0.15.12.tgz#2f21aec551dd5fd7d24eb069f90f54d5bc6ee5e9"
Expand Down Expand Up @@ -1139,10 +1144,10 @@ has-flag@^4.0.0:
resolved "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz"
integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==

highlight.js@^11.3.1:
version "11.3.1"
resolved "https://registry.npmjs.org/highlight.js/-/highlight.js-11.3.1.tgz"
integrity sha512-PUhCRnPjLtiLHZAQ5A/Dt5F8cWZeMyj9KRsACsWT+OD6OP0x6dp5OmT5jdx0JgEyPxPZZIPQpRN2TciUT7occw==
highlight.js@11.5.1:
version "11.5.1"
resolved "https://registry.yarnpkg.com/highlight.js/-/highlight.js-11.5.1.tgz#027c24e4509e2f4dcd00b4a6dda542ce0a1f7aea"
integrity sha512-LKzHqnxr4CrD2YsNoIf/o5nJ09j4yi/GcH5BnYz9UnVpZdS4ucMgvP61TDty5xJcFGRjnH4DpujkS9bHT3hq0Q==

htmlnano@^2.0.0:
version "2.0.0"
Expand Down Expand Up @@ -1353,10 +1358,10 @@ minimist@^1.2.0, minimist@^1.2.5:
resolved "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz"
integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==

mldoc@1.3.2:
version "1.3.2"
resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-1.3.2.tgz#d08bb6bd7a6ea6ae27cb02f4d5b4c8f7c95d31ba"
integrity sha512-yK/IfRwLpNEyFyU61wISho3ik2en/VVtY4wxZFfyasOX5xBvxcI/FBAqXmO4hV7V6paH8Ko9APgAW3AcX+CiOQ==
mldoc@1.3.3:
version "1.3.3"
resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-1.3.3.tgz#b7f39b48eb0ef3558619d3e3522265977bd78fe3"
integrity sha512-TzW06GBltdKxwWAxOvflPmIVedu6bzl9T4YoYqnDUyZ3kELFMllEgiYCh65PPW3xsRMA/5OcRQqqGZGiKEJEug==
dependencies:
yargs "^12.0.2"

Expand Down

0 comments on commit 0e4f750

Please sign in to comment.