@@ -848,38 +848,44 @@ export function magicJoinCommand(independentMode: boolean) {
848848
849849export async function updateBlocksCommand (
850850 callback : ( content : string , level : number , block : BlockEntity , parent ?: BlockEntity ) => string ,
851+ recursive : boolean = false ,
851852) {
852853 let [ blocks , isSelectedState ] = await getChosenBlocks ( )
853854 if ( blocks . length === 0 )
854855 return
855856
856857 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+ }
865868
866869 if ( blocks . length === 0 )
867870 return
868871
869-
870872 // it is important to check if any block in the tree has references
871873 // (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+ }
877879
878880 for ( const block of blocks ) {
879- // ensure .children always is array
881+ // ensure .children is always an array
880882 if ( ! block . children )
881883 block . children = [ ]
882884
885+ // skip child nodes in non-recursive mode
886+ if ( ! recursive )
887+ block . children = [ ]
888+
883889 const newTree = walkBlockTree ( block as WalkBlock , ( b , level , p , data ) => {
884890 const properties = PropertiesUtils . fromCamelCaseAll ( data . node . properties )
885891 const propertiesOrder = PropertiesUtils . getPropertyNames ( b . content )
@@ -909,11 +915,9 @@ export async function updateBlocksCommand(
909915 }
910916}
911917
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
919923}
0 commit comments