Skip to content

Commit

Permalink
Merge pull request #5 from b-yp/dev
Browse files Browse the repository at this point in the history
feat: Support recursive retrieval of all child node content under the…
  • Loading branch information
b-yp committed Feb 2, 2024
2 parents 0206a7c + 02cb233 commit 28a973d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "@logseq/libs";
import { BlockEntity, BlockUUID, IHookEvent } from "@logseq/libs/dist/LSPlugin.user";

import { extractCodeBlockFromMarkdown } from './utils'

import { deepFirstTraversal, extractCodeBlockFromMarkdown } from './utils'
import { logseq as PL } from "../package.json";

const pluginId = PL.id;
Expand Down Expand Up @@ -54,17 +54,24 @@ const getBlockTags = (content: string): Promise<{ result: string }> => {
})
}

const setBlockTags = async (e: any) => {
const setBlockTags = async (e: IHookEvent & { uuid: BlockUUID }) => {
const block = await logseq.Editor.getBlock(e.uuid)
if (block?.content) {
const res = await getBlockTags(block?.content)
const contents: string[] = []

if (!block) return
await deepFirstTraversal([block], async (obj: BlockEntity) => {
contents.push(obj.content)
})

if (contents.length) {
const res = await getBlockTags(contents.join('\n'))
const tags = eval(extractCodeBlockFromMarkdown(res.result))
await logseq.Editor.updateBlock(block?.uuid, `${block.content} ${tags.map((i: string) => `#${hasSpace(i) ? `[[${i}]]` : i}`).join(' ')}`)
logseq.Editor.exitEditingMode()
}
}

const setPageTags = async (e: any) => {
const setPageTags = async (e: IHookEvent & { page: string }) => {
const page = await logseq.Editor.getPage(e.page)
const tree = await logseq.Editor.getPageBlocksTree(e.page)
if (!tree.length) return
Expand Down
36 changes: 36 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BlockEntity } from "@logseq/libs/dist/LSPlugin.user";

/**
* Get the code block content of MarkDown.
* @param markdownText
Expand All @@ -12,3 +14,37 @@ export const extractCodeBlockFromMarkdown = (markdownText: string): string => {

return '';
}

/**
* @param arr BlockEntity[]
* @param fn (block: BlockEntity) => void
*/
export const deepFirstTraversal = async (arr: BlockEntity[] | Array<string>, fn: (block: BlockEntity) => void): Promise<void> => {
// Create an array to store promises for each recursive call
const promises: Promise<void>[] = [];

// Use for...of loop to handle asynchronous operations correctly
for (const obj of arr) {
const promise = (async () => {
if (obj instanceof Array && obj[0] === 'uuid') {
const block = await logseq.Editor.getBlock(obj[1]);
if (block) {
fn(block);
// Recursively call deepFirstTraversal and wait for it to complete
await deepFirstTraversal((block.children || []) as unknown as Array<string>, fn);
}
} else {
const block = obj as BlockEntity;
fn(block);
// Recursively call deepFirstTraversal and wait for it to complete
await deepFirstTraversal((block.children || []) as unknown as Array<string>, fn);
}
})();

promises.push(promise);
}

// Wait for all promises to resolve before resolving the main promise
await Promise.all(promises);
};

0 comments on commit 28a973d

Please sign in to comment.