From 39d3a8c44670da23a411425124723e0a17ad0740 Mon Sep 17 00:00:00 2001 From: hyrijk Date: Sun, 10 Oct 2021 23:35:58 +0800 Subject: [PATCH] feat: add reference check #1 --- index.ts | 11 ++++++++--- package.json | 2 +- util.ts | 19 ++++++++++++++++--- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/index.ts b/index.ts index dc72a6d..e62fff3 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,6 @@ import "@logseq/libs"; import { BlockEntity, BlockIdentity } from "@logseq/libs/dist/LSPlugin.user"; -import { toBatchBlocks } from "./util"; +import { toBatchBlocks, mayBeReferenced } from "./util"; async function main(blockId: string) { const block = await logseq.Editor.getBlock(blockId, { @@ -9,6 +9,11 @@ async function main(blockId: string) { if (block === null || block.children?.length === 0) { return; } + if (mayBeReferenced(block.children as BlockEntity[])) { + // https://github.com/hyrijk/logseq-plugin-block-to-page/issues/1 + logseq.App.showMsg("some sub block may be referenced", "error"); + return; + } const pageRegx = /^\[\[(.*)\]\]$/; const firstLine = block.content.split("\n")[0].trim(); @@ -25,7 +30,7 @@ async function main(blockId: string) { if (srcBlock) { // page.format 为空 if (srcBlock.format !== block.format) { - logseq.App.showMsg("page format not same", "warning"); + logseq.App.showMsg("page format not same", "error"); return Promise.reject("page format not same"); } @@ -44,7 +49,7 @@ async function main(blockId: string) { await logseq.Editor.exitEditingMode(); - if (block.properties.collapsed) { + if (block.properties?.collapsed) { await logseq.Editor.removeBlockProperty(block.uuid, "collapsed"); } } diff --git a/package.json b/package.json index 77ff4fb..c0801ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "logseq-plugin-block-to-page", - "version": "0.0.2", + "version": "0.0.3", "description": "turn block into page", "main": "dist/index.html", "scripts": { diff --git a/util.ts b/util.ts index 8dcf127..e260a89 100644 --- a/util.ts +++ b/util.ts @@ -1,8 +1,7 @@ import { BlockEntity } from "@logseq/libs/dist/LSPlugin.user"; -function hasProperties(block: BlockEntity) { - const properties = block.meta?.properties || {}; - return Object.keys(properties).length > 0; +function hasProperty(block: BlockEntity, propertyKey: string): boolean { + return block.properties?.[propertyKey] !== undefined; } export function toBatchBlocks(blocks: BlockEntity[]) { @@ -15,3 +14,17 @@ export function toBatchBlocks(blocks: BlockEntity[]) { properties: c.properties, })); } + +export function mayBeReferenced(blocks: BlockEntity[]) { + return blocks.some((b) => { + if (hasProperty(b, "id")) { + return true; + } else { + if (b.children) { + return mayBeReferenced(b.children as BlockEntity[]); + } else { + return false; + } + } + }); +}