Skip to content

Commit

Permalink
feat: remark-mdx is not compatible with normal markdown syntaxes
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed May 24, 2020
1 parent 80686eb commit 96fffcf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/eslint-mdx/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {
ParserOptions,
} from './types'

export const mdxProcessor = unified().use(remarkParse).use(remarkMdx).freeze()
export const mdProcessor = unified().use(remarkParse).freeze()
export const mdxProcessor = mdProcessor().use(remarkMdx).freeze()

export const AST_PROPS = ['body', 'comments', 'tokens'] as const
export const ES_NODE_TYPES: readonly string[] = ['export', 'import', 'jsx']
Expand Down Expand Up @@ -157,7 +158,7 @@ export class Parser {
return this._eslintParse(code, options)
}

const root = mdxProcessor.parse(code) as Parent
const root = (isMdx ? mdxProcessor : mdProcessor).parse(code) as Parent

this._ast = {
...normalizePosition(root.position),
Expand Down
10 changes: 8 additions & 2 deletions packages/eslint-plugin-mdx/src/rules/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const requirePkg = (
let searchSync: (searchFrom?: string) => CosmiconfigResult
let remarkProcessor: Processor

export const getRemarkProcessor = (searchFrom: string) => {
export const getRemarkProcessor = (searchFrom: string, isMdx: boolean) => {
if (!searchSync) {
searchSync = cosmiconfigSync('remark', {
packageProp: 'remarkConfig',
Expand All @@ -66,6 +66,12 @@ export const getRemarkProcessor = (searchFrom: string) => {
// just ignore if the package does not exist
}

const initProcessor = remarkProcessor().use({ settings }).use(remarkStringify)

if (isMdx) {
initProcessor.use(remarkMdx)
}

return plugins
.reduce((processor, pluginWithSettings) => {
const [plugin, ...pluginSettings] = Array.isArray(pluginWithSettings)
Expand All @@ -78,6 +84,6 @@ export const getRemarkProcessor = (searchFrom: string) => {
: plugin,
...pluginSettings,
)
}, remarkProcessor().use({ settings }).use(remarkStringify).use(remarkMdx))
}, initProcessor)
.freeze()
}
14 changes: 8 additions & 6 deletions packages/eslint-plugin-mdx/src/rules/remark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ export const remark: Rule.RuleModule = {
const filename = context.getFilename()
const extname = path.extname(filename)
const sourceCode = context.getSourceCode()
const extensions = DEFAULT_EXTENSIONS.concat(
context.parserOptions.extensions || [],
MARKDOWN_EXTENSIONS,
context.parserOptions.markdownExtensions || [],
const options = context.parserOptions
const isMdx = DEFAULT_EXTENSIONS.concat(options.extensions || []).includes(
extname,
)
const isMarkdown = MARKDOWN_EXTENSIONS.concat(
options.markdownExtensions || [],
).includes(extname)
return {
Program(node) {
/* istanbul ignore if */
if (!extensions.includes(extname)) {
if (!isMdx && !isMarkdown) {
return
}
const sourceText = sourceCode.getText(node)
const remarkProcessor = getRemarkProcessor(filename)
const remarkProcessor = getRemarkProcessor(filename, isMdx)
const file = vfile({
path: filename,
contents: sourceText,
Expand Down

0 comments on commit 96fffcf

Please sign in to comment.