-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor loading remark plugins from tsconfig.json (#383)
`@mdx-js/language-service` now exposes a function `resolveRemarkPlugins`, which resolves remark plugins in a environment agnostic manner. The language server implements Node.js specific logic to resolve this from `tsconfig.json`.
- Loading branch information
1 parent
ca6a805
commit 135f633
Showing
5 changed files
with
109 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@mdx-js/language-service": patch | ||
--- | ||
|
||
Expose function `resolveRemarkPlugins` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export {createMdxLanguagePlugin} from './lib/language-plugin.js' | ||
export {createMdxServicePlugin} from './lib/service-plugin.js' | ||
export {resolveRemarkPlugins} from './lib/tsconfig.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* @typedef {import('typescript').ParsedCommandLine} ParsedCommandLine | ||
* @typedef {import('unified').Pluggable} Pluggable | ||
* @typedef {import('unified').PluggableList} PluggableList | ||
* @typedef {import('unified').Plugin} Plugin | ||
*/ | ||
|
||
/** | ||
* Resolve remark plugins from TypeScript’s parsed command line options. | ||
* | ||
* @param {unknown} mdxConfig | ||
* The parsed command line options from which to resolve plugins. | ||
* @param {(name: string) => Plugin | PromiseLike<Plugin>} resolvePlugin | ||
* A function to resolve a single remark plugin. | ||
* @returns {Promise<PluggableList | undefined>} | ||
* An array of resolved plugins, or `undefined` in case of an invalid configuration. | ||
*/ | ||
export async function resolveRemarkPlugins(mdxConfig, resolvePlugin) { | ||
if ( | ||
typeof mdxConfig !== 'object' || | ||
!mdxConfig || | ||
!('plugins' in mdxConfig) | ||
) { | ||
return | ||
} | ||
|
||
const pluginConfig = mdxConfig.plugins | ||
|
||
if (typeof pluginConfig !== 'object' || !pluginConfig) { | ||
return | ||
} | ||
|
||
const pluginArray = Array.isArray(pluginConfig) | ||
? pluginConfig | ||
: Object.entries(pluginConfig) | ||
|
||
if (pluginArray.length === 0) { | ||
return | ||
} | ||
|
||
/** @type {Promise<Pluggable>[]} */ | ||
const plugins = [] | ||
for (const maybeTuple of pluginArray) { | ||
const [name, ...options] = Array.isArray(maybeTuple) | ||
? maybeTuple | ||
: [maybeTuple] | ||
|
||
if (typeof name !== 'string') { | ||
continue | ||
} | ||
|
||
plugins.push( | ||
Promise.resolve(name) | ||
.then(resolvePlugin) | ||
.then((plugin) => [plugin, ...options]) | ||
) | ||
} | ||
|
||
return Promise.all(plugins) | ||
} |