import "@logseq/libs" ; import { BlockEntity , BlockIdentity } from "@logseq/libs/dist/LSPlugin.user" ; import { toBatchBlocks , mayBeReferenced } from "./util" ; async function main ( blockId ) { const block = await logseq . Editor . getBlock ( blockId , { includeChildren : true } ) ; if ( block === null || block . children ?. length === 0 ) { return ; } if ( mayBeReferenced ( block . children as BlockEntity [ ] ) ) { logseq . App . showMsg ( "some sub block may be referenced" , "error" ) ; return ; } const pageRegx = /^\[\[(.*)\]\]$/ ; const firstLine = block . content . split ( "\n" ) [ 0 ] . trim ( ) ; const pageName = firstLine . replace ( pageRegx , "$1" ) ; let newBlockContent = "" ; if ( ! pageRegx . test ( firstLine ) ) { newBlockContent = block . content . replace ( firstLine , `[[${ firstLine }]]` ) ; } await createPageIfNotExist ( pageName ) ; const srcBlock = await getLastBlock ( pageName ) ; if ( srcBlock ) { if ( srcBlock . format !== block . format ) { logseq . App . showMsg ( "page format not same" , "error" ) ; return Promise . reject ( "page format not same" ) ; } await removeBlocks ( block . children as BlockEntity [ ] ) ; if ( newBlockContent ) { await logseq . Editor . updateBlock ( block . uuid , newBlockContent ) ; } await insertBatchBlock ( srcBlock . uuid , block . children as BlockEntity [ ] ) ; if ( srcBlock . content === "" ) { await logseq . Editor . removeBlock ( srcBlock . uuid ) ; } await logseq . Editor . exitEditingMode ( ) ; if ( block . properties ?. collapsed ) { await logseq . Editor . removeBlockProperty ( block . uuid , "collapsed" ) ; } } } logseq . ready ( ( ) => { logseq . Editor . registerSlashCommand ( "Turn Into Page" , async ( e ) => { main ( e . uuid ) ; } ) ; logseq . Editor . registerBlockContextMenuItem ( "Turn into page" , async ( e ) => { main ( e . uuid ) ; } ) ; } ) . catch ( console . error ) ; async function insertBatchBlock ( srcBlock , blocks ) { const batchBlocks = toBatchBlocks ( blocks ) ; debug ( "insertBatchBlock" , srcBlock , batchBlocks ) ; await logseq . Editor . insertBatchBlock ( srcBlock , batchBlocks , { sibling : true , before : false } ) ; } async function createPageIfNotExist ( pageName ) { let page = await logseq . Editor . getPage ( pageName ) ; if ( ! page ) { await logseq . Editor . createPage ( pageName , { } , { createFirstBlock : true , redirect : false } ) ; } else { debug ( "page already exist" ) ; const lastBlock = await getLastBlock ( pageName ) ; if ( lastBlock === null ) { await logseq . Editor . deletePage ( pageName ) ; await logseq . Editor . createPage ( pageName , { } , { createFirstBlock : true , redirect : false } ) ; } } } async function removeBlocks ( blocks ) { for ( let i = 0 ; i < blocks . length ; i ++ ) { const child = blocks [ i ] ; await logseq . Editor . removeBlock ( child . uuid ) ; } } async function getLastBlock ( pageName ) { const blocks = await logseq . Editor . getPageBlocksTree ( pageName ) ; if ( blocks . length === 0 ) { return null ; } return blocks [ blocks . length - 1 ] ; } function debug ( ... args ) { console . debug ( "block-to-page" , ... args ) ; }