Skip to content

Commit

Permalink
feat: add reference check #1
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrijk committed Oct 10, 2021
1 parent d7dbed1 commit 39d3a8c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
11 changes: 8 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -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, {
Expand All @@ -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();
Expand All @@ -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");
}

Expand All @@ -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");
}
}
Expand Down
2 changes: 1 addition & 1 deletion 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.2",
"version": "0.0.3",
"description": "turn block into page",
"main": "dist/index.html",
"scripts": {
Expand Down
19 changes: 16 additions & 3 deletions util.ts
Original file line number Diff line number Diff line change
@@ -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[]) {
Expand All @@ -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;
}
}
});
}

0 comments on commit 39d3a8c

Please sign in to comment.