Skip to content

Commit

Permalink
Merge pull request #27 from zakhenry/feat/support-mermaid
Browse files Browse the repository at this point in the history
feat(Language Support): Add Mermaid support
  • Loading branch information
zakhenry authored Jul 13, 2019
2 parents b2fa8b0 + e0f9efb commit 47431ab
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ script:
- yarn build
# tests
- yarn test
- yarn readme:check

deploy:
- provider: script
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ npx embedme README.md

Et voilà! Your README.md file will be updated with the content of your source file:

<!-- prettier-ignore -->
This is a *markdown* document with a code block:

```ts
// example.ts

export function helloWorld(name: string): string {
return `Hello ${name}!, how are you today?`;
}

```

As the comment is preserved, you can happily re-run `embedme` and it will run again but there will be no changes.
Expand Down Expand Up @@ -78,14 +79,14 @@ Here's a list of file types supported by this utility, if you have a need for an
contribute, it is easy!

```ts
// src/embedme.lib.ts#L44-L71
// src/embedme.lib.ts#L44-L73

enum SupportedFileType {
PLAIN_TEXT = 'txt',
TYPESCRIPT = 'ts',
JAVASCRIPT = 'js',
SCSS = 'scss',
RUST = 'rs',
RUST = 'rust',
JAVA = 'java',
CPP = 'cpp',
C = 'c',
Expand All @@ -107,6 +108,8 @@ enum SupportedFileType {
KOTLIN = 'kotlin',
SCALA = 'scala',
CRYSTAL = 'cr',
PLANT_UML = 'puml',
MERMAID = 'mermaid',
}
```

Expand Down
42 changes: 18 additions & 24 deletions src/embedme.lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ enum SupportedFileType {
SCALA = 'scala',
CRYSTAL = 'cr',
PLANT_UML = 'puml',
MERMAID = 'mermaid',
}

enum CommentFamily {
Expand All @@ -77,6 +78,7 @@ enum CommentFamily {
XML,
HASH,
SINGLE_QUOTE,
DOUBLE_PERCENT,
}

const languageMap: Record<CommentFamily, SupportedFileType[]> = {
Expand Down Expand Up @@ -109,18 +111,23 @@ const languageMap: Record<CommentFamily, SupportedFileType[]> = {
SupportedFileType.CRYSTAL,
],
[CommentFamily.SINGLE_QUOTE]: [SupportedFileType.PLANT_UML],
[CommentFamily.DOUBLE_PERCENT]: [SupportedFileType.MERMAID],
};

const leadingSymbol = (symbol: string): FilenameFromCommentReader => line => {
const regex = new RegExp(`${symbol}\\s?(\\S*?$)`);

const match = line.match(regex);
if (!match) {
return null;
}

return match[1];
};

const filetypeCommentReaders: Record<CommentFamily, FilenameFromCommentReader> = {
[CommentFamily.NONE]: _ => null,
[CommentFamily.C]: line => {
const match = line.match(/\/\/\s?(\S*?$)/m);
if (!match) {
return null;
}

return match[1];
},
[CommentFamily.C]: leadingSymbol('//'),
[CommentFamily.XML]: line => {
const match = line.match(/<!--\s*?(\S*?)\s*?-->/);
if (!match) {
Expand All @@ -129,22 +136,9 @@ const filetypeCommentReaders: Record<CommentFamily, FilenameFromCommentReader> =

return match[1];
},
[CommentFamily.HASH]: line => {
const match = line.match(/#\s*?(\S*?)$/);
if (!match) {
return null;
}

return match[1];
},
[CommentFamily.SINGLE_QUOTE]: line => {
const match = line.match(/'\s*?(\S*?)$/);
if (!match) {
return null;
}

return match[1];
},
[CommentFamily.HASH]: leadingSymbol('#'),
[CommentFamily.SINGLE_QUOTE]: leadingSymbol('//'),
[CommentFamily.DOUBLE_PERCENT]: leadingSymbol('%%'),
};

function lookupLanguageCommentFamily(fileType: SupportedFileType): CommentFamily | null {
Expand Down
17 changes: 17 additions & 0 deletions test/fixture.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,23 @@ return bye
```

Mermaid

```mermaid
%% sample.mermaid
sequenceDiagram
Alice ->> Bob: Hello Bob, how are you?
Bob-->>John: How about you John?
Bob--x Alice: I am good thanks!
Bob-x John: I am good thanks!
Note right of John: Bob thinks a long<br/>long time, so long<br/>that the text does<br/>not fit on a row.
Bob-->Alice: Checking with John...
Alice->John: Yes... John, how are you?
```

## Extension-less selection

```sh
Expand Down
9 changes: 9 additions & 0 deletions test/sample.mermaid
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sequenceDiagram
Alice ->> Bob: Hello Bob, how are you?
Bob-->>John: How about you John?
Bob--x Alice: I am good thanks!
Bob-x John: I am good thanks!
Note right of John: Bob thinks a long<br/>long time, so long<br/>that the text does<br/>not fit on a row.

Bob-->Alice: Checking with John...
Alice->John: Yes... John, how are you?

0 comments on commit 47431ab

Please sign in to comment.