diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d41d891..3e160b3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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: diff --git a/icon.png b/icon.png new file mode 100644 index 0000000..2140a81 Binary files /dev/null and b/icon.png differ diff --git a/index.ts b/index.ts index 0525f0b..93f02ec 100644 --- a/index.ts +++ b/index.ts @@ -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; } @@ -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, }); } diff --git a/package.json b/package.json index b341283..77ff4fb 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -22,6 +22,7 @@ "@logseq/libs": "^0.0.1-alpha.27" }, "logseq": { - "id": "_olkce22ke" + "id": "logseq-block-to-page", + "icon": "./icon.png" } } \ No newline at end of file diff --git a/util.ts b/util.ts new file mode 100644 index 0000000..501f952 --- /dev/null +++ b/util.ts @@ -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, + })); +}