-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: resolve remark plugin with prefix
remark-
automatically
- Loading branch information
Showing
5 changed files
with
92 additions
and
58 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
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,74 @@ | ||
import remarkStringify from 'remark-stringify' | ||
import unified, { Processor } from 'unified' | ||
import remarkMdx from 'remark-mdx' | ||
import remarkParse from 'remark-parse' | ||
|
||
import { RemarkConfig } from './types' | ||
|
||
import cosmiconfig, { Explorer, CosmiconfigResult } from 'cosmiconfig' | ||
|
||
export const requirePkg = (plugin: string, prefix: string) => { | ||
prefix = prefix.endsWith('-') ? prefix : prefix + '-' | ||
const packages = [ | ||
plugin, | ||
plugin.startsWith('@') | ||
? plugin.replace('/', '/' + prefix) | ||
: prefix + plugin, | ||
] | ||
let error: Error | ||
for (const pkg of packages) { | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports | ||
return require(pkg) | ||
} catch (err) { | ||
if (!error) { | ||
error = err | ||
} | ||
} | ||
} | ||
throw error | ||
} | ||
|
||
let remarkConfig: Explorer | ||
let remarkProcessor: Processor | ||
|
||
export const getRemarkProcessor = (searchFrom: string) => { | ||
if (!remarkConfig) { | ||
remarkConfig = cosmiconfig('remark', { | ||
packageProp: 'remarkConfig', | ||
}) | ||
} | ||
|
||
if (!remarkProcessor) { | ||
remarkProcessor = unified() | ||
.use(remarkParse) | ||
.freeze() | ||
} | ||
|
||
/* istanbul ignore next */ | ||
const { plugins = [], settings }: Partial<RemarkConfig> = | ||
(remarkConfig.searchSync(searchFrom) || ({} as CosmiconfigResult)).config || | ||
{} | ||
|
||
// disable this rule automatically since we already have a parser option `extensions` | ||
plugins.push(['lint-file-extension', false]) | ||
|
||
return plugins | ||
.reduce( | ||
(remarkProcessor, pluginWithSettings) => { | ||
const [plugin, ...pluginSettings] = Array.isArray(pluginWithSettings) | ||
? pluginWithSettings | ||
: [pluginWithSettings] | ||
return remarkProcessor.use( | ||
/* istanbul ignore next */ | ||
typeof plugin === 'string' ? requirePkg(plugin, 'remark') : plugin, | ||
...pluginSettings, | ||
) | ||
}, | ||
remarkProcessor() | ||
.use({ settings }) | ||
.use(remarkStringify) | ||
.use(remarkMdx), | ||
) | ||
.freeze() | ||
} |
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 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 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,12 @@ | ||
import { requirePkg } from 'eslint-plugin-mdx' | ||
|
||
describe('Helpers', () => { | ||
it('should resolve package correctly', () => { | ||
expect(requirePkg('@1stg/config', 'husky')).toBeDefined() | ||
expect(requirePkg('lint', 'remark')).toBeDefined() | ||
expect(requirePkg('remark-parse', 'non existed')).toBeDefined() | ||
}) | ||
|
||
it('should throw on non existed package', () => | ||
expect(() => requirePkg('@1stg/config', 'unexpected-')).toThrow()) | ||
}) |