UNPKG

@jmuchovej/paperpile-notion

Version:
59 lines (58 loc) 2.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const base_1 = (0, tslib_1.__importDefault)(require("../../base")); const notion_cms_1 = require("../../notion-cms"); const author_1 = require("../../models/author"); const notion_api_1 = require("@jitl/notion-api"); const lodash_1 = (0, tslib_1.__importDefault)(require("lodash")); class AuthorsSync extends base_1.default { async run() { await this.parse(AuthorsSync); if (!this.appConfig.hasAuthorDB) { this.error("You don't have an Authors database. Exiting."); this.exit(0); // analogous to this.exit(0), but keeps WebStorm from whining } const authorCMS = (0, notion_cms_1.createCMS)(this.config, this.appConfig, this.notion, "authors"); const parent = { database_id: this.appConfig.databases.authors.databaseID, }; const toCreate = []; const existingPages = await fetchDB(this.BibTeXAuthors, authorCMS); for await (const author of this.BibTeXAuthors) { const page = existingPages[author]; if (!page) { toCreate.push({ parent, properties: (0, author_1.AuthorToNotion)((0, author_1.prepareAuthorsForNotion)(author)), }); } } await (0, notion_cms_1.batchEntries)(this, toCreate, async (entry) => { await authorCMS.config.notion.pages.create(entry); }); } } exports.default = AuthorsSync; AuthorsSync.summary = `Syncs your Authors Database with the local BibTeX file.`; AuthorsSync.description = `Authors will be created if not present (or if they don't match a manually entered alias). Otherwise, Authors will have their name stripped of whitespace and articles consolidation based on matching Aliases.`; AuthorsSync.args = base_1.default.args; AuthorsSync.flags = base_1.default.flags; AuthorsSync.examples = base_1.default.examples; const fetchDB = async (authors, cms) => { const db = {}; const chunks = lodash_1.default.chunk(authors, 50); for await (const batch of chunks) { const filter = cms.filter.or(...batch.map((name) => cms.filter.or(cms.filter.name.equals(name), cms.filter.aliases.contains(name)))); for await (const page of cms.query({ filter })) { let { frontmatter: { name, aliases } } = page; name = (0, notion_api_1.richTextAsPlainText)(name); aliases = (0, notion_api_1.richTextAsPlainText)(aliases) .split(";").map((a) => a.trim()).filter((a) => a); for (const alias of [name, ...aliases]) { db[alias] = page; } } } return db; };