Skip to content

Commit

Permalink
feat(nesting)!: Works with multiple nesting levels
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeguimaraes committed Nov 14, 2023
1 parent 54da4da commit 53604c2
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@ import {
SettingSchemaDesc,
} from "@logseq/libs/dist/LSPlugin.user";

async function merge_nested_blocks(blockId: string) {
const block = await logseq.Editor.getBlock(blockId, {
function content_from_nested_blocks(parent_block: BlockEntity): string {
const newline = logseq.settings!["newlineBetweenBlocks"] ? "\n\n" : "\n";
const children = parent_block.children as BlockEntity[];

const content = children.reduce(function(acc: string, block: BlockEntity) {
if (block.children?.length > 0) {
return acc + newline + block.content + content_from_nested_blocks(block);
} else {
return acc + newline + block.content;
}
}, "")

return content
}

async function merge_nested_blocks(parent_block_id: string) {
const block = await logseq.Editor.getBlock(parent_block_id, {
includeChildren: true,
});
if (block === null || block.children?.length === 0) {
return;
}

const newline = logseq.settings!["newlineBetweenBlocks"] ? "\n\n" : "\n";

const children = block.children as BlockEntity[];
let content = children.reduce(function (acc, block) {
return acc + newline + block.content;
}, "");
const content = content_from_nested_blocks(block)

await logseq.Editor.insertBlock(block.uuid, content, {
before: false,
});

for (let child of children) {
for (let child of block.children as BlockEntity[]) {
await logseq.Editor.removeBlock(child.uuid);
}
}
Expand Down

0 comments on commit 53604c2

Please sign in to comment.