Skip to content

Commit 85dedf2

Browse files
committed
Add metadata args to macro function calls
This adds source type, name, and file path as arguments to macro functions, allowing macros to customize their output based on the current source file.
1 parent 933bfc1 commit 85dedf2

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

src/core/markdown/generate-docs.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,6 @@ export type MarkdownGeneratorConfig = Omit<
4747
referenceGuideTemplate: string;
4848
};
4949

50-
function replaceMacros(
51-
unparsedBundles: UnparsedSourceBundle[],
52-
macros: Record<string, MacroFunction> | undefined,
53-
): UnparsedSourceBundle[] {
54-
if (!macros) {
55-
return unparsedBundles;
56-
}
57-
58-
return unparsedBundles.map((bundle) => {
59-
return {
60-
...bundle,
61-
content: Object.entries(macros).reduce((acc, [macroName, macroFunction]) => {
62-
return acc.replace(new RegExp(`{{${macroName}}}`, 'g'), macroFunction());
63-
}, bundle.content),
64-
};
65-
});
66-
}
67-
6850
export function generateDocs(unparsedBundles: UnparsedSourceBundle[], config: MarkdownGeneratorConfig) {
6951
const convertToReferences = apply(parsedFilesToReferenceGuide, config);
7052
const convertToRenderableBundle = apply(parsedFilesToRenderableBundle, config);
@@ -85,12 +67,6 @@ export function generateDocs(unparsedBundles: UnparsedSourceBundle[], config: Ma
8567
return pipe(
8668
TE.right(replaceMacros(unparsedBundles, config.macros)),
8769
TE.flatMap((unparsedBundles) => generateForApex(filterApexSourceFiles(unparsedBundles), config)),
88-
TE.tap((parsedApexFiles) => {
89-
parsedApexFiles.forEach((parsedApexFile) => {
90-
console.log(parsedApexFile.type.docComment);
91-
});
92-
return TE.right(undefined);
93-
}),
9470
TE.chain((parsedApexFiles) => {
9571
return pipe(
9672
reflectCustomFieldsAndObjectsAndMetadataRecords(
@@ -124,6 +100,31 @@ export function generateDocs(unparsedBundles: UnparsedSourceBundle[], config: Ma
124100
);
125101
}
126102

103+
function replaceMacros(
104+
unparsedBundles: UnparsedSourceBundle[],
105+
macros: Record<string, MacroFunction> | undefined,
106+
): UnparsedSourceBundle[] {
107+
if (!macros) {
108+
return unparsedBundles;
109+
}
110+
111+
return unparsedBundles.map((bundle) => {
112+
return {
113+
...bundle,``
114+
content: Object.entries(macros).reduce((acc, [macroName, macroFunction]) => {
115+
return acc.replace(
116+
new RegExp(`{{${macroName}}}`, 'g'),
117+
macroFunction({
118+
type: bundle.type,
119+
name: bundle.name,
120+
filePath: bundle.filePath,
121+
}),
122+
);
123+
}, bundle.content),
124+
};
125+
});
126+
}
127+
127128
function generateForApex(apexBundles: UnparsedApexBundle[], config: MarkdownGeneratorConfig) {
128129
const filterOutOfScope = apply(filterScope, config.scope);
129130
const removeExcluded = apply(removeExcludedTags, config.excludeTags);

src/core/shared/types.d.ts

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

20-
// TODO: Allow macros to either be a string or a function
21-
// TODO: Figure out if we can add arguments (only metadata, like the name of the metadata being parsed, and readonly)
22-
export type MacroFunction = () => string;
20+
type MacroSourceMetadata = {
21+
type: 'apex' | 'customobject' | 'customfield' | 'custommetadata' | 'trigger';
22+
name: string;
23+
filePath: string;
24+
};
25+
26+
export type MacroFunction = (metadata: MacroSourceMetadata) => string;
2327

2428
export type CliConfigurableMarkdownConfig = {
2529
sourceDir: string;

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type {
22
MarkdownConfigurableHooks,
3+
MacroFunction,
4+
MacroSourceMetadata,
35
Skip,
46
UserDefinedMarkdownConfig,
57
ReferenceGuidePageData,
@@ -67,6 +69,8 @@ function defineChangelogConfig(config: ConfigurableChangelogConfig): Partial<Use
6769
export {
6870
defineMarkdownConfig,
6971
ConfigurableMarkdownConfig,
72+
MacroFunction,
73+
MacroSourceMetadata,
7074
defineOpenApiConfig,
7175
ConfigurableOpenApiConfig,
7276
defineChangelogConfig,

0 commit comments

Comments
 (0)