mdx-js / mdx

Markdown for the component era
https://mdxjs.com
MIT License
17.38k stars 1.14k forks source link

Provide a CLI based on `unified-args` #2378

Open remcohaszing opened 10 months ago

remcohaszing commented 10 months ago

Initial checklist

Problem

The unified ecosystem has some CLIs based on unified-args that can be used for linting and formatting. Since MDX is based on remark, remark-cli can support MDX, but it’s not obvious. By default remark-cli doesn’t contain remark-mdx, not does it check .mdx files by default.

It would be nice if there is a simple blessed way to lint MDX files.

Solution

Create a new package remark-mdx-cli. This registers the remark-mdx command.

#!/usr/bin/env node

/**
 * @typedef Pack
 * @property {string} name
 * @property {string} version
 * @property {string} description
 */

import fs from 'node:fs/promises'
import {remark} from 'remark'
import remarkMdx from 'remark-mdx'
import {args} from 'unified-args'

/** @type {Pack} */
const cli = JSON.parse(
  String(await fs.readFile(new URL('package.json', import.meta.url)))
)

args({
  description: cli.description,
  extensions: ['mdx'],
  ignoreName: '.remarkmdxignore',
  name: 'remark-mdx',
  packageField: 'remarkmdxConfig',
  pluginPrefix: 'remark',
  processor: remark.use(remarkMdx),
  rcName: '.remarkmdxrc',
  version: cli.name + ': ' + cli.version
})

This will also be accompanied by a language server (remark-mdx-language-server) and VSCode plugin (unifiedjs.vscode-remark-mdx) based on unified-language-server.

Alternatives

There are plenty of variations possible on the names of various parameters. I.e. should the package name be scoped? Should the command be different? Should it use .remarkignore instead of .remarkmdxignore?

wooorm commented 10 months ago

simple way is remark -e mdx -u mdx? Not sure if another CLI that needs to be documented is an improvement, over adding docs somewhere on how to do this?

wooorm commented 10 months ago

By extension, should we have remark-gfm-cli? What if you want to combine both?

wooorm commented 10 months ago

And, wouldn’t an MDX CLI be more about compiling/evaluating code?

remcohaszing commented 10 months ago

IMO configuration is usually preferred over CLI arguments.

This goes for other tooling as well. I.e. it’s perfectly possible to run git, Prettier, or ESLint using CLI arguments and no configuration, but it’s very inconvenient and you don’t get editor integrations. IMO this falls in the same category.

Perhaps we could also investigate an MDX CLI for compiling code, but that’s a different issue and I don’t see a need for that right now.

remcohaszing commented 10 months ago

The difference between GFM and MDX is that typically it’s fine to treat other .md files as GFM, but it’s not ok to treat .md files as MDX, or .mdx files as regular markdown.

wooorm commented 10 months ago