Skip to content

Commit

Permalink
Merge pull request #20 from Mermaid-Chart/feat/use-a-url-id-for-front…
Browse files Browse the repository at this point in the history
…matter

feat(cli)!: use a URL id in the frontmatter
  • Loading branch information
aloisklink authored Apr 22, 2024
2 parents aa9fbd7 + 5602309 commit 0e3299b
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ which points to the diagram on MermaidChart.com:
```mermaid
---
title: My diagram
id: xxxx-xxxxx-xxxxx # this field is created by @mermaidchart/cli
id: https://www.mermaidchart.com/d/xxxx-xxxxx-xxxxx # this field is created by @mermaidchart/cli
---
flowchart
x[My Diagram]
Expand Down
18 changes: 11 additions & 7 deletions packages/cli/src/commander.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ describe('link', () => {
);

await expect(readFile(diagram, { encoding: 'utf8' })).resolves.toContain(
`id: ${mockedEmptyDiagram.documentID}`,
`id: https://test.mermaidchart.invalid/d/${mockedEmptyDiagram.documentID}`,
);
});

Expand Down Expand Up @@ -271,7 +271,7 @@ describe('link', () => {
await Promise.all(
[diagram, diagram2, diagram3].map(async (file) => {
await expect(readFile(file, { encoding: 'utf8' })).resolves.toContain(
`id: ${mockedEmptyDiagram.documentID}`,
`id: https://test.mermaidchart.invalid/d/${mockedEmptyDiagram.documentID}`,
);
}),
);
Expand All @@ -298,8 +298,10 @@ describe('link', () => {

const file = await readFile(unlinkedMarkdownFile, { encoding: 'utf8' });

expect(file).toMatch(`id: ${mockedEmptyDiagram.documentID}\n`);
expect(file).toMatch(`id: second-id\n`);
expect(file).toMatch(
`id: https://test.mermaidchart.invalid/d/${mockedEmptyDiagram.documentID}\n`,
);
expect(file).toMatch(`id: https://test.mermaidchart.invalid/d/second-id\n`);
});

it('should link diagrams in partially linked markdown file', async () => {
Expand All @@ -323,7 +325,7 @@ describe('link', () => {

const file = await readFile(partiallyLinkedMarkdownFile, { encoding: 'utf8' });

expect(file).toMatch(`id: second-id\n`);
expect(file).toMatch(`id: https://test.mermaidchart.invalid/d/second-id\n`);
});

it('should handle unusual markdown formatting', async () => {
Expand All @@ -347,7 +349,7 @@ describe('link', () => {

const file = await readFile(unusualMarkdownFile, { encoding: 'utf8' });

const idLineRegex = /^.*id: my-mocked-diagram-id\n/gm;
const idLineRegex = /^.*id: https:\/\/test.mermaidchart.invalid\/d\/my-mocked-diagram-id\n/gm;

expect(file).toMatch(idLineRegex);
// other than the added `id: xxxx` field, everything else should be identical,
Expand Down Expand Up @@ -412,7 +414,9 @@ title: My cool flowchart
for (const file of [diagram, diagram2]) {
const diagramContents = await readFile(file, { encoding: 'utf8' });

expect(diagramContents).toContain(`id: ${mockedDiagram.documentID}`);
expect(diagramContents).toContain(
`id: https://test.mermaidchart.invalid/d/${mockedDiagram.documentID}`,
);
expect(diagramContents).toContain("flowchart TD\n A[I've been updated!]");
}
});
Expand Down
27 changes: 25 additions & 2 deletions packages/cli/src/frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,37 @@
import * as yaml from 'js-yaml';

const frontMatterRegex = /^-{3}\s*[\n\r](.*?)[\n\r]-{3}\s*[\n\r]+/s;
const urlIDRegex = /(?<baseURL>.*)\/d\/(?<documentID>[\w-]+)/;

type UrlID = `${string}/d/${string}`;

export function getDocumentID(urlID: UrlID, expectedbaseURL: string) {
const match = urlID.match(urlIDRegex);
if (match === null) {
throw new Error(
`Invalid document ID: ${urlID}. Please report this issue to the @mermaidchart/cli maintainers.`,
);
}
// we know that this regex will always return groups on a match
const { baseURL, documentID } = match.groups as { baseURL: string; documentID: string };
if (baseURL !== expectedbaseURL) {
throw new Error(
`Your @mermaidchart/cli is configured to use ${expectedbaseURL}, but your diagram is using ${baseURL}`,
);
}
return documentID;
}
export function createUrlID(baseURL: string, documentID: string): UrlID {
return `${baseURL}/d/${documentID}`;
}

interface FrontMatterMetadata {
title?: string;
// Allows custom display modes. Currently used for compact mode in gantt charts.
displayMode?: string;
config?: Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
/** Unique ID for the diagram on MermaidChart.com */
id?: string;
/** Unique ID for the diagram, e.g. `https://www.mermaidchart.com/d/xxxxxx-xxxx-xxxxx` */
id?: UrlID;
}

export interface FrontMatterResult {
Expand Down
16 changes: 12 additions & 4 deletions packages/cli/src/methods.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import type { MermaidChart } from '@mermaidchart/sdk';

import { CommanderError } from '@commander-js/extra-typings';
import { extractFrontMatter, injectFrontMatter, removeFrontMatterKeys } from './frontmatter.js';
import {
createUrlID,
extractFrontMatter,
getDocumentID,
injectFrontMatter,
removeFrontMatterKeys,
} from './frontmatter.js';

/**
* Cached data to use when pulling/pushing/linking multiple files at once.
Expand Down Expand Up @@ -81,7 +87,9 @@ export async function link(
code: diagram,
});

const diagramWithId = injectFrontMatter(diagram, { id: createdDocument.documentID });
const diagramWithId = injectFrontMatter(diagram, {
id: createUrlID(client.baseURL, createdDocument.documentID),
});

return diagramWithId;
}
Expand All @@ -103,7 +111,7 @@ export async function pull(diagram: string, client: MermaidChart, { title }: Pul
}

const uploadedFile = await client.getDocument({
documentID: frontmatter.metadata.id,
documentID: getDocumentID(frontmatter.metadata.id, client.baseURL),
});

if (uploadedFile.code === undefined) {
Expand All @@ -129,7 +137,7 @@ export async function push(diagram: string, client: MermaidChart, { title }: Pus

// TODO: check if file has changed since last push and print a warning
const existingDiagram = await client.getDocument({
documentID: frontmatter.metadata.id,
documentID: getDocumentID(frontmatter.metadata.id, client.baseURL),
});

// due to MC-1056, try to remove YAML frontmatter if we can
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/fixtures/connected-diagram.mmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: My cool flowchart
id: my-test-document-id
id: https://test.mermaidchart.invalid/d/my-test-document-id
---
flowchart TD
A[Needs Update]
4 changes: 2 additions & 2 deletions packages/cli/test/fixtures/linked-markdown-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is a flowchart diagram

```mermaid
---
id: xxxxxxx-flowchart
id: https://test.mermaidchart.invalid/d/xxxxxxx-flowchart
---
flowchart
A[Hello World]
Expand All @@ -16,7 +16,7 @@ While this is a pie diagram

```mermaid
---
id: xxxxxxx-pie
id: https://test.mermaidchart.invalid/d/xxxxxxx-pie
---
pie
"Flowchart" : 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is a journey diagram that is already linked.

```mermaid
---
id: xxxxxxx-journey
id: https://test.mermaidchart.invalid/d/xxxxxxx-journey
---
journey
title This is a linked journey diagram.
Expand Down

0 comments on commit 0e3299b

Please sign in to comment.