Skip to content

Commit 897832e

Browse files
committed
@orgajs/orgx to js
1 parent 72a557d commit 897832e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1173
-871
lines changed

packages/orgx/.gitignore

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

packages/orgx/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @typedef {import('./lib/core.js').ProcessorOptions} ProcessorOptions
3+
* @typedef {import('./lib/compile.js').CompileOptions} CompileOptions
4+
* @typedef {import('./lib/evaluate.js').EvaluateOptions} EvaluateOptions
5+
* @typedef {import('./lib/types.js').OrgComponents} OrgComponents
6+
* @typedef {import('./lib/types.js').OrgContent} OrgContent
7+
* @typedef {import('./lib/types.js').OrgModule} OrgModule
8+
* @typedef {import('./lib/types.js').OrgProps} OrgProps
9+
*/
10+
11+
export { createProcessor } from './lib/core.js'
12+
export { compile, compileSync } from './lib/compile.js'
13+
export { evaluate, evaluateSync } from './lib/evaluate.js'
14+
export { run, runSync } from './lib/run.js'

packages/orgx/lib/compile.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @typedef {import('vfile').VFile} VFile
3+
* @typedef {import('vfile').VFileCompatible} VFileCompatible
4+
* @typedef {import('./core.js').PluginOptions} PluginOptions
5+
* @typedef {import('./core.js').BaseProcessorOptions} BaseProcessorOptions
6+
*/
7+
8+
/**
9+
* @typedef {Omit<BaseProcessorOptions, 'format'>} CoreProcessorOptions
10+
* Core configuration.
11+
*
12+
* @typedef ExtraOptions
13+
* Extra configuration.
14+
* @property {'detect' | 'mdx' | 'md' | null | undefined} [format='detect']
15+
* Format of `file`.
16+
*
17+
* @typedef {CoreProcessorOptions & PluginOptions & ExtraOptions} CompileOptions
18+
* Configuration.
19+
*/
20+
21+
import {createProcessor} from './core.js'
22+
import {resolveFileAndOptions} from './util/resolve-file-and-options.js'
23+
24+
/**
25+
* Compile MDX to JS.
26+
*
27+
* @param {VFileCompatible} vfileCompatible
28+
* MDX document to parse (`string`, `Buffer`, `vfile`, anything that can be
29+
* given to `vfile`).
30+
* @param {CompileOptions | null | undefined} [compileOptions]
31+
* Compile configuration.
32+
* @return {Promise<VFile>}
33+
* File.
34+
*/
35+
export function compile(vfileCompatible, compileOptions) {
36+
const {file, options} = resolveFileAndOptions(vfileCompatible, compileOptions)
37+
return createProcessor(options).process(file)
38+
}
39+
40+
/**
41+
* Synchronously compile MDX to JS.
42+
*
43+
* @param {VFileCompatible} vfileCompatible
44+
* MDX document to parse (`string`, `Buffer`, `vfile`, anything that can be
45+
* given to `vfile`).
46+
* @param {CompileOptions | null | undefined} [compileOptions]
47+
* Compile configuration.
48+
* @return {VFile}
49+
* File.
50+
*/
51+
export function compileSync(vfileCompatible, compileOptions) {
52+
const {file, options} = resolveFileAndOptions(vfileCompatible, compileOptions)
53+
return createProcessor(options).processSync(file)
54+
}

packages/orgx/lib/core.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @typedef {import('unified').PluggableList} PluggableList
3+
* @typedef {import('unified').Processor} Processor
4+
* @typedef {import('./plugin/rehype-recma.js').Options} RehypeRecmaOptions
5+
* @typedef {import('./plugin/recma-document.js').RecmaDocumentOptions} RecmaDocumentOptions
6+
* @typedef {import('./plugin/recma-stringify.js').RecmaStringifyOptions} RecmaStringifyOptions
7+
* @typedef {import('./plugin/recma-jsx-rewrite.js').RecmaJsxRewriteOptions} RecmaJsxRewriteOptions
8+
*/
9+
10+
/**
11+
* @typedef BaseProcessorOptions
12+
* Base configuration.
13+
* @property {boolean | null | undefined} [jsx=false]
14+
* Whether to keep JSX.
15+
* Format of the files to be processed.
16+
* @property {'function-body' | 'program'} [outputFormat='program']
17+
* Whether to compile to a whole program or a function body..
18+
* @property {PluggableList | null | undefined} [recmaPlugins]
19+
* List of recma (esast, JavaScript) plugins.
20+
* @property {PluggableList | null | undefined} [remarkPlugins]
21+
* List of remark (mdast, markdown) plugins.
22+
* @property {PluggableList | null | undefined} [rehypePlugins]
23+
* List of rehype (hast, HTML) plugins.
24+
* @property {import('@orgajs/reorg-rehype').Options | null | undefined} [reorgRehypeOptions]
25+
* Options to pass through to `reorg-rehype`.
26+
*
27+
* @typedef {Omit<RehypeRecmaOptions & RecmaDocumentOptions & RecmaStringifyOptions & RecmaJsxRewriteOptions, 'outputFormat'>} PluginOptions
28+
* Configuration for internal plugins.
29+
*
30+
* @typedef {BaseProcessorOptions & PluginOptions} ProcessorOptions
31+
* Configuration for processor.
32+
*/
33+
34+
import { unified } from 'unified'
35+
import reorgParse from '@orgajs/reorg-parse'
36+
import reorgRehype from '@orgajs/reorg-rehype'
37+
import { recmaJsxBuild } from './plugin/recma-jsx-build.js'
38+
import { recmaDocument } from './plugin/recma-document.js'
39+
import { recmaJsxRewrite } from './plugin/recma-jsx-rewrite.js'
40+
import { recmaStringify } from './plugin/recma-stringify.js'
41+
import { rehypeRecma } from './plugin/rehype-recma.js'
42+
// import { remarkMarkAndUnravel } from './plugin/remark-mark-and-unravel.js'
43+
import { development as defaultDevelopment } from './condition.js'
44+
45+
/**
46+
* Pipeline to:
47+
*
48+
* 1. Parse org-mode
49+
* 2. Transform through reorg (oast), rehype (hast), and recma (esast)
50+
* 3. Serialize as JavaScript
51+
*
52+
* @param {ProcessorOptions | null | undefined} [options]
53+
* Configuration.
54+
* @return {Processor}
55+
* Processor.
56+
*/
57+
export function createProcessor(options) {
58+
const {
59+
development,
60+
jsx,
61+
outputFormat,
62+
providerImportSource,
63+
recmaPlugins,
64+
rehypePlugins,
65+
remarkPlugins,
66+
reorgRehypeOptions,
67+
elementAttributeNameCase,
68+
stylePropertyNameCase,
69+
SourceMapGenerator,
70+
...rest
71+
} = options || {}
72+
const dev =
73+
development === null || development === undefined
74+
? defaultDevelopment
75+
: development
76+
77+
const pipeline = unified()
78+
.use(reorgParse)
79+
// .use(remarkMarkAndUnravel)
80+
.use(remarkPlugins || [])
81+
.use(reorgRehype, {
82+
...reorgRehypeOptions,
83+
allowDangerousHtml: true,
84+
})
85+
.use(rehypePlugins || [])
86+
.use(rehypeRecma, { elementAttributeNameCase, stylePropertyNameCase })
87+
.use(recmaDocument, { ...rest, outputFormat })
88+
.use(recmaJsxRewrite, {
89+
development: dev,
90+
providerImportSource,
91+
outputFormat,
92+
})
93+
94+
if (!jsx) {
95+
pipeline.use(recmaJsxBuild, { development: dev, outputFormat })
96+
}
97+
98+
pipeline.use(recmaStringify, { SourceMapGenerator }).use(recmaPlugins || [])
99+
100+
return pipeline
101+
}

packages/orgx/lib/evaluate.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @typedef {import('./types.js').OrgModule} ExportMap
3+
* @typedef {import('vfile').VFileCompatible} VFileCompatible
4+
* @typedef {import('./util/resolve-evaluate-options.js').EvaluateOptions} EvaluateOptions
5+
*/
6+
7+
import { compile, compileSync } from './compile.js'
8+
import { run, runSync } from './run.js'
9+
import { resolveEvaluateOptions } from './util/resolve-evaluate-options.js'
10+
11+
/**
12+
* Evaluate MDX.
13+
*
14+
* @param {VFileCompatible} vfileCompatible
15+
* MDX document to parse (`string`, `Buffer`, `vfile`, anything that can be
16+
* given to `vfile`).
17+
* @param {EvaluateOptions} evaluateOptions
18+
* Configuration for evaluation.
19+
* @return {Promise<ExportMap>}
20+
* Export map.
21+
*/
22+
export async function evaluate(vfileCompatible, evaluateOptions) {
23+
const { compiletime, runtime } = resolveEvaluateOptions(evaluateOptions)
24+
return run(await compile(vfileCompatible, compiletime), runtime)
25+
}
26+
27+
/**
28+
* Synchronously evaluate MDX.
29+
*
30+
* @param {VFileCompatible} vfileCompatible
31+
* MDX document to parse (`string`, `Buffer`, `vfile`, anything that can be
32+
* given to `vfile`).
33+
* @param {EvaluateOptions} evaluateOptions
34+
* Configuration for evaluation.
35+
* @return {ExportMap}
36+
* Export map.
37+
*/
38+
export function evaluateSync(vfileCompatible, evaluateOptions) {
39+
const { compiletime, runtime } = resolveEvaluateOptions(evaluateOptions)
40+
return runSync(compileSync(vfileCompatible, compiletime), runtime)
41+
}

0 commit comments

Comments
 (0)