|
| 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 | +} |
0 commit comments