Skip to content

Commit 1c300c0

Browse files
committed
Add macros feature to Markdown documentation generation
1 parent 85dedf2 commit 1c300c0

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

README.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,62 @@ There are hooks for both Markdown and Changelog operations (but not for OpenApi)
351351

352352
#### Markdown Hooks
353353

354+
##### **macros**
355+
356+
Allows defining custom macros that can be used in the documentation.
357+
358+
Macros are reusable pieces of text that can be injected into the documentation,
359+
allowing you to define common pieces of text that you can use across multiple files.
360+
361+
A common use case is injecting copyright or license information, without
362+
having to copy-paste the same text across multiple classes, polluting your
363+
source code.
364+
365+
A macro can be defined in your documentation using the `{{macro_name}}` syntax.
366+
In the configuration file, you can then define the macro behavior as a key-value pair, where the key is the name of the macro, and the value is a function that returns the text to inject in place of the macro.
367+
368+
**Type**
369+
370+
```typescript
371+
type MacroSourceMetadata = {
372+
type: 'apex' | 'customobject' | 'customfield' | 'custommetadata' | 'trigger';
373+
name: string;
374+
filePath: string;
375+
};
376+
377+
type MacroFunction = (metadata: MacroSourceMetadata) => string;
378+
```
379+
380+
Notice that the `metadata` object contains information about the source of the file for which the macro is being injected. This allows you to optionally
381+
return different text based on the source of the file.
382+
383+
Example: Injecting a copyright notice
384+
385+
```typescript
386+
//...
387+
macros: {
388+
copyright: () => {
389+
return `Copyright (c) ${new Date().getFullYear()} My Name`;
390+
}
391+
}
392+
//...
393+
```
394+
395+
And then in your source code, you can use the macro like this:
396+
397+
```apex
398+
/**
399+
* {{copyright}}
400+
* @description This is a class
401+
*/
402+
public class MyClass {
403+
//...
404+
}
405+
```
406+
354407
##### **transformReferenceGuide**
355408

356-
Allows changing the Allows changing the frontmatter and content of the reference guide, or even if creating a reference
357-
guide page should be skipped.
409+
Allows changing the frontmatter and content of the reference guide, or if creating a reference guide page altogether should be skipped.
358410

359411
**Type**
360412

src/core/markdown/generate-docs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function replaceMacros(
110110

111111
return unparsedBundles.map((bundle) => {
112112
return {
113-
...bundle,``
113+
...bundle,
114114
content: Object.entries(macros).reduce((acc, [macroName, macroFunction]) => {
115115
return acc.replace(
116116
new RegExp(`{{${macroName}}}`, 'g'),

src/core/shared/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type LinkingStrategy =
1717
// No logic will be applied, the reference path will be used as is.
1818
| 'none';
1919

20-
type MacroSourceMetadata = {
20+
export type MacroSourceMetadata = {
2121
type: 'apex' | 'customobject' | 'customfield' | 'custommetadata' | 'trigger';
2222
name: string;
2323
filePath: string;

0 commit comments

Comments
 (0)