Skip to content

Commit

Permalink
Merge pull request #108 from cesarParra/mermaid-support
Browse files Browse the repository at this point in the history
Mermaid support.
  • Loading branch information
cesarParra authored Feb 25, 2024
2 parents eec1437 + d2cc753 commit 963498d
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 5 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,31 @@ To fix this issue, when not sanitizing HTML, you should wrap any code that conta
treated as HTML within '\`'
or within `<code>` tags.

### Displaying diagrams

You can display diagrams in your documentation by leveraging Github's built-in [Mermaid](https://mermaid-js.github.io/mermaid/#/) support.

If you are using a markdown generator that supports Mermaid (e.g. [Github's markdown](https://github.blog/2022-02-14-include-diagrams-markdown-files-mermaid/),
you can add diagrams to your documentation by using the
`@mermaid` tag.

**Example**

```apex
/**
* @mermaid
* graph TD
* A[Christmas] -->|Get money| B(Go shopping)
* B --> C{Let me think}
* C -->|One| D[Laptop]
* C -->|Two| E[iPhone]
* C -->|Three| F[Car]
*/
public class MyClass {
}
```

[Here's an example of how that looks](/docs/types/Classes/nspc.AnotherInterface.md)

### Ignoring files and members

Expand Down
15 changes: 15 additions & 0 deletions docs/types/Classes/nspc.AnotherInterface.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,18 @@ Some desc

**Group** Classes


```mermaid
sequenceDiagram
participant dotcom
participant iframe
participant viewscreen
dotcom->>iframe: loads html w/ iframe url
iframe->>viewscreen: request template
viewscreen->>iframe: html & javascript
iframe->>dotcom: iframe ready
dotcom->>iframe: set mermaid data on iframe
iframe->>iframe: render mermaid
```


15 changes: 15 additions & 0 deletions docs/types/Classes/nspc.ChildClass.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ This method was overridden.
|---|---|
|`String`|A String.|


```mermaid
sequenceDiagram
participant dotcom
participant iframe
participant viewscreen
dotcom->>iframe: loads html w/ iframe url
iframe->>viewscreen: request template
viewscreen->>iframe: html & javascript
iframe->>dotcom: iframe ready
dotcom->>iframe: set mermaid data on iframe
iframe->>iframe: render mermaid
```


### `public void execute()`

Executes the command.
Expand Down
11 changes: 11 additions & 0 deletions examples/force-app/main/default/classes/AnotherInterface.cls
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
/**
* @description Some desc
* @group Classes
* @mermaid
* sequenceDiagram
* participant dotcom
* participant iframe
* participant viewscreen
* dotcom->>iframe: loads html w/ iframe url
* iframe->>viewscreen: request template
* viewscreen->>iframe: html & javascript
* iframe->>dotcom: iframe ready
* dotcom->>iframe: set mermaid data on iframe
* iframe->>iframe: render mermaid
*/
public interface AnotherInterface{}
11 changes: 11 additions & 0 deletions examples/force-app/main/default/classes/ChildClass.cls
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ public class ChildClass extends ParentClass implements SampleInterface {
/**
* @description This method was overridden.
* @return A String.
* @mermaid
* sequenceDiagram
* participant dotcom
* participant iframe
* participant viewscreen
* dotcom->>iframe: loads html w/ iframe url
* iframe->>viewscreen: request template
* viewscreen->>iframe: html & javascript
* iframe->>dotcom: iframe ready
* dotcom->>iframe: set mermaid data on iframe
* iframe->>iframe: render mermaid
*/
public override String overridableMethodOverridden() {
return null ?? '';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cparra/apexdocs",
"version": "2.21.0",
"version": "2.22.0",
"description": "Library with CLI capabilities to generate documentation for Salesforce Apex classes.",
"keywords": [
"apex",
Expand Down
4 changes: 2 additions & 2 deletions src/model/markdown-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export class MarkdownFile extends OutputFile {
this.addText(`\{@link ${text}\}`, encodeHtml);
}

startCodeBlock() {
this.addText('```apex');
startCodeBlock(language = 'apex') {
this.addText(`\`\`\`${language}`);
}

endCodeBlock() {
Expand Down
16 changes: 16 additions & 0 deletions src/model/markdown-generation-util/doc-comment-annotation-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,25 @@ interface DocCommentAware {
docComment?: DocComment;
}

export function addMermaid(markdownFile: MarkdownFile, docCommentAware: DocCommentAware) {
const mermaid = docCommentAware.docComment?.annotations.find((annotation) => annotation.name === 'mermaid');
if (!mermaid) {
return;
}

markdownFile.addBlankLine();
markdownFile.startCodeBlock('mermaid');
mermaid.bodyLines.forEach((line) => {
markdownFile.addText(line, false);
});
markdownFile.endCodeBlock();
markdownFile.addBlankLine();
}

export function addCustomDocCommentAnnotations(markdownFile: MarkdownFile, docCommentAware: DocCommentAware) {
docCommentAware.docComment?.annotations
.filter((currentAnnotation: DocCommentAnnotation) => currentAnnotation.name !== 'description')
.filter((currentAnnotation: DocCommentAnnotation) => currentAnnotation.name !== 'mermaid')
.forEach((currentAnnotation: DocCommentAnnotation) => {
markdownFile.addBlankLine();
markdownFile.addText(buildDocAnnotationText(currentAnnotation));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ConstructorMirror, DocComment } from '@cparra/apex-reflection';
import { MarkdownFile } from '../markdown-file';
import { ParameterMirror } from '@cparra/apex-reflection';
import { addCustomDocCommentAnnotations } from './doc-comment-annotation-util';
import { addCustomDocCommentAnnotations, addMermaid } from './doc-comment-annotation-util';
import { MethodMirrorWithInheritance } from '../inheritance';

export function declareMethod(
Expand Down Expand Up @@ -55,6 +55,8 @@ export function declareMethod(

addCustomDocCommentAnnotations(markdownFile, currentMethod);

addMermaid(markdownFile, currentMethod);

if (currentMethod.docComment?.exampleAnnotation) {
addExample(markdownFile, currentMethod, startingHeadingLevel);
}
Expand Down
4 changes: 3 additions & 1 deletion src/model/markdown-generation-util/type-declaration-util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MarkdownFile } from '../markdown-file';
import { addCustomDocCommentAnnotations } from './doc-comment-annotation-util';
import { addCustomDocCommentAnnotations, addMermaid } from './doc-comment-annotation-util';
import { Annotation, ClassMirror, InterfaceMirror, Type } from '@cparra/apex-reflection';
import { TypesRepository } from '../types-repository';

Expand All @@ -26,6 +26,8 @@ export function declareType(markdownFile: MarkdownFile, typeMirror: Type): void
}

addCustomDocCommentAnnotations(markdownFile, typeMirror);

addMermaid(markdownFile, typeMirror);
}

function addInheritanceSectionForClass(typeMirror: Type, markdownFile: MarkdownFile) {
Expand Down

0 comments on commit 963498d

Please sign in to comment.