Skip to content

Commit 6288180

Browse files
committed
fix @orgajs/astro
1 parent f3257ff commit 6288180

File tree

11 files changed

+237
-25
lines changed

11 files changed

+237
-25
lines changed

packages/astro/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.d.ts

packages/astro/index.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,30 @@
66
*/
77
import orga from '@orgajs/rollup'
88
import { parse as parseMetadata } from '@orgajs/metadata'
9+
import { addAstroFragment } from './lib/plugin/recma-add-astro-fragment.js'
910

1011
/**
1112
* @returns {import('astro').AstroIntegration}
1213
*/
13-
export default function withOrga() {
14+
export default function org() {
1415
return {
1516
name: '@orgajs/astro',
1617
hooks: {
17-
'astro:config:setup': async (params) => {
18-
const { addPageExtension, addContentEntryType, updateConfig } =
19-
/** @type {SetupHookParams} */ params
18+
// @ts-ignore - addPageExtension, addContentEntryType are internal APIs
19+
'astro:config:setup': async (/** @type {SetupHookParams} */ params) => {
20+
const { addPageExtension, addContentEntryType, updateConfig } = params
2021
addPageExtension('.org')
2122

2223
addContentEntryType({
2324
extensions: ['.org'],
2425
async getEntryInfo({ contents }) {
2526
const data = parseMetadata(contents)
27+
2628
return {
2729
data,
28-
contents,
30+
body: contents,
31+
rawData: JSON.stringify(data),
32+
slug: Array.isArray(data.slug) ? data.slug[0] : data.slug,
2933
}
3034
},
3135
})
@@ -35,7 +39,24 @@ export default function withOrga() {
3539
plugins: [
3640
{
3741
enforce: 'pre',
38-
...orga(),
42+
...orga({
43+
jsxImportSource: 'astro',
44+
recmaPlugins: [addAstroFragment],
45+
elementAttributeNameCase: 'html',
46+
development: false,
47+
}),
48+
},
49+
{
50+
name: '@orgajs/org-postprocess',
51+
/**
52+
* @param {string} code
53+
* @param {string} id
54+
*/
55+
transform(code, id) {
56+
if (!id.endsWith('.org')) return
57+
// console.log(code)
58+
return { code, map: null }
59+
},
3960
},
4061
],
4162
},
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/**
2+
* @typedef {import('estree-jsx').Node} Node
3+
* @typedef {import('estree-jsx').Program} Program
4+
* @typedef {import('estree-jsx').Expression} Expression
5+
* @typedef {import('estree-jsx').ModuleDeclaration} ModuleDeclaration
6+
* @typedef {any} Options
7+
*/
8+
9+
import { visit, EXIT, SKIP } from 'estree-util-visit'
10+
11+
/**
12+
* @param {Node} node
13+
* @returns {node is Program}
14+
*/
15+
function isProgram(node) {
16+
return node.type === 'Program'
17+
}
18+
19+
/**
20+
* A plugin to add Astro Fragment to the final component
21+
*
22+
* @type {import('unified').Plugin<[], Program>}
23+
*/
24+
export function addAstroFragment() {
25+
return (tree) => {
26+
// console.log(inspect(tree, false, null, true))
27+
tree.body.unshift({
28+
type: 'ImportDeclaration',
29+
specifiers: [
30+
{
31+
type: 'ImportDefaultSpecifier',
32+
local: {
33+
type: 'Identifier',
34+
name: 'Fragment',
35+
},
36+
},
37+
],
38+
source: {
39+
type: 'Literal',
40+
value: 'astro/jsx-runtime',
41+
},
42+
})
43+
44+
/** @type {import('estree-jsx').ExportNamedDeclaration} */
45+
const code = {
46+
type: 'ExportNamedDeclaration',
47+
declaration: {
48+
type: 'VariableDeclaration',
49+
declarations: [
50+
{
51+
type: 'VariableDeclarator',
52+
id: {
53+
type: 'Identifier',
54+
name: 'Content',
55+
},
56+
init: {
57+
type: 'ArrowFunctionExpression',
58+
expression: true,
59+
generator: false,
60+
async: false,
61+
params: [
62+
{
63+
type: 'AssignmentPattern',
64+
left: {
65+
type: 'Identifier',
66+
name: 'props',
67+
},
68+
right: {
69+
type: 'ObjectExpression',
70+
properties: [],
71+
},
72+
},
73+
],
74+
body: {
75+
type: 'CallExpression',
76+
callee: {
77+
type: 'Identifier',
78+
name: 'OrgContent',
79+
},
80+
arguments: [
81+
{
82+
type: 'ObjectExpression',
83+
properties: [
84+
{
85+
type: 'SpreadElement',
86+
argument: {
87+
type: 'Identifier',
88+
name: 'props',
89+
},
90+
},
91+
{
92+
type: 'Property',
93+
method: false,
94+
shorthand: false,
95+
computed: false,
96+
key: {
97+
type: 'Identifier',
98+
name: 'components',
99+
},
100+
value: {
101+
type: 'ObjectExpression',
102+
properties: [
103+
{
104+
type: 'Property',
105+
method: false,
106+
shorthand: true,
107+
computed: false,
108+
key: {
109+
type: 'Identifier',
110+
name: 'Fragment',
111+
},
112+
kind: 'init',
113+
value: {
114+
type: 'Identifier',
115+
name: 'Fragment',
116+
},
117+
},
118+
{
119+
type: 'SpreadElement',
120+
argument: {
121+
type: 'MemberExpression',
122+
object: {
123+
type: 'Identifier',
124+
name: 'props',
125+
},
126+
property: {
127+
type: 'Identifier',
128+
name: 'components',
129+
},
130+
computed: false,
131+
optional: false,
132+
},
133+
},
134+
],
135+
},
136+
kind: 'init',
137+
},
138+
],
139+
},
140+
],
141+
optional: false,
142+
},
143+
},
144+
},
145+
],
146+
kind: 'const',
147+
},
148+
specifiers: [],
149+
}
150+
151+
visit(tree, (node, _key, index, ancestors) => {
152+
if (ancestors.length === 0 || index === null) return
153+
const parent = ancestors[ancestors.length - 1]
154+
if (!isProgram(parent)) return SKIP
155+
if (
156+
node.type === 'ExportDefaultDeclaration' &&
157+
node.declaration.type === 'Identifier'
158+
) {
159+
node.declaration.name = 'Content'
160+
parent.body.splice(index, 0, code)
161+
return EXIT
162+
}
163+
})
164+
}
165+
}

packages/astro/package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
"exports": {
88
".": "./index.js"
99
},
10-
"scripts": {},
10+
"scripts": {
11+
"clean": "tsc --build --clean",
12+
"build": "tsc --build"
13+
},
1114
"keywords": [
1215
"astro-integration"
1316
],
@@ -20,9 +23,14 @@
2023
},
2124
"dependencies": {
2225
"@orgajs/metadata": "workspace:1.0.2",
23-
"@orgajs/rollup": "workspace:^"
26+
"@orgajs/rollup": "workspace:^",
27+
"estree-util-visit": "^1.2.1",
28+
"source-map": "^0.7.4"
2429
},
2530
"devDependencies": {
26-
"astro": "^2.5.0"
31+
"@types/estree": "^1.0.1",
32+
"@types/estree-jsx": "^1.0.0",
33+
"astro": "^2.5.0",
34+
"unified": "^10.1.2"
2735
}
2836
}

packages/astro/tsconfig.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": "../../tsconfig.types.json",
3+
"include": ["lib/**/*.js", "index.js"],
4+
"exclude": ["node_modules/"]
5+
}

packages/orgx/lib/core.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* Whether to compile to a whole program or a function body..
1818
* @property {PluggableList | null | undefined} [recmaPlugins]
1919
* List of recma (esast, JavaScript) plugins.
20-
* @property {PluggableList | null | undefined} [remarkPlugins]
20+
* @property {PluggableList | null | undefined} [reorgPlugins]
2121
* List of remark (mdast, markdown) plugins.
2222
* @property {PluggableList | null | undefined} [rehypePlugins]
2323
* List of rehype (hast, HTML) plugins.
@@ -62,7 +62,7 @@ export function createProcessor(options) {
6262
providerImportSource,
6363
recmaPlugins,
6464
rehypePlugins,
65-
remarkPlugins,
65+
reorgPlugins,
6666
reorgRehypeOptions,
6767
elementAttributeNameCase,
6868
stylePropertyNameCase,
@@ -76,8 +76,7 @@ export function createProcessor(options) {
7676

7777
const pipeline = unified()
7878
.use(reorgParse)
79-
// .use(remarkMarkAndUnravel)
80-
.use(remarkPlugins || [])
79+
.use(reorgPlugins || [])
8180
.use(reorgRehype, {
8281
...reorgRehypeOptions,
8382
allowDangerousHtml: true,

packages/orgx/lib/plugin/recma-jsx-build.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
* @typedef {BuildJsxOptions & ExtraOptions} RecmaJsxBuildOptions
1414
*/
1515

16-
import {buildJsx} from 'estree-util-build-jsx'
17-
import {specifiersToDeclarations} from '../util/estree-util-specifiers-to-declarations.js'
18-
import {toIdOrMemberExpression} from '../util/estree-util-to-id-or-member-expression.js'
16+
import { buildJsx } from 'estree-util-build-jsx'
17+
import { specifiersToDeclarations } from '../util/estree-util-specifiers-to-declarations.js'
18+
import { toIdOrMemberExpression } from '../util/estree-util-to-id-or-member-expression.js'
1919

2020
/**
2121
* A plugin to build JSX into function calls.
@@ -24,12 +24,11 @@ import {toIdOrMemberExpression} from '../util/estree-util-to-id-or-member-expres
2424
* @type {import('unified').Plugin<[RecmaJsxBuildOptions | null | undefined] | [], Program>}
2525
*/
2626
export function recmaJsxBuild(options) {
27-
// Always given inside `@mdx-js/mdx`
28-
/* c8 ignore next */
29-
const {development, outputFormat} = options || {}
27+
// Always given inside `@orgajs/orgx`
28+
const { development, outputFormat } = options || {}
3029

3130
return (tree, file) => {
32-
buildJsx(tree, {development, filePath: file.history[0]})
31+
buildJsx(tree, { development, filePath: file.history[0] })
3332

3433
// When compiling to a function body, replace the import that was just
3534
// generated, and get `jsx`, `jsxs`, and `Fragment` from `arguments[0]`
@@ -47,7 +46,7 @@ export function recmaJsxBuild(options) {
4746
declarations: specifiersToDeclarations(
4847
tree.body[0].specifiers,
4948
toIdOrMemberExpression(['arguments', 0])
50-
)
49+
),
5150
}
5251
}
5352
}

packages/orgx/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"extends": "../../tsconfig.types.json",
33
"include": ["lib/**/*.js", "index.js"],
4-
"exclude": ["node_modules/", "src"]
4+
"exclude": ["node_modules/"]
55
}

packages/react/lib/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Get current components from the MDX Context.
2+
* Get current components from the org context.
33
*
44
* @param {Components | MergeComponents | null | undefined} [components]
55
* Additional components to use or a function that takes the current
@@ -9,7 +9,7 @@
99
*/
1010
export function useOrgComponents(components?: Components | MergeComponents | null | undefined): Components;
1111
/**
12-
* Provider for MDX context
12+
* Provider for org context
1313
*
1414
* @param {Props} props
1515
* @returns {JSX.Element}

pnpm-lock.yaml

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)