@@ -848,38 +848,44 @@ export function magicJoinCommand(independentMode: boolean) {
848
848
849
849
export async function updateBlocksCommand (
850
850
callback : ( content : string , level : number , block : BlockEntity , parent ?: BlockEntity ) => string ,
851
+ recursive : boolean = false ,
851
852
) {
852
853
let [ blocks , isSelectedState ] = await getChosenBlocks ( )
853
854
if ( blocks . length === 0 )
854
855
return
855
856
856
857
blocks = unique ( blocks , ( b ) => b . uuid )
857
- blocks = await Promise . all (
858
- blocks . map ( async ( b ) => {
859
- return (
860
- await logseq . Editor . getBlock ( b . uuid , { includeChildren : true } )
861
- ) !
862
- } )
863
- )
864
- blocks = filterOutChildBlocks ( blocks )
858
+ if ( recursive ) {
859
+ blocks = await Promise . all (
860
+ blocks . map ( async ( b ) => {
861
+ return (
862
+ await logseq . Editor . getBlock ( b . uuid , { includeChildren : true } )
863
+ ) !
864
+ } )
865
+ )
866
+ blocks = filterOutChildBlocks ( blocks )
867
+ }
865
868
866
869
if ( blocks . length === 0 )
867
870
return
868
871
869
-
870
872
// it is important to check if any block in the tree has references
871
873
// (Logseq replaces references with it's text)
872
- for ( const block of blocks ) {
873
- const blocksWithReferences = await getBlocksWithReferences ( block )
874
- block . _treeHasReferences = blocksWithReferences . length !== 0
875
- }
876
-
874
+ if ( recursive )
875
+ for ( const block of blocks ) {
876
+ const blocksWithReferences = await getBlocksWithReferences ( block )
877
+ block . _treeHasReferences = blocksWithReferences . length !== 0
878
+ }
877
879
878
880
for ( const block of blocks ) {
879
- // ensure .children always is array
881
+ // ensure .children is always an array
880
882
if ( ! block . children )
881
883
block . children = [ ]
882
884
885
+ // skip child nodes in non-recursive mode
886
+ if ( ! recursive )
887
+ block . children = [ ]
888
+
883
889
const newTree = walkBlockTree ( block as WalkBlock , ( b , level , p , data ) => {
884
890
const properties = PropertiesUtils . fromCamelCaseAll ( data . node . properties )
885
891
const propertiesOrder = PropertiesUtils . getPropertyNames ( b . content )
@@ -909,11 +915,9 @@ export async function updateBlocksCommand(
909
915
}
910
916
}
911
917
912
- export function removeNewLinesCommand ( ) {
913
- return updateBlocksCommand ( ( content , level , block , parent ) => {
914
- return content
915
- . replaceAll ( / \n + / g, '\n' )
916
- . replaceAll ( / (?< = [ ^ \S \n ] ) \n / g, '' ) // remove \n when there are spaces before
917
- . replaceAll ( / (?< ! [ ^ \S ] ) \n / g, ' ' ) // replace \n to space when there are no spaces before
918
- } )
918
+ export function removeNewLines ( content , level , block , parent ) {
919
+ return content
920
+ . replaceAll ( / \n + / g, '\n' )
921
+ . replaceAll ( / (?< = [ ^ \S \n ] ) \n / g, '' ) // remove \n when there are spaces before
922
+ . replaceAll ( / (?< ! [ ^ \S ] ) \n / g, ' ' ) // replace \n to space when there are no spaces before
919
923
}
0 commit comments