-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from briansunter/bs/gpt-page
Bs/gpt page
- Loading branch information
Showing
6 changed files
with
230 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { BlockEntity, BlockUUIDTuple } from "@logseq/libs/dist/LSPlugin.user"; | ||
|
||
function isBlockEntity(b: BlockEntity | BlockUUIDTuple): b is BlockEntity { | ||
return (b as BlockEntity).uuid !== undefined; | ||
} | ||
|
||
async function getTreeContent(b: BlockEntity) { | ||
let content = ""; | ||
const trimmedBlockContent = b.content.trim(); | ||
if (trimmedBlockContent.length > 0) { | ||
content += trimmedBlockContent; | ||
} | ||
|
||
if (!b.children) { | ||
return content; | ||
} | ||
|
||
for (const child of b.children) { | ||
if (isBlockEntity(child)) { | ||
content += await getTreeContent(child); | ||
} else { | ||
const childBlock = await logseq.Editor.getBlock(child[1], { | ||
includeChildren: true, | ||
}); | ||
if (childBlock) { | ||
content += await getTreeContent(childBlock); | ||
} | ||
} | ||
} | ||
return content; | ||
} | ||
|
||
async function getPageContentFromBlock(b: BlockEntity): Promise<string> { | ||
let blockContents = []; | ||
|
||
const currentBlock = await logseq.Editor.getBlock(b); | ||
if (!currentBlock) { | ||
throw new Error("Block not found"); | ||
} | ||
|
||
const page = await logseq.Editor.getPage(currentBlock.page.id); | ||
if (!page) { | ||
throw new Error("Page not found"); | ||
} | ||
|
||
const pageBlocks = await logseq.Editor.getPageBlocksTree(page.name); | ||
for (const pageBlock of pageBlocks) { | ||
const blockContent = await getTreeContent(pageBlock); | ||
if (blockContent.length > 0) { | ||
blockContents.push(blockContent); | ||
} | ||
} | ||
return blockContents.join(" "); | ||
} | ||
|
||
export { getPageContentFromBlock }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.