Last active
July 18, 2025 10:41
-
-
Save joakin/ff7d5cd9cfccd9dfa1e9c226e96d0b5b to your computer and use it in GitHub Desktop.
Add a block to a page by title in Routine via the console
This file contains hidden or 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
/** | |
* Function to search for a page by title and append a block to it | |
* | |
* Usage examples: | |
* | |
* // Add a JavaScript code block: | |
* await addBlockToPage('My Dev Notes', { | |
* type: 'code', | |
* language: 'javascript', | |
* content: 'const result = await fetchData();\nconsole.log(result);' | |
* }); | |
* | |
* // Add a Python code block: | |
* await addBlockToPage('My Dev Notes', { | |
* type: 'code', | |
* language: 'python', | |
* content: 'def calculate_sum(a, b):\n return a + b\n\nresult = calculate_sum(5, 3)' | |
* }); | |
* | |
* // Add a SQL code block: | |
* await addBlockToPage('Database Notes', { | |
* type: 'code', | |
* language: 'sql', | |
* content: 'SELECT * FROM users\nWHERE created_at > CURRENT_DATE - INTERVAL 7 DAY;' | |
* }); | |
* | |
* // Add a simple paragraph: | |
* await addBlockToPage('My Page Title', 'This is a new paragraph'); | |
*/ | |
async function addBlockToPage(pageTitle, blockContent) { | |
try { | |
// Step 1: Search for the page by title | |
const searchResults = await controller.search(pageTitle, 10); | |
// Find the page in search results | |
const pageResult = searchResults.find( | |
(result) => | |
result.id.id.kind === "page" && | |
result.title.toLowerCase().includes(pageTitle.toLowerCase()) | |
); | |
if (!pageResult) { | |
console.error(`Page with title "${pageTitle}" not found`); | |
return null; | |
} | |
const pageId = pageResult.id.id.id; | |
console.log(`Found page: ${pageResult.title} (ID: ${pageId})`); | |
// Step 2: Generate a block ID | |
const blockId = await controller.document.generateBlockId(); | |
// Step 3: Create the block object | |
const block = { | |
id: blockId, | |
type: blockContent.type || "paragraph", | |
...(typeof blockContent === "string" | |
? { content: blockContent } | |
: blockContent), | |
}; | |
// Step 4: Create the document reference | |
const document = { | |
id: { | |
kind: "page", | |
id: pageId, | |
}, | |
}; | |
// Step 5: Append the block to the page | |
const result = await controller.document.append(document, block); | |
if (result) { | |
console.log(`Successfully added block to page "${pageResult.title}"`); | |
console.log("Block:", block); | |
return result; | |
} else { | |
console.error("Failed to append block to page"); | |
return null; | |
} | |
} catch (error) { | |
console.error("Error adding block to page:", error); | |
return null; | |
} | |
} |
Author
joakin
commented
Jul 18, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment