Skip to content

Commit 1ee1ab1

Browse files
committedMar 6, 2024
feat(cli): support GFM (github flavored markdown)
[remark][1] by default only supports [CommonMark][2], however, with [remark-gfm][3] and [remark-frontmatter][4] we can support [GFM (GitHub flavoured markdown][5], which is used by GitHub. [1]: https://github.com/remarkjs/remark [2]: https://commonmark.org/ [3]: https://github.com/remarkjs/remark-gfm [4]: https://github.com/remarkjs/remark-frontmatter [5]: https://github.github.com/gfm/
1 parent 093eafc commit 1ee1ab1

File tree

6 files changed

+314
-4
lines changed

6 files changed

+314
-4
lines changed
 

‎packages/cli/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ npx @mermaidchart/cli --help
3030
on https://mermaidchart.com.
3131

3232
These local diagrams can either be stored in `.mmd` or `.mermaid` files, or
33-
the can be stored within ```` ```mermaid```` code blocks within `.md` markdown
34-
files.
33+
they can be stored within ```` ```mermaid```` code blocks within `.md`
34+
[GFM](https://github.github.com/gfm/) markdown files.
3535

3636
### `login`
3737

‎packages/cli/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
"commander": "^11.1.0",
6363
"js-yaml": "^4.1.0",
6464
"remark": "^15.0.1",
65+
"remark-frontmatter": "^5.0.0",
66+
"remark-gfm": "^4.0.0",
6567
"to-vfile": "^8.0.0",
6668
"unist-util-visit": "^5.0.0"
6769
}

‎packages/cli/src/commander.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const LINKED_MARKDOWN_FILE = 'test/fixtures/linked-markdown-file.md';
1717
const PARTIALLY_LINKED_MARKDOWN_FILE = 'test/fixtures/partially-linked-markdown-file.md';
1818
/** Markdown file that has unlinked Mermaid diagrams */
1919
const UNLINKED_MARKDOWN_FILE = 'test/fixtures/unlinked-markdown-file.md';
20+
/** Markdown file that has non-standard Markdown features like YAML frontmatter */
21+
const UNUSUAL_MARKDOWN_FILE = 'test/fixtures/unusual-markdown-file.md';
2022

2123
type Optional<T> = T | undefined;
2224
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -323,6 +325,37 @@ describe('link', () => {
323325

324326
expect(file).toMatch(`id: second-id\n`);
325327
});
328+
329+
it('should handle unusual markdown formatting', async () => {
330+
const unusualMarkdownFile = 'test/output/unusual-markdown-file.md';
331+
await copyFile(UNUSUAL_MARKDOWN_FILE, unusualMarkdownFile);
332+
333+
const { program } = mockedProgram();
334+
335+
vi.mock('@inquirer/confirm');
336+
vi.mock('@inquirer/select');
337+
vi.mocked(confirm).mockResolvedValue(true);
338+
vi.mocked(select).mockResolvedValueOnce(mockedProjects[0].id);
339+
340+
vi.mocked(MermaidChart.prototype.createDocument).mockResolvedValueOnce({
341+
...mockedEmptyDiagram,
342+
documentID: 'my-mocked-diagram-id',
343+
});
344+
await program.parseAsync(['--config', CONFIG_AUTHED, 'link', unusualMarkdownFile], {
345+
from: 'user',
346+
});
347+
348+
const file = await readFile(unusualMarkdownFile, { encoding: 'utf8' });
349+
350+
const idLineRegex = /^.*id: my-mocked-diagram-id\n/gm;
351+
352+
expect(file).toMatch(idLineRegex);
353+
// other than the added `id: xxxx` field, everything else should be identical,
354+
// although in practice, we'd expect some formatting changes
355+
expect(file.replace(idLineRegex, '')).toStrictEqual(
356+
await readFile(UNUSUAL_MARKDOWN_FILE, { encoding: 'utf8' }),
357+
);
358+
});
326359
});
327360

328361
describe('pull', () => {

‎packages/cli/src/remark.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type { MermaidChart } from '@mermaidchart/sdk';
55
import { type LinkOptions, link, pull, push } from './methods.js';
66

77
import { remark } from 'remark';
8+
import remarkFrontmatter from 'remark-frontmatter';
9+
import remarkGfm from 'remark-gfm';
810
import { read, write } from 'to-vfile';
911
interface MCPluginCommonOptions {
1012
/** Authenticated client */
@@ -87,7 +89,11 @@ export function plugin({ client, ...options }: MCPluginOptions) {
8789
*/
8890
export async function processMarkdown(inputFile: string, options: MCPluginOptions) {
8991
const inVFile = await read(inputFile);
90-
const outVFile = await remark().use(plugin, options).process(inVFile);
92+
const outVFile = await remark()
93+
.use(remarkFrontmatter)
94+
.use(remarkGfm)
95+
.use(plugin, options)
96+
.process(inVFile);
9197

9298
switch (options.command) {
9399
case 'link':
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
description: |
3+
Here is a YAML frontmatter, supported by GitHub (by not GFM!!), Docusaurus,
4+
Vitepress, etc.
5+
---
6+
7+
# This is an unusual markdown file with non-standard syntax
8+
9+
See <https://github.github.com/gfm> for some of GitHub's
10+
GitHub Flavored Markdown extensions.
11+
12+
Here is a markdown comment: <!-- Hello World -->
13+
14+
This is my text with a footnote.[^1]
15+
16+
[^1]: This is my footnote.
17+
18+
This is a ~~strikethrough~~.
19+
20+
Here is some math (not part of GFM): $ x=5 + 1 $
21+
22+
Here is some more math:
23+
24+
```math
25+
\sum_{n=1}^{\infty} x = \infty
26+
```
27+
28+
## Here is a table
29+
30+
| foo | bar |
31+
| --- | --- |
32+
| baz | bim |
33+
34+
## Here is my Mermaid diagram in a quote in a list
35+
36+
* This is my list
37+
* This is my list
38+
1. This is another list entry.
39+
2. And another one.
40+
3. This is my entry with the diagram:
41+
> Here is my quote with the diagram:
42+
>
43+
> ```mermaid
44+
> ---
45+
> title: This is my diagram title
46+
> ---
47+
> flowchart
48+
> A[Hello World]
49+
> ```

0 commit comments

Comments
 (0)
Failed to load comments.