UNPKG

@curvenote/cli

Version:
88 lines (87 loc) 4 kB
import { KINDS, NavListItemKindEnum } from '@curvenote/blocks'; import { Project } from '../../../models.js'; import { articleToMarkdown } from '../markdown.js'; import { notebookToIpynb } from '../notebook.js'; import { getBlockAndLatestVersion } from '../utils/getBlockAndLatestVersion.js'; import { writeBibtex } from '../utils/writeBibtex.js'; import { writeConfig } from './config.js'; import { writeTOC } from './toc.js'; async function pullAll(session, nav, opts) { const { bibtex = 'references.bib' } = opts !== null && opts !== void 0 ? opts : {}; const blocks = await Promise.all(nav.data.items.map((item) => { if (item.kind === NavListItemKindEnum.Item) return getBlockAndLatestVersion(session, item.blockId).catch(() => null); return null; })); const articles = await Promise.all(blocks.map(async (blockData) => { var _a, _b; if (!blockData) return null; const { block, version } = blockData; if (!version) { session.log.error(`Unable to download "${block.data.name}" - do you need to save the draft?`); return null; } switch (block.data.kind) { case KINDS.Article: { const filename = `${(_a = block.data.name) !== null && _a !== void 0 ? _a : block.id.block}.md`; try { const article = await articleToMarkdown(session, version.id, { ...opts, filename, writeBibtex: false, }); return article; } catch (error) { session.log.debug(`\n\n${error === null || error === void 0 ? void 0 : error.stack}\n\n`); session.log.error(`Problem downloading article: ${block.data.name}`); return null; } } case KINDS.Notebook: { const filename = `${(_b = block.data.name) !== null && _b !== void 0 ? _b : block.id.block}.ipynb`; try { const article = await notebookToIpynb(session, version.id, { ...opts, filename }); return article; } catch (error) { session.log.debug(`\n\n${error === null || error === void 0 ? void 0 : error.stack}\n\n`); session.log.error(`Problem downloading notebook: ${block.data.name}`); return null; } } default: session.log.warn(`Skipping block: "${block.data.name}" of kind "${block.data.kind}"`); return null; } })); const references = articles.reduce((obj, a) => ({ ...obj, ...a === null || a === void 0 ? void 0 : a.references }), {}); await writeBibtex(session, references, bibtex, { path: opts === null || opts === void 0 ? void 0 : opts.path, alwaysWriteFile: false }); } /** * Write jupyterbook from project * * Logs an error if no version of the nav is saved. */ export async function projectToJupyterBook(session, projectId, opts) { var _a; const [project, { version: nav }] = await Promise.all([ new Project(session, projectId).get(), getBlockAndLatestVersion(session, { project: projectId, block: 'nav' }), ]); if (!nav) { session.log.error(`Unable to load project navigation "${project.data.name}" - please save any article in your project?`); return; } if ((_a = opts.writeConfig) !== null && _a !== void 0 ? _a : true) { writeConfig(session, { path: opts.path, title: project.data.title, author: project.data.team, url: `${session.config.editorUrl}/@${project.data.team}/${project.data.name}`, }); } await writeTOC(session, nav, { path: opts.path, ci: opts.ci }); await pullAll(session, nav, { bibtex: 'references.bib', ...opts }); }