mmdfmt 0.0.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.ja.md +784 -0
- package/README.md +677 -0
- package/lib/cli.d.mts +1 -0
- package/lib/cli.mjs +433 -0
- package/lib/format-file-DSbEElSV.mjs +545 -0
- package/lib/index.d.mts +72 -0
- package/lib/index.mjs +22 -0
- package/package.json +34 -6
package/README.md
ADDED
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
# mmdfmt
|
|
2
|
+
|
|
3
|
+
Japanese version: [README.ja.md](./README.ja.md)
|
|
4
|
+
|
|
5
|
+
Format the [Mermaid](https://mermaid.js.org/) diagrams in your Markdown and
|
|
6
|
+
`.mmd` files, and leave everything else as you wrote it.
|
|
7
|
+
|
|
8
|
+
`mmdfmt` formats `.mmd` and `.mermaid` files, and the `mermaid` and `mmd` code
|
|
9
|
+
blocks of `.md` and `.markdown` files. The rest of a Markdown file stays byte
|
|
10
|
+
for byte as it is. The diagrams are formatted by
|
|
11
|
+
[Prettier](https://prettier.io/) with
|
|
12
|
+
[`prettier-plugin-mmd`](../prettier-plugin-mmd), the Mermaid plugin for
|
|
13
|
+
Prettier, so they come out as that plugin prints them, apart from the cases in
|
|
14
|
+
[If you know Prettier](#if-you-know-prettier).
|
|
15
|
+
|
|
16
|
+
This is a third-party project, not affiliated with
|
|
17
|
+
[mermaid-js](https://mermaid.js.org/). The
|
|
18
|
+
[mmdfmt guide](https://mmd-toolkit.github.io/mmd-toolkit/formatter/mmdfmt.html)
|
|
19
|
+
is on the documentation site.
|
|
20
|
+
|
|
21
|
+
## When to use it
|
|
22
|
+
|
|
23
|
+
Use `mmdfmt` when you want your diagrams formatted but do not let Prettier
|
|
24
|
+
format your Markdown:
|
|
25
|
+
|
|
26
|
+
- You format Markdown with another formatter, such as `markdownlint --fix` or
|
|
27
|
+
dprint. Prettier would rewrite the same text its own way.
|
|
28
|
+
- You keep your Markdown as you write it, on purpose.
|
|
29
|
+
- You use Prettier for other files, and keep it off your Markdown.
|
|
30
|
+
|
|
31
|
+
If Prettier already formats your Markdown, add `prettier-plugin-mmd` to it
|
|
32
|
+
instead: Prettier then formats the diagrams in the same run, and you do not need
|
|
33
|
+
`mmdfmt`.
|
|
34
|
+
|
|
35
|
+
`mmdfmt` needs no Prettier setup; it is one command.
|
|
36
|
+
|
|
37
|
+
## What it does not do
|
|
38
|
+
|
|
39
|
+
- It formats nothing but diagrams.
|
|
40
|
+
- It reports no mistakes in a diagram beyond what mermaid.js rejects;
|
|
41
|
+
[`@mmd-toolkit/eslint-plugin`](../eslint-plugin) does that.
|
|
42
|
+
|
|
43
|
+
## Quick start
|
|
44
|
+
|
|
45
|
+
`mmdfmt` needs Node.js `^22.13.0 || >=24`. `--write` rewrites files in place, so
|
|
46
|
+
commit your work first, or see which files would change with
|
|
47
|
+
`npx mmdfmt --check .`. To format every diagram under the current directory:
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
npx mmdfmt --write .
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Only the diagrams change. This `notes.md`:
|
|
54
|
+
|
|
55
|
+
<!-- prettier-ignore -->
|
|
56
|
+
````md
|
|
57
|
+
# Notes
|
|
58
|
+
|
|
59
|
+
* A list item that mmdfmt leaves as it is.
|
|
60
|
+
|
|
61
|
+
```mermaid
|
|
62
|
+
flowchart LR
|
|
63
|
+
A --> B; B --> C
|
|
64
|
+
```
|
|
65
|
+
````
|
|
66
|
+
|
|
67
|
+
becomes:
|
|
68
|
+
|
|
69
|
+
<!-- prettier-ignore -->
|
|
70
|
+
````md
|
|
71
|
+
# Notes
|
|
72
|
+
|
|
73
|
+
* A list item that mmdfmt leaves as it is.
|
|
74
|
+
|
|
75
|
+
```mermaid
|
|
76
|
+
flowchart LR
|
|
77
|
+
A --> B
|
|
78
|
+
B --> C
|
|
79
|
+
```
|
|
80
|
+
````
|
|
81
|
+
|
|
82
|
+
Without `mmdfmt` installed, `npx` asks to download it and runs its latest
|
|
83
|
+
version; to keep using one version, [install it](#installation).
|
|
84
|
+
|
|
85
|
+
## Installation
|
|
86
|
+
|
|
87
|
+
A new version of `mmdfmt` can format a diagram a little differently. Pin an
|
|
88
|
+
exact version in your project, so that everyone who works on it, and your CI,
|
|
89
|
+
format the same way:
|
|
90
|
+
|
|
91
|
+
```sh
|
|
92
|
+
npm install --save-dev --save-exact mmdfmt
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
With pnpm, `pnpm add --save-dev --save-exact mmdfmt`. `npx mmdfmt` (or
|
|
96
|
+
`pnpm exec mmdfmt`) then runs that version.
|
|
97
|
+
|
|
98
|
+
To use it yourself across many repositories, install it globally, and run it as
|
|
99
|
+
`mmdfmt`:
|
|
100
|
+
|
|
101
|
+
```sh
|
|
102
|
+
npm install --global mmdfmt
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
A global install is not pinned to a project, so two machines can format a
|
|
106
|
+
diagram differently.
|
|
107
|
+
|
|
108
|
+
## Usage
|
|
109
|
+
|
|
110
|
+
```sh
|
|
111
|
+
mmdfmt --write [--check | --list-different] <pattern>...
|
|
112
|
+
mmdfmt --check <pattern>...
|
|
113
|
+
mmdfmt --list-different <pattern>...
|
|
114
|
+
mmdfmt --stdin-filepath <path>
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
| Option | What it does |
|
|
118
|
+
| ------------------------- | --------------------------------------------------------------------------------------------- |
|
|
119
|
+
| `-w`, `--write` | Formats the files in place, and lists each file it formats on standard output, with its time. |
|
|
120
|
+
| `-c`, `--check` | Checks whether the files are formatted, and prints a summary and the files that are not. |
|
|
121
|
+
| `-l`, `--list-different` | Changes nothing. Lists on standard output the files that `--write` would change. |
|
|
122
|
+
| `--stdin-filepath <path>` | Formats standard input as if it were the file at `<path>`, and writes it to standard output. |
|
|
123
|
+
| `-h`, `--help` | Prints the usage on standard output. |
|
|
124
|
+
| `-v`, `--version` | Prints the version on standard output. |
|
|
125
|
+
|
|
126
|
+
Give patterns with at least one of `--write`, `--check`, or `--list-different`.
|
|
127
|
+
`--write` can be combined with either `--check` or `--list-different`:
|
|
128
|
+
`--write --check` rewrites files and prints the check's summary and warnings;
|
|
129
|
+
`--write --list-different` rewrites files and lists only the ones that changed.
|
|
130
|
+
`--check` and `--list-different` cannot be combined. `--no-write`, `--no-check`,
|
|
131
|
+
and `--no-list-different` turn the option off. `--stdin-filepath` takes no
|
|
132
|
+
patterns, and with it `--write` writes no file. `--help` and `--version` only
|
|
133
|
+
print, whatever else of this table is given with them. With no argument at all,
|
|
134
|
+
`mmdfmt` prints the usage on standard error and exits with `2`.
|
|
135
|
+
|
|
136
|
+
`--write` alone prints a line for each file it formats, as Prettier does: the
|
|
137
|
+
name and the time that formatting it took, such as `docs/flow.mmd 12ms`,
|
|
138
|
+
followed by `(unchanged)` for a file that did not change. On a terminal that
|
|
139
|
+
shows colors, the name of an unchanged file is gray. To list only the files that
|
|
140
|
+
changed, give `--write --list-different`. The name of a file in what `mmdfmt`
|
|
141
|
+
prints is its path relative to the directory where it runs, with `/` between its
|
|
142
|
+
parts also on Windows, as Prettier writes it.
|
|
143
|
+
|
|
144
|
+
There are no other options: no formatting options, no configuration file of its
|
|
145
|
+
own, and no option for an ignore file. An option that takes no value ignores a
|
|
146
|
+
value written after `=`: `--write=false` is read as `--write`.
|
|
147
|
+
|
|
148
|
+
When the reader of standard output goes away, as `head` does once it has its
|
|
149
|
+
lines, `mmdfmt` writes no more there and says nothing, and finishes the run:
|
|
150
|
+
`--write` still rewrites every file, and the exit code still tells the result.
|
|
151
|
+
|
|
152
|
+
### Patterns
|
|
153
|
+
|
|
154
|
+
- The path of a file selects that file.
|
|
155
|
+
- The path of a directory, such as `.`, selects the files under it.
|
|
156
|
+
- Any other pattern is a glob, which also matches names that start with a dot.
|
|
157
|
+
Quote it, so that `mmdfmt` expands it rather than your shell:
|
|
158
|
+
`npx mmdfmt --write "docs/**/*.md"`. On Windows, a `\` in a glob is read as
|
|
159
|
+
`/`.
|
|
160
|
+
- A pattern that starts with `!` takes the files it matches out of the others.
|
|
161
|
+
See [Excluding files](#excluding-files).
|
|
162
|
+
- After `--`, every argument is a pattern, so a file whose name starts with `-`
|
|
163
|
+
can be given: `mmdfmt --write -- -a.md`.
|
|
164
|
+
|
|
165
|
+
Of the selected files, those ending in `.md`, `.markdown`, `.mmd`, or `.mermaid`
|
|
166
|
+
are formatted, each once, however many patterns select it. A pattern that
|
|
167
|
+
selects none of them is an error: `mmdfmt --check .` fails in a project without
|
|
168
|
+
such a file, and so does a file with another ending given by name. A pattern
|
|
169
|
+
whose files a `!` pattern all takes out is an error too, as in Prettier's CLI. A
|
|
170
|
+
pattern that selects only files that the `.gitignore` lists is not an error.
|
|
171
|
+
|
|
172
|
+
As Prettier's CLI does, `mmdfmt` skips without a word a pattern that names a
|
|
173
|
+
path in `node_modules` or in a directory of version control, whether a file, a
|
|
174
|
+
directory, or a glob, such as `node_modules/pkg` or `"node_modules/**/*.md"`.
|
|
175
|
+
When no pattern is left to select files, that is an error.
|
|
176
|
+
|
|
177
|
+
## Excluding files
|
|
178
|
+
|
|
179
|
+
`mmdfmt` leaves out:
|
|
180
|
+
|
|
181
|
+
- the files under `node_modules`, and under the directories of version control
|
|
182
|
+
(`.git`, `.hg`, `.jj`, `.sl`, `.svn`), also when a pattern names them
|
|
183
|
+
([Patterns](#patterns));
|
|
184
|
+
- the files that the `.gitignore` in the directory where you run `mmdfmt` lists.
|
|
185
|
+
Other `.gitignore` files, in subdirectories or above, are not read;
|
|
186
|
+
- the files that a `!` pattern matches. In bash, put such a pattern in single
|
|
187
|
+
quotes, since `!` in double quotes starts a history expansion:
|
|
188
|
+
`npx mmdfmt --write . '!vendor/**'`. In the Windows command prompt, use double
|
|
189
|
+
quotes.
|
|
190
|
+
|
|
191
|
+
`mmdfmt` does not read `.prettierignore`. A project that keeps Prettier off its
|
|
192
|
+
Markdown by listing it there still gets its diagrams formatted.
|
|
193
|
+
|
|
194
|
+
To leave one diagram in a Markdown file as it is, put `<!-- prettier-ignore -->`
|
|
195
|
+
before its code block, with nothing between them but blank lines. Outside lists
|
|
196
|
+
and block quotes, `<!-- prettier-ignore-start -->` and
|
|
197
|
+
`<!-- prettier-ignore-end -->` leave every diagram between them. These are the
|
|
198
|
+
comments that Prettier reads. To leave a whole `.mmd` file as it is, exclude it
|
|
199
|
+
with a `!` pattern or `.gitignore`.
|
|
200
|
+
|
|
201
|
+
## Checking in CI
|
|
202
|
+
|
|
203
|
+
Install `mmdfmt` in the project, install the dependencies in CI (`npm ci`), and
|
|
204
|
+
run:
|
|
205
|
+
|
|
206
|
+
```sh
|
|
207
|
+
npx mmdfmt --check .
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
Without the dependencies installed, `npx` would run the latest version instead
|
|
211
|
+
of the pinned one.
|
|
212
|
+
|
|
213
|
+
`--check` prints `Checking formatting...` on standard output, then
|
|
214
|
+
`[warn] <name>` on standard error for each file that would change, and at the
|
|
215
|
+
end one summary. When a file would change, the summary is on standard error, and
|
|
216
|
+
the exit code is `1`:
|
|
217
|
+
|
|
218
|
+
```txt
|
|
219
|
+
[warn] Code style issues found in the above file. Run mmdfmt with --write to fix.
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
With more than one such file, it says `in <n> files` instead of
|
|
223
|
+
`in the above file`. On a terminal that shows colors, the word `warn` is yellow.
|
|
224
|
+
When no file would change, the summary is
|
|
225
|
+
`All diagrams in the matched files use Prettier code style!` on standard output,
|
|
226
|
+
and the exit code is `0`. `--list-different` prints only the names of the files
|
|
227
|
+
that would change, on standard output, with the same exit codes.
|
|
228
|
+
|
|
229
|
+
With `--write --check`, the files are rewritten, the summary is
|
|
230
|
+
`[warn] Code style issues fixed in the above file.` (or `in <n> files.`), and
|
|
231
|
+
the exit code is `0`. With an error, the exit code is `2`;
|
|
232
|
+
[Errors and exit codes](#errors-and-exit-codes) says which summary then follows.
|
|
233
|
+
|
|
234
|
+
A `.mmd` or `.mermaid` file whose diagram mermaid.js rejects fails the check
|
|
235
|
+
with `2`. A diagram in Markdown that mermaid.js rejects gives only a warning,
|
|
236
|
+
because a document can show a broken diagram on purpose, and `mmdfmt` has no
|
|
237
|
+
option to fail on it. To fail CI on such a diagram, lint the Markdown with
|
|
238
|
+
[`@mmd-toolkit/eslint-plugin`](../eslint-plugin), which reports it.
|
|
239
|
+
|
|
240
|
+
In a project without any diagram file, `--check .` fails with `2`; give the
|
|
241
|
+
directories that hold diagrams instead of `.`.
|
|
242
|
+
|
|
243
|
+
## Formatting before a commit
|
|
244
|
+
|
|
245
|
+
With [lint-staged](https://github.com/lint-staged/lint-staged), which a Git hook
|
|
246
|
+
manager such as husky or simple-git-hooks runs on each commit, add this to
|
|
247
|
+
`package.json`:
|
|
248
|
+
|
|
249
|
+
```json
|
|
250
|
+
{
|
|
251
|
+
"lint-staged": {
|
|
252
|
+
"*.{md,markdown,mmd,mermaid}": "mmdfmt --write"
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Keep the glob to these four endings: `mmdfmt` has no option such as Prettier's
|
|
258
|
+
`--ignore-unknown`, and a file with another ending is an error. lint-staged
|
|
259
|
+
passes absolute paths, which `mmdfmt` accepts.
|
|
260
|
+
|
|
261
|
+
## From an editor or another tool
|
|
262
|
+
|
|
263
|
+
`mmdfmt` has no editor extension. An editor or a tool that runs a formatter on
|
|
264
|
+
standard input can run:
|
|
265
|
+
|
|
266
|
+
```sh
|
|
267
|
+
mmdfmt --stdin-filepath docs/guide.md
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
with the text of the file on standard input, and take the formatted text from
|
|
271
|
+
standard output. The file at the path is neither read nor written. The path, if
|
|
272
|
+
relative, is resolved against the directory where `mmdfmt` runs; its ending says
|
|
273
|
+
whether the text is a diagram or Markdown, and its directory says which
|
|
274
|
+
`.editorconfig` and Prettier configuration apply.
|
|
275
|
+
|
|
276
|
+
- A Markdown text is written out in full, formatted or already formatted, and
|
|
277
|
+
the exit code is `0`, also when a diagram in it is rejected; the warning goes
|
|
278
|
+
to standard error.
|
|
279
|
+
- A diagram whose text mermaid.js rejects gets nothing on standard output, an
|
|
280
|
+
error on standard error, and the exit code `2`.
|
|
281
|
+
- A path under `node_modules` or a directory of version control, or listed in
|
|
282
|
+
the `.gitignore` of the directory where `mmdfmt` runs, gets its text back
|
|
283
|
+
unchanged.
|
|
284
|
+
- With `--check` or `--list-different`, the text is not written out: when it
|
|
285
|
+
would change, `(stdin)` goes to standard output and the exit code is `1`,
|
|
286
|
+
unless `--write` is given too; otherwise nothing is written and the exit code
|
|
287
|
+
is `0`. The position of a warning is in the text as it was read.
|
|
288
|
+
- A path with another ending is an error.
|
|
289
|
+
|
|
290
|
+
## From a script
|
|
291
|
+
|
|
292
|
+
An editor extension, a language server, or another tool written for Node.js can
|
|
293
|
+
format the diagrams of a file as the `mmdfmt` command does, without running it.
|
|
294
|
+
(A tool that is not written for Node.js can run the command with
|
|
295
|
+
[`--stdin-filepath`](#from-an-editor-or-another-tool).) Add `mmdfmt` to the
|
|
296
|
+
dependencies of the tool, pinned as in [Installation](#installation), since a
|
|
297
|
+
new version can format a diagram a little differently:
|
|
298
|
+
|
|
299
|
+
```sh
|
|
300
|
+
npm install --save-exact mmdfmt
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
It needs Node.js `^22.13.0 || >=24`. Import its functions `resolveConfig`,
|
|
304
|
+
`format`, and `clearConfigCache`. Their arguments and results follow Prettier's
|
|
305
|
+
functions of the same names, and each section below says where they differ. This
|
|
306
|
+
formats the diagrams of `docs/notes.md`:
|
|
307
|
+
|
|
308
|
+
```js
|
|
309
|
+
import { readFile } from "node:fs/promises"
|
|
310
|
+
import { format, resolveConfig } from "mmdfmt"
|
|
311
|
+
|
|
312
|
+
const filepath = "docs/notes.md"
|
|
313
|
+
const source = await readFile(filepath, "utf8")
|
|
314
|
+
const config = await resolveConfig(filepath)
|
|
315
|
+
const formatted = await format(source, { ...config, filepath })
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
`formatted` is the text that the command writes for `docs/notes.md`: its
|
|
319
|
+
diagrams formatted, and the rest as written. Writing it to the file is up to the
|
|
320
|
+
tool.
|
|
321
|
+
|
|
322
|
+
### `resolveConfig(fileUrlOrPath)`
|
|
323
|
+
|
|
324
|
+
`resolveConfig` finds the configuration of a file as the command finds it: the
|
|
325
|
+
Prettier configuration files and the `.editorconfig` that apply to the file,
|
|
326
|
+
`overrides` included (see [Configuration](#configuration)). Pass the path of the
|
|
327
|
+
file or a file URL; a relative path is resolved against the current directory,
|
|
328
|
+
so a language server passes an absolute path or a URL. The file itself is not
|
|
329
|
+
read, and need not exist.
|
|
330
|
+
|
|
331
|
+
It returns a promise of an object of Prettier options to pass to `format`, or of
|
|
332
|
+
`null` when no configuration applies to the file. Spreading `null` adds nothing,
|
|
333
|
+
so `{ ...config, filepath }` works in both cases. The object holds only the
|
|
334
|
+
options that the configuration sets, of the eight that `mmdfmt` reads, listed in
|
|
335
|
+
[Configuration](#configuration): with a configuration that sets no `tabWidth`,
|
|
336
|
+
`{ tabWidth: 4, ...config, filepath }` keeps a `tabWidth` of 4. With TypeScript,
|
|
337
|
+
its type is `ReadOptions`. The promise is rejected when a configuration file
|
|
338
|
+
cannot be read.
|
|
339
|
+
|
|
340
|
+
`resolveConfig` reads `.editorconfig` as the command does, and as Prettier's CLI
|
|
341
|
+
does by default; Prettier's `resolveConfig` reads it only when it is given
|
|
342
|
+
`editorconfig: true`.
|
|
343
|
+
|
|
344
|
+
### `clearConfigCache()`
|
|
345
|
+
|
|
346
|
+
`resolveConfig` keeps the configuration files that it has read for the life of
|
|
347
|
+
the process, as Prettier's does, so a later call does not see a change to them.
|
|
348
|
+
Call `clearConfigCache` when the tool learns that a Prettier configuration file
|
|
349
|
+
or an `.editorconfig` has changed, for example from the editor's file watcher:
|
|
350
|
+
the next call of `resolveConfig` reads the files again. It returns a promise
|
|
351
|
+
that is resolved once the files are forgotten.
|
|
352
|
+
|
|
353
|
+
### `format(source, options)`
|
|
354
|
+
|
|
355
|
+
`format` formats a text as Prettier's `format` does, except that in Markdown it
|
|
356
|
+
formats only the Mermaid code blocks and leaves the rest as written. It reads no
|
|
357
|
+
file and no configuration.
|
|
358
|
+
|
|
359
|
+
- `source`: the text of the file.
|
|
360
|
+
- `options.filepath`: the path of the file, required. Only its ending is used:
|
|
361
|
+
the text is read as one diagram for `.mmd` and `.mermaid`, and as Markdown for
|
|
362
|
+
`.md` and `.markdown`. The file need not exist.
|
|
363
|
+
- The other options: what `resolveConfig` returns for the same path, since the
|
|
364
|
+
`overrides` of a configuration apply by path. They are the eight options
|
|
365
|
+
listed in [Configuration](#configuration). For Markdown, `printWidth` is the
|
|
366
|
+
width for the whole file: a diagram in a list item or a block quote is
|
|
367
|
+
formatted to that width less the width of what stands before its lines (see
|
|
368
|
+
[Code blocks in detail](#code-blocks-in-detail)).
|
|
369
|
+
- Another option, such as `parser`, `endOfLine`, or `semi`, is not an option of
|
|
370
|
+
`format`: with TypeScript it is a type error, and in JavaScript it is ignored.
|
|
371
|
+
With TypeScript, the type of the options is `FormatOptions`.
|
|
372
|
+
|
|
373
|
+
It returns a promise of the formatted text. The line endings and a byte order
|
|
374
|
+
mark stay as they are in the text (see
|
|
375
|
+
[Code blocks in detail](#code-blocks-in-detail)).
|
|
376
|
+
|
|
377
|
+
In Markdown, `format` leaves three kinds of block as written and reports none of
|
|
378
|
+
them: a diagram that mermaid.js rejects, a block with a tab in its indentation
|
|
379
|
+
that is read partly as the diagram, and a block whose opening fence stands on
|
|
380
|
+
the line of a footnote label (see
|
|
381
|
+
[Code blocks in detail](#code-blocks-in-detail)). Prettier leaves an embedded
|
|
382
|
+
code block that it cannot format in the same way. To find the diagrams that
|
|
383
|
+
mermaid.js rejects, parse them with [`@mmd-toolkit/parser`](../parser) or lint
|
|
384
|
+
the file with [`@mmd-toolkit/eslint-plugin`](../eslint-plugin).
|
|
385
|
+
|
|
386
|
+
The promise is rejected:
|
|
387
|
+
|
|
388
|
+
- with the `SyntaxError` that Prettier throws for a `.mmd` or `.mermaid` text
|
|
389
|
+
that mermaid.js rejects, when it formats the text with `prettier-plugin-mmd`.
|
|
390
|
+
Its `loc.start` and `loc.end` give the `line` and the `column` where the error
|
|
391
|
+
starts and where it ends, both counted from 1. An error whose range runs on to
|
|
392
|
+
the start of a later line, such as an error at a line break, has no `loc.end`;
|
|
393
|
+
- with Prettier's error, when an option has a value that Prettier refuses, such
|
|
394
|
+
as `printWidth: "wide"` (`Invalid printWidth value. …`), for a `.mmd` or
|
|
395
|
+
`.mermaid` text, and for a Markdown text with a code block whose diagram
|
|
396
|
+
`mmdfmt` formats ([Code blocks in detail](#code-blocks-in-detail)), whether or
|
|
397
|
+
not mermaid.js rejects the diagram. A Markdown text with no such block is
|
|
398
|
+
returned as it is: one with no `mermaid` or `mmd` code block, or one whose
|
|
399
|
+
`mermaid` and `mmd` blocks are all left as written, after
|
|
400
|
+
`<!-- prettier-ignore -->`, between `<!-- prettier-ignore-start -->` and
|
|
401
|
+
`<!-- prettier-ignore-end -->`, on the line of a footnote label, or with a tab
|
|
402
|
+
in their indentation read partly as the diagram;
|
|
403
|
+
- with an error, when `options.filepath` does not end in `.md`, `.markdown`,
|
|
404
|
+
`.mmd`, or `.mermaid`.
|
|
405
|
+
|
|
406
|
+
### Which files to format
|
|
407
|
+
|
|
408
|
+
The command formats only the files that end in `.md`, `.markdown`, `.mmd`, or
|
|
409
|
+
`.mermaid`, and skips a path under `node_modules` or a directory of version
|
|
410
|
+
control, or a path that the `.gitignore` of the directory where it runs lists
|
|
411
|
+
(see [Excluding files](#excluding-files)). `format` rejects the other endings,
|
|
412
|
+
but formats the text of any other path that it is given, wherever the path is: a
|
|
413
|
+
tool that should skip the same files as the command checks them before it calls
|
|
414
|
+
`format`.
|
|
415
|
+
|
|
416
|
+
## Configuration
|
|
417
|
+
|
|
418
|
+
A diagram is indented by two spaces, and its line width is Prettier's 80. When
|
|
419
|
+
an `.editorconfig` or a Prettier configuration applies to the file, `mmdfmt`
|
|
420
|
+
reads eight options of it, found as Prettier's CLI finds it, `overrides`
|
|
421
|
+
included: `indent_style`, `indent_size`, `tab_width`, and `max_line_length` of
|
|
422
|
+
`.editorconfig`, or the options of a
|
|
423
|
+
[Prettier configuration file](https://prettier.io/docs/configuration). For a
|
|
424
|
+
code block, the file is the Markdown file. A configuration file that cannot be
|
|
425
|
+
read is an error. For example, this `.editorconfig` indents diagrams by four
|
|
426
|
+
spaces and lets a line stay up to 100 columns wide:
|
|
427
|
+
|
|
428
|
+
```ini
|
|
429
|
+
[*.{md,mmd,mermaid}]
|
|
430
|
+
indent_style = space
|
|
431
|
+
indent_size = 4
|
|
432
|
+
max_line_length = 100
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
The eight options are:
|
|
436
|
+
|
|
437
|
+
- `printWidth` is the line width. It decides two things in a diagram: whether a
|
|
438
|
+
data block `@{ … }` on one line is printed on several lines, and whether the
|
|
439
|
+
colons of the tasks of a Gantt section are lined up (see
|
|
440
|
+
[Formatter options](https://mmd-toolkit.github.io/mmd-toolkit/formatter/options.html#line-width)).
|
|
441
|
+
`max_line_length = off` is no limit. In a code block in a list or a block
|
|
442
|
+
quote, `printWidth` is less the width of what `mmdfmt` writes before the lines
|
|
443
|
+
(see [Code blocks in detail](#code-blocks-in-detail)).
|
|
444
|
+
- `tabWidth` and `useTabs` set the indentation of a diagram.
|
|
445
|
+
- `embeddedLanguageFormatting`, `singleQuote`, `bracketSpacing`, `proseWrap`,
|
|
446
|
+
and `trailingComma` apply to the YAML frontmatter of a diagram, as they do
|
|
447
|
+
when Prettier formats it with `prettier-plugin-mmd`.
|
|
448
|
+
`embeddedLanguageFormatting: "off"` leaves the frontmatter as written.
|
|
449
|
+
|
|
450
|
+
`mmdfmt` reads no other option, not even one that a later version of Prettier
|
|
451
|
+
adds. The range (`rangeStart` and `rangeEnd`), `cursorOffset`, the pragma
|
|
452
|
+
options (`requirePragma`, `insertPragma`, and `checkIgnorePragma`), `filepath`,
|
|
453
|
+
and the options of other languages are not used. `plugins` and `parser` are not
|
|
454
|
+
used either: `mmdfmt` always formats with `prettier-plugin-mmd`, so a plugin
|
|
455
|
+
that the configuration names is not loaded. `endOfLine` is not used, since a
|
|
456
|
+
file keeps its line endings (see
|
|
457
|
+
[Code blocks in detail](#code-blocks-in-detail)).
|
|
458
|
+
|
|
459
|
+
## Errors and exit codes
|
|
460
|
+
|
|
461
|
+
A diagram that mermaid.js rejects is left as it is, and reported on standard
|
|
462
|
+
error with its line and column. The other files, and the other diagrams of the
|
|
463
|
+
file, are still formatted:
|
|
464
|
+
|
|
465
|
+
```txt
|
|
466
|
+
docs/flow.mmd:3:1: error: Unexpected end of text
|
|
467
|
+
notes.md:6:1: warning: Unexpected end of text
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
In a `.mmd` or `.mermaid` file, it is an error. In a Markdown code block, it is
|
|
471
|
+
a warning. With `--write` and `--stdin-filepath`, the position of a warning is
|
|
472
|
+
in the text after formatting, where the diagrams above it may have gained or
|
|
473
|
+
lost lines, so that it points at the block in what you open after the run. With
|
|
474
|
+
`--check` or `--list-different` without `--write`, it is in the file as it is.
|
|
475
|
+
Lines and columns count from 1, and a column counts UTF-16 code units, as most
|
|
476
|
+
editors do.
|
|
477
|
+
[Formatter compatibility](https://mmd-toolkit.github.io/mmd-toolkit/formatter/compatibility.html)
|
|
478
|
+
explains which diagrams are rejected.
|
|
479
|
+
|
|
480
|
+
| Code | Meaning |
|
|
481
|
+
| ---- | ----------------------------------------------------------------------------------------------------- |
|
|
482
|
+
| `0` | No errors; with `--check` or `--list-different`, no file would change or `--write` is given. |
|
|
483
|
+
| `1` | With `--check` or `--list-different` without `--write`, a file would change, and there are no errors. |
|
|
484
|
+
| `2` | An error. |
|
|
485
|
+
|
|
486
|
+
An error is a rejected diagram in a `.mmd` or `.mermaid` file, a file that
|
|
487
|
+
cannot be read or written, a pattern that selects no file, a configuration that
|
|
488
|
+
cannot be read, or a wrong use of the options. With an error, the code is `2`
|
|
489
|
+
even when a file would change.
|
|
490
|
+
|
|
491
|
+
When a file cannot be formatted, such as a `.mmd` or `.mermaid` file whose
|
|
492
|
+
diagram mermaid.js rejects, the summary of `--check` is
|
|
493
|
+
`Error occurred when checking code style in the above file.` (or
|
|
494
|
+
`in <n> files.`) on standard output. As in Prettier's `--check`, the summary
|
|
495
|
+
counts only the files that cannot be formatted. A file that cannot be read, and
|
|
496
|
+
a pattern that selects no file, are reported as errors, but the summary is the
|
|
497
|
+
one it would be without them, also when no pattern selects any file. A file that
|
|
498
|
+
cannot be written counts among the files whose issues were fixed, and `--write`
|
|
499
|
+
and `--write --list-different` still list it.
|
|
500
|
+
|
|
501
|
+
## If you know Prettier
|
|
502
|
+
|
|
503
|
+
The diagrams are formatted by Prettier with `prettier-plugin-mmd`, so the rules
|
|
504
|
+
of the plugin apply. Five results differ from running Prettier with the plugin
|
|
505
|
+
on Markdown and `.mmd` files:
|
|
506
|
+
|
|
507
|
+
- Prettier removes the whitespace at the end of the last line of a code block,
|
|
508
|
+
even where mermaid.js treats it as part of the diagram
|
|
509
|
+
([Markdown code blocks](../prettier-plugin-mmd/docs/compatibility.md#markdown-code-blocks)).
|
|
510
|
+
`mmdfmt` replaces only the text of the block, so that whitespace stays.
|
|
511
|
+
- Prettier formats a code block of Markdown, such as one marked `md`, as a
|
|
512
|
+
Markdown document, and with it the diagrams in its code blocks. `mmdfmt`
|
|
513
|
+
leaves every other code block as it is.
|
|
514
|
+
- Prettier prints what stands before the lines of a list item or a block quote
|
|
515
|
+
itself, while `mmdfmt` keeps it as written (see
|
|
516
|
+
[Code blocks in detail](#code-blocks-in-detail)). For a list item, Prettier
|
|
517
|
+
prints the marker and aligns the content to `tabWidth`. For a block quote, it
|
|
518
|
+
prints `> ` and drops the spaces and tabs between the `>` and the fence. Where
|
|
519
|
+
the two give the content another column, the line width left for a diagram in
|
|
520
|
+
the item or the quote differs by that, and a block of one line near the width
|
|
521
|
+
can be split by one and not by the other.
|
|
522
|
+
- In a list item or a block quote, Prettier lines up the colons of a Gantt
|
|
523
|
+
section without counting what stands before the lines, so a lined-up line can
|
|
524
|
+
be wider than the line width. `mmdfmt` counts it, so near the width a Gantt
|
|
525
|
+
section can be lined up by Prettier and not by `mmdfmt`, which then pads no
|
|
526
|
+
name to line the colons up: the whitespace before each colon stays as written,
|
|
527
|
+
and a line written wider than the line width stays so. `mmdfmt` still indents
|
|
528
|
+
the tasks as it does in any Gantt chart, so a colon moves along with its line
|
|
529
|
+
where the written indentation differs.
|
|
530
|
+
- Prettier leaves a diagram as written under four settings.
|
|
531
|
+
`requirePragma: true` leaves a Markdown file that has no `<!-- @format -->` or
|
|
532
|
+
`<!-- @prettier -->` comment at its top, after the frontmatter if there is
|
|
533
|
+
one. `checkIgnorePragma: true` leaves a Markdown file that has a
|
|
534
|
+
`<!-- @noformat -->` or `<!-- @noprettier -->` comment there. A range that
|
|
535
|
+
does not cover the whole file leaves a Markdown file or a `.mmd` file.
|
|
536
|
+
`embeddedLanguageFormatting: "off"` leaves every code block of a Markdown
|
|
537
|
+
file. `mmdfmt` formats the diagrams under all four. It does not read the first
|
|
538
|
+
three: it decides itself which files it formats (the patterns, and
|
|
539
|
+
`.gitignore`), and it formats whole diagrams. It reads
|
|
540
|
+
`embeddedLanguageFormatting` for the frontmatter of a diagram only, which it
|
|
541
|
+
then leaves as written.
|
|
542
|
+
|
|
543
|
+
The command differs from Prettier's CLI too: it does not read `.prettierignore`,
|
|
544
|
+
has no `--ignore-path`, `--ignore-unknown`, or formatting options of its own,
|
|
545
|
+
and reads the configuration for the diagrams only. A value that Prettier refuses
|
|
546
|
+
for an option that `mmdfmt` does not read, such as `semi: "yes"`, is not an
|
|
547
|
+
error here, while Prettier's CLI reports it.
|
|
548
|
+
|
|
549
|
+
The check's success line says
|
|
550
|
+
`All diagrams in the matched files use Prettier code style!`, rather than
|
|
551
|
+
Prettier's `All matched files use Prettier code style!`: `mmdfmt` formats
|
|
552
|
+
nothing but diagrams, so the rest of a Markdown file may not use that style. The
|
|
553
|
+
warning asks you to run `mmdfmt --write`, because `prettier --write` would
|
|
554
|
+
format the whole Markdown file and, without the plugin configured, would not
|
|
555
|
+
format its diagrams.
|
|
556
|
+
|
|
557
|
+
Prettier reads `--write=false` as false; `mmdfmt` ignores a value written after
|
|
558
|
+
`=` on an option that takes no value, and reads it as `--write`. The same holds
|
|
559
|
+
for `--check=false` and `--list-different=false`. Prettier also takes a `true`
|
|
560
|
+
or `false` written after `--write` as its value; `mmdfmt` reads it as a pattern,
|
|
561
|
+
so `mmdfmt --write false notes.md` formats `notes.md` and reports that no file
|
|
562
|
+
matches `false`.
|
|
563
|
+
|
|
564
|
+
`--check --list-different` is an error in both, but `mmdfmt` prints
|
|
565
|
+
`mmdfmt: error: Optional argument '--check' conflicts with '--list-different'`
|
|
566
|
+
and exits with `2`, as for any wrong use of the options, while Prettier prints
|
|
567
|
+
`[error] Cannot use --check and --list-different together.` and exits with `1`.
|
|
568
|
+
When `mmdfmt` checks which options go together, a `--no-` form counts as giving
|
|
569
|
+
its option: `--no-check --list-different` is an error, as
|
|
570
|
+
`--check --list-different` is. Prettier accepts `--no-check --list-different`.
|
|
571
|
+
|
|
572
|
+
With `--check` or `--list-different` on standard input, Prettier exits with `0`
|
|
573
|
+
for a text it cannot parse; `mmdfmt` exits with `2` for a rejected diagram, as
|
|
574
|
+
everywhere else ([Errors and exit codes](#errors-and-exit-codes)). `mmdfmt`
|
|
575
|
+
reports a rejected diagram of a `.mmd` or `.mermaid` file on one line with its
|
|
576
|
+
position, such as `docs/flow.mmd:3:1: error: Unexpected end of text`, while
|
|
577
|
+
Prettier writes `[error] docs/flow.mmd: SyntaxError: Unexpected end of text` and
|
|
578
|
+
then the lines around the place, each after `[error]`. For a rejected diagram in
|
|
579
|
+
a Markdown file, `mmdfmt` writes a warning on one line, such as
|
|
580
|
+
`notes.md:6:1: warning: Unexpected end of text`, while Prettier writes nothing
|
|
581
|
+
about it; neither treats it as an error.
|
|
582
|
+
|
|
583
|
+
Prettier also colors the word `warn` yellow and the name of an unchanged file
|
|
584
|
+
gray where its output is not a terminal, if the `CI` environment variable is set
|
|
585
|
+
or the system is Windows. `mmdfmt` colors them only on a terminal that shows
|
|
586
|
+
colors, or when `FORCE_COLOR` is set, and has no `--color` or `--no-color`.
|
|
587
|
+
|
|
588
|
+
## Code blocks in detail
|
|
589
|
+
|
|
590
|
+
The code blocks are those whose info string has `mermaid` or `mmd` as its first
|
|
591
|
+
word, case-sensitively, found as Prettier's Markdown parser finds them. A fence
|
|
592
|
+
inside another code block is not taken for a diagram, and a block inside a list
|
|
593
|
+
or a block quote is found.
|
|
594
|
+
|
|
595
|
+
Only the lines between the fences change. Each line that `mmdfmt` writes starts
|
|
596
|
+
with what stands before the opening fence on its line, with a list marker turned
|
|
597
|
+
into spaces, so that the diagram stays in its list item and its block quote. An
|
|
598
|
+
empty line gets that start without the spaces at its end, so that in a block
|
|
599
|
+
quote it is a `>` alone. For example, this block:
|
|
600
|
+
|
|
601
|
+
<!-- prettier-ignore -->
|
|
602
|
+
````md
|
|
603
|
+
> - ```mermaid
|
|
604
|
+
> flowchart LR
|
|
605
|
+
> A-->B
|
|
606
|
+
>
|
|
607
|
+
> B-->C
|
|
608
|
+
> ```
|
|
609
|
+
````
|
|
610
|
+
|
|
611
|
+
becomes:
|
|
612
|
+
|
|
613
|
+
<!-- prettier-ignore -->
|
|
614
|
+
````md
|
|
615
|
+
> - ```mermaid
|
|
616
|
+
> flowchart LR
|
|
617
|
+
> A --> B
|
|
618
|
+
>
|
|
619
|
+
> B --> C
|
|
620
|
+
> ```
|
|
621
|
+
````
|
|
622
|
+
|
|
623
|
+
When the opening fence follows a `>` at once, as in `>```mermaid`, the lines get
|
|
624
|
+
a space after the `>`. A block quote reads one space after its `>` as part of
|
|
625
|
+
its marker, so a line of the diagram that starts with a space would lose it
|
|
626
|
+
otherwise.
|
|
627
|
+
|
|
628
|
+
A block that is not closed runs to the end of its list item, its block quote, or
|
|
629
|
+
the file, and is formatted all the same. When it ends the file without a line
|
|
630
|
+
ending, its last line still has none. The lines at its end that are empty or
|
|
631
|
+
hold only spaces and tabs are left as written, and so is a block that holds
|
|
632
|
+
nothing else. Markdown reads them as part of the block, but they also part it
|
|
633
|
+
from what follows, as an empty line between two items keeps a list loose, with
|
|
634
|
+
space between its items.
|
|
635
|
+
|
|
636
|
+
Two kinds of block are left as written, with a warning that gives the position:
|
|
637
|
+
|
|
638
|
+
- a block with a tab in its indentation that is read partly as indentation and
|
|
639
|
+
partly as the diagram, as at the start of a line in a list item indented by
|
|
640
|
+
two spaces;
|
|
641
|
+
- a block whose opening fence stands on the line of a footnote label, as in
|
|
642
|
+
`[^1]: ```mermaid`.
|
|
643
|
+
|
|
644
|
+
A diagram that is empty or only whitespace between its fences becomes empty.
|
|
645
|
+
|
|
646
|
+
The line width of a diagram in a list item or a block quote is the line width of
|
|
647
|
+
the file less the width of what `mmdfmt` writes before each of its lines, the
|
|
648
|
+
width being at least 1. Each character of it counts as one column, except that a
|
|
649
|
+
tab reaches the next multiple of `tabWidth`, and counts for nothing where
|
|
650
|
+
`tabWidth` is 0. For example, with a line width of 40, a diagram in a list item
|
|
651
|
+
whose lines start with two spaces is formatted for 38 columns, and with a
|
|
652
|
+
`tabWidth` of 4, a diagram in a block quote whose lines start with `>` and a tab
|
|
653
|
+
is formatted for 36 columns, as the tab reaches column 4. Prettier takes off the
|
|
654
|
+
width of what it prints before the lines from a data block `@{ … }` in the same
|
|
655
|
+
way, but what it prints can differ from what `mmdfmt` writes (see
|
|
656
|
+
[If you know Prettier](#if-you-know-prettier)). The colons of a Gantt section
|
|
657
|
+
are the exception: Prettier does not count what stands before the lines when it
|
|
658
|
+
lines them up (see the same section).
|
|
659
|
+
|
|
660
|
+
A file keeps its line endings. Each line that `mmdfmt` writes ends as the first
|
|
661
|
+
line of the file ends, or with `\n` in a file of one line, except the last,
|
|
662
|
+
which ends as the last line of the diagram ended before formatting. The line
|
|
663
|
+
after the lines written then follows the same line ending as before; another one
|
|
664
|
+
could join with that line's into one, as a `\r` before an empty line that ends
|
|
665
|
+
with `\n` is read as one `\r\n`, and the number of lines would change. When the
|
|
666
|
+
diagram becomes empty, the line of the opening fence ends that way in place of
|
|
667
|
+
its own: the one change outside the lines between the fences. In a Markdown
|
|
668
|
+
file, only the lines of the diagrams that change are written. For the line and
|
|
669
|
+
the column of an error or a warning, a line ends at `\n`, `\r\n`, or `\r`.
|
|
670
|
+
|
|
671
|
+
## Documentation
|
|
672
|
+
|
|
673
|
+
- [Formatter options](https://mmd-toolkit.github.io/mmd-toolkit/formatter/options.html):
|
|
674
|
+
how the indentation and the line width apply to diagrams.
|
|
675
|
+
- [Formatter compatibility](https://mmd-toolkit.github.io/mmd-toolkit/formatter/compatibility.html):
|
|
676
|
+
rejected diagrams, differences from mermaid.js, and what the formatting
|
|
677
|
+
guarantees.
|
package/lib/cli.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|