From 161f63ee62a9802d84d15199937ca908a2e3d0c4 Mon Sep 17 00:00:00 2001 From: Sergey Kolesnik Date: Sun, 19 Mar 2023 18:59:05 +0300 Subject: [PATCH] feat(context): `level` value & access to current `page` --- src/utils/logseq.ts | 17 +++++++++-------- src/utils/parsing.ts | 5 ++++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/utils/logseq.ts b/src/utils/logseq.ts index 252a832..387032c 100644 --- a/src/utils/logseq.ts +++ b/src/utils/logseq.ts @@ -7,11 +7,14 @@ import { isInteger, isUUID } from './parsing' export type IBlockNode = Required> export function walkBlockTree( root: IBatchBlock, - callback: ((b: IBatchBlock) => string | void), + callback: ((b: IBatchBlock, lvl: number) => string | void), + level: number = 0, ): IBlockNode { return { - content: callback(root) ?? '', - children: (root.children || []).map(b => walkBlockTree(b as IBlockNode, callback)), + content: callback(root, level) ?? '', + children: (root.children || []).map( + b => walkBlockTree(b as IBlockNode, callback, level + 1) + ), } } @@ -127,11 +130,9 @@ export async function getPage(ref: LogseqReference): Promise } export async function getBlock( - ref: LogseqReference, - { - byProperty = '', - includeChildren = false, - }: { byProperty?: string, includeChildren?: boolean } + ref: LogseqReference, { + byProperty = '', + includeChildren = false }: { byProperty?: string, includeChildren?: boolean } ): Promise<[BlockEntity | null, LogseqReferenceAccessType]> { if (['page', 'tag'].includes(ref.type)) return [ null, 'page'] diff --git a/src/utils/parsing.ts b/src/utils/parsing.ts index ebfc813..69cdc86 100644 --- a/src/utils/parsing.ts +++ b/src/utils/parsing.ts @@ -42,12 +42,15 @@ const quotesValues = [ '\u301E'.repeat(2), // DOUBLE PRIME QUOTATION MARK ] - export function isUUID(str: string) { const regex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/ return !!str.match(regex) } +export function isObject(item: any): boolean { + return (item && typeof item === 'object' && !Array.isArray(item)) + } + export function isBoolean(obj: any): boolean { // source: https://stackoverflow.com/a/28814865 if (typeof obj === typeof true)