Skip to content

Commit fc6ab08

Browse files
committed
new gatsby plugin
1 parent f254279 commit fc6ab08

21 files changed

+180
-441
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = {
33
env: {
44
node: true,
55
browser: true,
6-
jest: true,
6+
es6: true,
77
},
88
extends: [
99
'eslint:recommended',
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/** @type {Map<string, unkown>} */
2+
const importCache = new Map()
3+
4+
/**
5+
* @template Type
6+
* @param {string} packageName
7+
* @returns {Promise<Type>}
8+
*/
9+
async function cachedImport(packageName) {
10+
if (importCache.has(packageName)) {
11+
return importCache.get(packageName)
12+
}
13+
const importedPackage = await import(packageName)
14+
return importedPackage
15+
}
16+
17+
exports.cachedImport = cachedImport

packages/gatsby-plugin-orga/gatsby-browser.js

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,152 @@
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+
}

packages/gatsby-plugin-orga/gatsby-ssr.js

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
// nothing to see here
1+
// noop

packages/gatsby-plugin-orga/loaders/orga-components.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/gatsby-plugin-orga/package.json

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,25 @@
1717
"*.js",
1818
"*.jsx"
1919
],
20-
"scripts": {
21-
"build": "pnpm clean && pnpm compile",
22-
"clean": "del ./dist tsconfig.tsbuildinfo",
23-
"compile": "tsc -b"
24-
},
20+
"scripts": {},
2521
"peerDependencies": {
26-
"gatsby": "^3.0.0",
27-
"gatsby-plugin-sharp": "*",
28-
"react": "^16.9.0 || ^17.0.0",
29-
"react-dom": "^16.9.0 || ^17.0.0"
22+
"gatsby": "^5.0.0-next",
23+
"gatsby-source-filesystem": "^5.0.0-next",
24+
"react": "^18.0.0",
25+
"react-dom": "^18.0.0"
3026
},
3127
"dependencies": {
3228
"@orgajs/estree-jsx": "3.0.2",
33-
"@orgajs/loader": "^3.1.10",
34-
"@orgajs/metadata": "1.0.2",
35-
"@orgajs/orgx": "1.0.8",
29+
"@orgajs/loader": "workspace:^",
30+
"@orgajs/metadata": "workspace:*",
31+
"@orgajs/orgx": "workspace:*",
3632
"@orgajs/react": "^3.0.1",
3733
"@orgajs/rehype-latex": "1.0.2",
3834
"astring": "^1.7.5",
3935
"dataloader": "^2.0.0",
4036
"estree-walker": "3.0.3",
4137
"fs-extra": "^10.0.0",
42-
"gatsby-core-utils": "^2.14.0",
38+
"gatsby-core-utils": "^2.15.0",
4339
"gatsby-plugin-page-creator": "^3.14.0",
4440
"loader-utils": "^2.0.0",
4541
"lodash": "^4.17.21"

packages/gatsby-plugin-orga/src/components/link.tsx

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/gatsby-plugin-orga/src/gatsby-node.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)