UNPKG

gatsby-source-roamresearch

Version:

Source plugin for pulling data into Gatsby from Roam Research

84 lines (83 loc) 3.15 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.sourceNodes = void 0; const fetch_roamresearch_1 = __importDefault(require("fetch-roamresearch")); const make_markdown_1 = require("./make-markdown"); const sourceNodes = async ({ actions, createNodeId, createContentDigest, reporter, getNode, }, options) => { const { url, email, password, headless } = options; const { createNode, createNodeField, createParentChildLink } = actions; const pages = await fetch_roamresearch_1.default(url, { email, password, }, { reporter, puppeteer: { headless } }); if (!pages) { reporter.error("Could not download the Roam Research data"); return; } const dbContent = JSON.stringify(pages); const dbMeta = { id: createNodeId(`roam-db-${url}`), parent: undefined, sourceUrl: url, children: [], internal: { type: `RoamDatabase`, mediaType: `application/json`, content: dbContent, contentDigest: createContentDigest(pages), }, }; createNode(dbMeta); const dbNode = getNode(dbMeta.id); const makeBlockNode = (block, parent, page) => { var _a; const content = make_markdown_1.makeMarkdown(block); const nodeMeta = { id: createNodeId(`roam-block-${block.uid || Math.random()}`), parent: parent.id, sourceUrl: url, children: [], internal: { type: `RoamBlock`, mediaType: `text/markdown`, content, contentDigest: createContentDigest(content), }, }; const nodeData = Object.assign({}, block, nodeMeta); createNode(nodeData); let node = getNode(nodeData.id); createParentChildLink({ parent, child: node }); createNodeField({ node, name: "parentPage___NODE", value: page.id, }); (_a = block.children) === null || _a === void 0 ? void 0 : _a.forEach((grandChild) => makeBlockNode(grandChild, node, page)); }; pages.forEach((page) => { var _a; const content = make_markdown_1.makeMarkdown(page); const nodeMeta = { id: createNodeId(`roam-page-${page.title}`), parent: dbMeta.id, sourceUrl: url, children: [], internal: { type: `RoamPage`, mediaType: "text/markdown", content, contentDigest: createContentDigest(content), }, }; const nodeData = Object.assign({}, page, nodeMeta); createNode(nodeData); let node = getNode(nodeData.id); createParentChildLink({ parent: dbNode, child: node }); (_a = page.children) === null || _a === void 0 ? void 0 : _a.forEach((child) => makeBlockNode(child, node, node)); }); }; exports.sourceNodes = sourceNodes;