Skip to content

Instantly share code, notes, and snippets.

@davidmatas
Created June 27, 2024 15:50
Show Gist options
  • Save davidmatas/5f5021dcb1891557cbaa8b93f793b352 to your computer and use it in GitHub Desktop.
Save davidmatas/5f5021dcb1891557cbaa8b93f793b352 to your computer and use it in GitHub Desktop.
Postmortem Document
const { Client } = require('@notionhq/client');
// Configura tu cliente de Notion con el token de integración
const notion = new Client({ auth: process.env.NOTION_API_KEY });
// ID de la página fuente de Notion de la que se copiarán los children
const sourcePageId = process.env.NOTION_SOURCE_PAGE_ID;
// ID de la base de datos de Notion donde se creará la nueva página
const databaseId = process.env.NOTION_DATABASE_ID;
// Función para obtener los children de la página fuente
async function getSourcePageChildren(pageId) {
const blocks = [];
let cursor;
while (true) {
const response = await notion.blocks.children.list({
block_id: pageId,
start_cursor: cursor,
});
blocks.push(...response.results);
if (!response.has_more) break;
cursor = response.next_cursor;
}
return blocks.filter(block => block.type !== 'child_database' && block.type !== 'unsupported')
}
// Función para crear una página en Notion
async function createNotionPage(children) {
try {
const response = await notion.pages.create({
parent: { database_id: databaseId },
properties: {
title: {
title: [
{
text: {
content: 'Nueva página copiada',
},
},
],
},
},
children: children.map(child => {
delete child.id; // Eliminamos el id ya que Notion generará uno nuevo
return child;
}),
});
console.log('Página creada en Notion:', response);
} catch (error) {
console.error('Error creando la página en Notion:', error);
}
}
// Función principal
(async () => {
const children = await getSourcePageChildren(sourcePageId);
console.log(children[17])
if (children) {
await createNotionPage(children);
} else {
console.error('No se pudo obtener los children de la página fuente.');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment