|
1 | | -module.exports = require('./dist/gatsby-node') |
| 1 | +/** @type {import('gatsby').GatsbyNode['resolvableExtensions']} */ |
| 2 | +const { |
| 3 | + getPathToContentComponent, |
| 4 | +} = require('gatsby-core-utils/dist/parse-component-path') |
| 5 | +const { promises: fs } = require('fs') |
| 6 | +const path = require('path') |
| 7 | +const { cachedImport } = require('./cache-helpers') |
| 8 | +const { compile } = require('@orgajs/orgx') |
| 9 | + |
| 10 | +exports.resolvableExtensions = () => ['.org'] |
| 11 | + |
| 12 | +/** @type {import('gatsby').GatsbyNode['onCreateWebpackConfig']} */ |
| 13 | +exports.onCreateWebpackConfig = async ({ actions, loaders }, pluginOptions) => { |
| 14 | + actions.setWebpackConfig({ |
| 15 | + module: { |
| 16 | + rules: [ |
| 17 | + { |
| 18 | + test: /\.org$/, |
| 19 | + use: ({ resourceQuery, issuer }) => [ |
| 20 | + loaders.js({ |
| 21 | + isPageTemplate: /async-requires/.test(issuer), |
| 22 | + resourceQuery, |
| 23 | + }), |
| 24 | + { |
| 25 | + loader: '@orgajs/loader', |
| 26 | + options: pluginOptions, |
| 27 | + }, |
| 28 | + ], |
| 29 | + }, |
| 30 | + ], |
| 31 | + }, |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +/** @type {import('gatsby').GatsbyNode['preprocessSource']} */ |
| 36 | +exports.preprocessSource = async ({ filename }, pluginOptions) => { |
| 37 | + const orgPath = getPathToContentComponent(filename) |
| 38 | + const ext = path.extname(orgPath) |
| 39 | + |
| 40 | + if (ext !== '.org') { |
| 41 | + return undefined |
| 42 | + } |
| 43 | + const contents = await fs.readFile(orgPath) |
| 44 | + const code = await compile(contents, pluginOptions) |
| 45 | + return code.toString() |
| 46 | +} |
| 47 | + |
| 48 | +/** @type {import('gatsby').GatsbyNode['createSchemaCustomization']} */ |
| 49 | +exports.createSchemaCustomization = ({ schema, actions }) => { |
| 50 | + const { createTypes } = actions |
| 51 | + const typeDefs = [ |
| 52 | + schema.buildObjectType({ |
| 53 | + name: 'Org', |
| 54 | + fields: { |
| 55 | + id: 'ID!', |
| 56 | + children: { |
| 57 | + type: '[Org]', |
| 58 | + resolve: (source) => source.children || [], |
| 59 | + }, |
| 60 | + }, |
| 61 | + extensions: { infer: true }, |
| 62 | + interfaces: ['Node'], |
| 63 | + }), |
| 64 | + ] |
| 65 | + createTypes(typeDefs) |
| 66 | +} |
| 67 | + |
| 68 | +/** @type {import('gatsby').GatsbyNode['shouldOnCreateNode']} */ |
| 69 | +exports.shouldOnCreateNode = ({ node }) => { |
| 70 | + return node.internal.type === `File` && node.ext === '.org' |
| 71 | +} |
| 72 | + |
| 73 | +/** @type {import('gatsby').GatsbyNode['onCreateNode']} */ |
| 74 | +exports.onCreateNode = async ({ |
| 75 | + node, |
| 76 | + createNodeId, |
| 77 | + actions, |
| 78 | + loadNodeContent, |
| 79 | +}) => { |
| 80 | + const { createNode, createParentChildLink } = actions |
| 81 | + const content = await loadNodeContent(node) |
| 82 | + const { parse } = await cachedImport('@orgajs/metadata') |
| 83 | + const metadata = parse(content) |
| 84 | + const orgNode = { |
| 85 | + id: createNodeId(`${node.id} >>> Org`), |
| 86 | + children: [], |
| 87 | + parent: node.id, |
| 88 | + internal: { |
| 89 | + type: 'Org', |
| 90 | + contentDigest: node.internal.contentDigest, |
| 91 | + contentFilePath: node.absolutePath, |
| 92 | + }, |
| 93 | + body: content, |
| 94 | + metadata, |
| 95 | + } |
| 96 | + createNode(orgNode) |
| 97 | + createParentChildLink({ parent: node, child: orgNode }) |
| 98 | + |
| 99 | + return orgNode |
| 100 | +} |
| 101 | + |
| 102 | +/** @type {import('gatsby').GatsbyNode['onCreatePage']} */ |
| 103 | +exports.onCreatePage = async ({ page, actions, getNodesByType }) => { |
| 104 | + const { createPage, deletePage } = actions |
| 105 | + |
| 106 | + const mdxPath = getPathToContentComponent(page.component) |
| 107 | + const ext = path.extname(mdxPath) |
| 108 | + |
| 109 | + // Only apply on pages based on .mdx files |
| 110 | + if (ext !== '.org') { |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + const fileNode = getNodesByType(`File`).find( |
| 115 | + (node) => node.absolutePath === mdxPath |
| 116 | + ) |
| 117 | + if (!fileNode) { |
| 118 | + throw new Error(`Could not locate File node for ${mdxPath}`) |
| 119 | + } |
| 120 | + |
| 121 | + // Avoid loops |
| 122 | + if (!page.context?.metadata) { |
| 123 | + const content = await fs.readFile(mdxPath, `utf8`) |
| 124 | + const { parse } = await cachedImport('@orgajs/metadata') |
| 125 | + const metadata = parse(content) |
| 126 | + |
| 127 | + deletePage(page) |
| 128 | + createPage({ |
| 129 | + ...page, |
| 130 | + context: { |
| 131 | + ...page.context, |
| 132 | + metadata, |
| 133 | + }, |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/** @type {import('gatsby').GatsbyNode['pluginOptionsSchema']} */ |
| 139 | +exports.pluginOptionsSchema = ({ Joi }) => { |
| 140 | + return Joi.object({ |
| 141 | + jsx: Joi.boolean().description('Whether to keep JSX'), |
| 142 | + outputFormat: Joi.string() |
| 143 | + .valid(`program`, `function-body`) |
| 144 | + .description(`Whether to compile to a whole program or a function body`), |
| 145 | + reorgPlugins: Joi.array().description( |
| 146 | + `List of remark (mdast, markdown) plugins` |
| 147 | + ), |
| 148 | + }) |
| 149 | + .unknown(true) |
| 150 | + .default({}) |
| 151 | + .description('') |
| 152 | +} |
0 commit comments