forked from hiero-ledger/solo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsdoc-plugin-macros.mjs
More file actions
63 lines (58 loc) · 2.14 KB
/
tsdoc-plugin-macros.mjs
File metadata and controls
63 lines (58 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// SPDX-License-Identifier: Apache-2.0
// eslint-disable-next-line n/no-unpublished-import
import {Converter} from 'typedoc';
/**
* Dictionary of macros. Define reusable text blocks here.
*/
const MACROS = {
DNS_1123_LABEL:
'A valid RFC-1123 DNS label consists of the following:\n' +
'- The first character must be a-z or 0-9\n' +
'- The middle part can contain a-z, 0-9, or -\n' +
'- The last character must be a-z or 0-9\n' +
'- The total length must not exceed 63 characters\n',
};
/**
* TypeDoc Plugin: Macro Processor for TSDoc
*/
export function load(app) {
app.converter.on(Converter.EVENT_RESOLVE, (context, reflection) => {
if (reflection.comment) {
replaceMacrosInComment(reflection, app);
}
});
}
/**
* Replaces @include references with predefined text.
*/
function replaceMacrosInComment(reflection, app) {
if (reflection.comment.blockTags) {
for (let i = 0; i < reflection.comment.blockTags.length; i++) {
if (reflection.comment.blockTags[i].tag === '@include') {
const replacementText = replaceMacros(reflection.comment.blockTags[i].content[0].text, reflection, app);
if (replacementText) {
reflection.comment.blockTags[i].content[0].text = replacementText;
}
console.log(' reflection.comment.summary[0]: ', reflection.comment.summary[0]);
if (!reflection.comment.summary) {
reflection.comment.summary = [];
}
if (!reflection.comment.summary[0]) {
reflection.comment.summary.push({kind: 'text', text: ''});
}
reflection.comment.summary[0].text = reflection.comment.summary[0]?.text
? `${reflection.comment.summary[0].text}\n${reflection.comment.blockTags[i].content[0].text}`
: `${reflection.comment.blockTags[i].content[0].text}`;
reflection.comment.blockTags.splice(i, 1);
i--;
}
}
}
}
function replaceMacros(macroName, reflection, app) {
if (MACROS[macroName]) {
return MACROS[macroName];
}
app.logger.error(`Unknown macro: ${macroName}, reflection.sources: ${JSON.stringify(reflection.sources, null, 2)}`);
return `/* Unknown macro: ${macroName} */`;
}