Skip to content

Commit

Permalink
fix: block should be simple
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrijk committed Oct 1, 2021
1 parent 600ab64 commit 6aada73
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- run: yarn build
- uses: montudor/action-zip@v1
with:
args: zip -qq -r logseq-plugin-block-to-page.zip dist demo README.md package.json LICENSE
args: zip -qq -r logseq-plugin-block-to-page.zip dist demo README.md package.json LICENSE icon.png
- name: Release
run: npx semantic-release
env:
Expand Down
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 10 additions & 11 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import "@logseq/libs";
import {
BlockEntity,
BlockIdentity,
IBatchBlock,
} from "@logseq/libs/dist/LSPlugin.user";
import { BlockEntity, BlockIdentity } from "@logseq/libs/dist/LSPlugin.user";
import { isSimpleBlock, toBatchBlocks } from "./util";

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

if (!isSimpleBlock(block)) {
logseq.App.showMsg("block has properties or is multi-line", "warning");
return;
}

Expand Down Expand Up @@ -55,15 +57,12 @@ async function insertBatchBlock(
srcBlock: BlockIdentity,
blocks: BlockEntity[]
) {
// children: [] 会出错
const batchBlocks = blocks.map((c) => ({
content: c.content,
children: c.children?.length ? (c.children as IBatchBlock[]) : undefined,
}));
const batchBlocks = toBatchBlocks(blocks);

debug("insertBatchBlock", srcBlock, batchBlocks);
await logseq.Editor.insertBatchBlock(srcBlock, batchBlocks, {
sibling: true,
before: false,
});
}

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logseq-plugin-block-to-page",
"version": "0.0.1",
"version": "0.0.2",
"description": "turn block into page",
"main": "dist/index.html",
"scripts": {
Expand All @@ -22,6 +22,7 @@
"@logseq/libs": "^0.0.1-alpha.27"
},
"logseq": {
"id": "_olkce22ke"
"id": "logseq-block-to-page",
"icon": "./icon.png"
}
}
27 changes: 27 additions & 0 deletions util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { BlockEntity } from "@logseq/libs/dist/LSPlugin.user";

export function isSimpleBlock(block: BlockEntity) {
if (hasProperties(block)) {
return false;
}
if (block.content.includes("\n")) {
return false;
}
return true;
}

function hasProperties(block: BlockEntity) {
const properties = block.meta?.properties || {};
return Object.keys(properties).length > 0;
}

export function toBatchBlocks(blocks: BlockEntity[]) {
return blocks.map((c) => ({
content: c.content,
// children: [] 会出错
children: c.children?.length
? toBatchBlocks(c.children as BlockEntity[])
: undefined,
properties: c.properties,
}));
}

0 comments on commit 6aada73

Please sign in to comment.