-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
1056 lines (917 loc) · 33.7 KB
/
index.ts
File metadata and controls
1056 lines (917 loc) · 33.7 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const path = require('path');
const fs = require('fs');
const nodeFetch = require('node-fetch');
enum TypeDocReflectionKind {
Project = 'Project',
Module = 'Module',
Namespace = 'Namespace',
Enumeration = 'Enumeration',
EnumerationMember = 'Enumeration member',
Variable = 'Variable',
Function = 'Function',
Class = 'Class',
Interface = 'Interface',
Constructor = 'Constructor',
Property = 'Property',
Method = 'Method',
CallSignature = 'Call signature',
IndexSignature = 'Index signature',
ConstructorSignature = 'Constructor signature',
Parameter = 'Parameter',
TypeLiteral = 'Type literal',
TypeParameter = 'TypeParameter',
Accessor = 'Accessor',
GetSignature = 'GetSignature',
SetSignature = 'SetSignature',
ObjectLiteral = 'ObjectLiteral',
TypeAlias = 'Type alias',
Reference = 'Reference',
}
interface TypeDocCommentTag {
tag: string;
text: string;
}
interface TypeDocSource {
fileName: string;
line: number;
character: number;
}
interface TypeDocNode {
id: number;
name: string;
kind: number;
kindString: TypeDocReflectionKind;
sources?: TypeDocSource[];
children?: TypeDocNode[];
groups?: TypeDocGroup[];
comment?: TypeDocComment;
flags?: any;
}
interface TypeDocComment {
shortText?: string;
text?: string;
returns?: string;
tags?: TypeDocCommentTag[];
}
interface TypeDocGroup {
title: string;
kind: number;
children: number[];
}
interface EnumerationMemberNode extends TypeDocNode {
kindString: TypeDocReflectionKind.EnumerationMember;
defaultValue: string;
}
interface ConstructorNode extends TypeDocNode {
kindString: TypeDocReflectionKind.EnumerationMember;
signatures: ConstructorSignatureNode[];
}
interface TypeDocType {
type:
| 'reference'
| 'union'
| 'intrinsic'
| 'reflection'
| 'array'
| 'literal';
id?: number;
name?: string;
types?: TypeDocType[];
declaration?: TypeLiteralNode;
typeArguments?: TypeDocType[];
elementType?: TypeDocType;
value?: string;
}
interface ConstructorSignatureNode extends TypeDocNode {
kindString: TypeDocReflectionKind.ConstructorSignature;
parameters: ParameterNode[];
type: TypeDocType;
overwrites: TypeDocType;
}
interface SignatureNode extends TypeDocNode {
// Call signature, Method signature, Constructor signature
parameters?: ParameterNode[];
type: TypeDocType;
overwrites?: TypeDocType;
inheritedFrom?: TypeDocType;
}
interface FunctionNode extends TypeDocNode {
signatures: SignatureNode[];
overwrites?: TypeDocType;
inheritedFrom?: TypeDocType;
}
interface ParameterNode extends TypeDocNode {
type: TypeDocType;
defaultValue?: string;
}
interface TypeLiteralNode extends TypeDocNode {
signatures?: SignatureNode[];
indexSignature?: SignatureNode;
}
interface TypeAliasNode extends TypeDocNode {
type: TypeDocType;
}
interface TypeDocLinkingNode extends TypeDocNode {
parentId?: number;
signatures?: SignatureNode[];
}
const encodePageId = (pageId: string) => {
// making pageId's great again
return pageId.replace(/ /g, '%20');
};
// eslint-disable-next-line no-underscore-dangle
const _indent = (
content: string,
indentDelim: string,
level: number,
): string => {
if (!content || !indentDelim || !level) return content;
const lines = content.split('\n');
const indentChar = indentDelim.repeat(level);
const updateLines = lines.map((line) => {
if (line) return `${indentChar}\n${line}`;
return line;
});
return updateLines.join('\n');
};
// All the parse functions are to be used internally (its used to get sub content)
class TypeDocInternalParser {
static convertToItalic = (name: string | undefined) => (name ? `_${name}_` : '');
static convertNameToLink: (node: string | undefined, includeParent?: boolean) => string;
static GITHUB_LINK = 'https://github.com/thoughtspot/visual-embed-sdk/blob/main/src';
static covertTypeDocText = (text: string) => {
// 1) Convert Markdown links -> AsciiDoc links
const updated = text.replace(
/\[([^\]]+)\]\((https?:\/\/[^)]+)\)/g,
'link:$2[$1]',
);
// 2) Existing logic: convert {@link Name.hash} -> xref:...
const matches = updated.match(/{@link\s[^{]+}/g);
if (!matches) return updated;
return matches.reduce((prevUpdatedText, curLinkText) => {
const linkTo = curLinkText.split(/\s/)[1].replace(/}/g, '');
const newLinkText = this.convertNameToLink(linkTo, true);
if (!newLinkText) return prevUpdatedText;
return prevUpdatedText.replace(curLinkText, newLinkText);
}, updated);
};
// function to parse a tag
static parseTag(tag: TypeDocCommentTag): string {
if (!tag.tag.trim() || !tag.text.trim()) {
console.log('\t', 'Tag skipped ', JSON.stringify(tag));
return '';
}
if (tag.tag === 'group') return '';
if (tag.tag === 'version')
return `[version]#Version : ${tag.text.replace(/\n/g, '')}#\n`;
if (tag.tag === 'example') return `${tag.text}\n`;
if (tag.tag === 'param') {
return `\nParameter::\n${this.covertTypeDocText(tag.text)}\n`;
}
if (tag.tag === 'deprecated') {
return `[deprecated]#Deprecated : ${tag.text.replace(
/\n/g,
'',
)}#\n`;
}
if (tag.tag === 'tryItOut') {
return `++++\n<a href="{{previewPrefix}}${tag.text}" id="preview-in-playground" target="_blank">Try it out</a>\n++++\n`;
}
return `\n\`${tag.tag}\` : ${this.covertTypeDocText(tag.text)} \n`;
}
static parseComment(comment: TypeDocComment | undefined): string {
if (!comment) return '';
let content = '';
const shortText = comment?.shortText?.trim() || '';
const text = comment?.text?.trim() || '';
content += this.covertTypeDocText(
`${shortText}\n\n${text}\n\n`,
);
return content;
}
static parseTags(tags: TypeDocCommentTag[] | undefined) {
// process tags
let content = '';
const addTagsToContent = (tagsToAdd: TypeDocCommentTag[]) => {
tagsToAdd.forEach((tag) => {
content += `\n${this.parseTag(tag)}\n\n`;
});
};
if (tags) {
const versionTags = tags.filter((tag) => tag.tag === 'version');
const deprecatedTags = tags.filter(
(tag) => tag.tag === 'deprecated',
);
const restTags = tags.filter(
(tag) => tag.tag !== 'version' && tag.tag !== 'deprecated',
);
addTagsToContent(versionTags);
addTagsToContent(deprecatedTags);
addTagsToContent(restTags);
}
return content;
}
static parseSources = (sources: TypeDocSource[] | undefined) => {
if (!sources) return '';
return sources
.map(
(source) =>
`[definedInTag]#Defined in : link:${this.GITHUB_LINK}/${source.fileName}#L${source.line}[${source.fileName}, window=_blank]#`,
)
.join('\n');
};
// TODO : better handling for typeArg
static parseTypeDocType = (
node: TypeDocType | undefined,
link = false,
): string => {
let typeArg = '';
if (!node) return '';
if (node.typeArguments && node.typeArguments.length) {
typeArg = `< ${node.typeArguments
?.map((type) => this.parseTypeDocType(type, link))
.join(', ')} >`;
}
switch (node.type) {
case 'literal':
if (link) return this.convertToItalic(node.value);
return `"${node.value}"`;
case 'intrinsic':
if (link)
return this.convertToItalic(node.name) + typeArg || '';
return node.name + typeArg || '';
case 'reference': {
// since code block doesn't support links
if (link) {
return this.convertNameToLink(node.name) + typeArg;
}
return node.name + typeArg || '';
}
case 'union': {
return (
node.types
?.map((type) => this.parseTypeDocType(type, link))
.join(' | ') + typeArg || ''
);
}
case 'reflection': {
return (
this.parseTypeLiteralNode(node.declaration, link) + typeArg
);
}
case 'array': {
return `${
this.parseTypeDocType(node.elementType, link) + typeArg
}[]`;
}
default: {
console.error(`${node.type} not handled`);
return node.name || '';
}
}
};
// handles both call and constructor signature
static parseCallSignature = (node: SignatureNode, link?: boolean) => {
return `(${this.parseParameters(
node.parameters,
link,
)}) : ${this.parseTypeDocType(node.type, link)}`;
};
static parseIndexSignatures = (node: SignatureNode, link?: boolean) => {
return `{[${this.parseParameters(
node.parameters,
link,
)}] : ${this.parseTypeDocType(node.type, link)}}`;
};
static parseTypeLiteralNode = (
node: TypeLiteralNode | undefined,
link?: boolean,
) => {
// 3 types
if (!node) return '';
if (node.indexSignature) {
return this.parseIndexSignatures(node.indexSignature, link);
}
if (node.signatures) {
return node.signatures
.map((sig) => this.parseCallSignature(sig, link))
.join('\n\n');
}
if (node.children) {
return `{${this.parseParameters(
node.children as ParameterNode[],
link,
)}}`;
}
console.error(
`No handler defined for : ${node.kindString}, Name : ${node.name}`,
);
return '';
};
static parseParameters = (
parameters: ParameterNode[] | undefined,
link?: boolean,
) => {
if (!parameters) return '';
return parameters
.map((param) => {
const isOptional =
param.defaultValue !== undefined || param.flags?.isOptional
? '?'
: '';
const defaultValue =
param.defaultValue !== undefined
? `= ${param.defaultValue}`
: '';
return `${param.name}${isOptional}: ${this.parseTypeDocType(
param.type,
link,
)} ${defaultValue}`;
})
.join(', ');
};
}
// To get the main content use the handleNode function
class TypeDocParser {
private childrenIdMap: Record<number, TypeDocLinkingNode> = {};
private childrenNameMap: Record<string, TypeDocLinkingNode> = {};
private groupMap: Record<string, TypeDocLinkingNode[]> = {};
public convertNameToLink = (
linkTo: string | undefined,
includeParent = false,
) => {
if (!linkTo) return '';
const [name, hash] = linkTo.split('.');
const nameNode = this.childrenNameMap[name || ''];
if (hash) {
const hashNode = nameNode?.children?.filter(
(node) => node.name === hash,
)[0];
if (hashNode)
return this.convertNodeToLink(hashNode, includeParent);
}
if (nameNode) return this.convertNodeToLink(nameNode, includeParent);
// could not be resolved, so passing back the name to be displayed.
if (hash && includeParent) return `${name}.${hash}`;
return hash || name;
};
private parseTypeDocType = (
node: TypeDocType | undefined,
link = false,
): string => {
let typeArg = '';
if (!node) return '';
if (node.typeArguments && node.typeArguments.length) {
typeArg = `< ${node.typeArguments
?.map((type) => this.parseTypeDocType(type, link))
.join(', ')} >`;
}
switch (node.type) {
case 'literal':
if (link)
return TypeDocInternalParser.convertToItalic(node.value);
return `"${node.value}"`;
case 'intrinsic':
if (link)
return (
TypeDocInternalParser.convertToItalic(node.name) +
typeArg || ''
);
return node.name + typeArg || '';
case 'reference': {
// since code block doesn't support links
if (link) {
const nodeToLink = this.childrenNameMap[node.name || ''];
if (nodeToLink) {
return this.convertNodeToLink(nodeToLink);
}
}
return node.name + typeArg || '';
}
case 'union': {
return (
node.types
?.map((type) => this.parseTypeDocType(type, link))
.join(' | ') + typeArg || ''
);
}
case 'reflection': {
return (
TypeDocInternalParser.parseTypeLiteralNode(
node.declaration,
link,
) + typeArg
);
}
case 'array': {
return `${
this.parseTypeDocType(node.elementType, link) + typeArg
}[]`;
}
default: {
console.error(`${node.type} not handled`);
return node.name || '';
}
}
};
private getHeadingString = (options: {
toc?: boolean;
tocLevel?: number;
title: string;
pageId: string;
description?: string;
}) => {
const {
toc = true,
tocLevel = 2,
title,
pageId,
description = '',
} = options;
return [
`:toc: ${toc}`,
`:toclevels: ${tocLevel}`,
`:page-title: ${title}`,
`:page-pageid: ${pageId}`,
`:page-description: ${description.replace(/\n/g, ' ')}`,
].join('\n');
};
private createTypeDocTable = (
data: string[],
noOfColumns: number,
): string => {
let content = `[cols="${'1,'.repeat(noOfColumns - 1)}1"]\n|===\n`;
data.forEach((str) => {
content += `| ${str}\n`;
});
for (let i = 0; i < noOfColumns - 1; i++) {
content += '| \n';
}
content += '|===\n';
return content;
};
private generateMap = (node: TypeDocLinkingNode) => {
const groupTag =
node.comment?.tags?.filter((e) => e.tag === 'group')[0] ||
node?.signatures?.[0].comment?.tags?.filter(
(e) => e.tag === 'group',
)[0];
if (groupTag) {
const groupName = groupTag.text.trim();
if (!this.groupMap[groupName]) this.groupMap[groupName] = [];
this.groupMap[groupName].push(node);
}
if (!this.childrenIdMap[node.id]) this.childrenIdMap[node.id] = node;
if (!this.childrenNameMap[node.name])
this.childrenNameMap[node.name] = node;
node?.children?.forEach((childNode) => {
const child = childNode as TypeDocLinkingNode;
child.parentId = node.id;
this.childrenIdMap[child.id] = child;
this.childrenNameMap[child.name] = child;
this.generateMap(child);
});
};
public getTypeCSSClass = (node) => {
return `typedoc-${node.kindString.replace(/ /g, '_')}`;
};
private convertToAdocSectionLink = (str: string) => {
// special handling for -- in the name
let convertedStr = `_${str.replace(/--/g, '')}`;
// convert all - to _
convertedStr = convertedStr.replace(/-/g, '_');
// convert multiple _ to single _
convertedStr = convertedStr.replace(/_+/g, '_');
// remove all special characters
convertedStr = convertedStr.replace(/[^a-zA-Z0-9_]/g, '');
// convert all spaces to -
convertedStr = convertedStr.replace(/ /g, '-');
// convert all uppercase to lowercase
convertedStr = convertedStr.toLowerCase();
return convertedStr;
};
private convertNodeToLink = (node: TypeDocNode, includeParent = false) => {
const parent = this.childrenIdMap[node.id]?.parentId;
if (parent === undefined) return node.name;
if (
this.childrenIdMap[parent]?.kindString ===
TypeDocReflectionKind.Project
) {
return `[.typedoc-${node.kindString.replace(/ /g, '_')}]#xref:${
node.name
}.adoc[${node.name}]#`;
}
const grandParent = this.childrenIdMap[parent]?.parentId;
if (grandParent === undefined) return node.name;
if (
this.childrenIdMap[grandParent]?.kindString ===
TypeDocReflectionKind.Project
) {
let newLinkText = `[.typedoc-${node.kindString.replace(
/ /g,
'_',
)}]#xref:${this.childrenIdMap[parent].name}.adoc`;
newLinkText += `#${this.convertToAdocSectionLink(node.name)}`;
const visibleName = includeParent
? `${this.childrenIdMap[parent].name}.${node.name}`
: node.name;
newLinkText += `[${visibleName}]#`;
return newLinkText;
}
return '';
};
// handles the Main page nodes (Enum, Class, Interface, Type Alias)
private handleMainNode = (node: TypeDocNode) => {
const pageTitle = `= ${node.name}`;
const mainPageContent = '';
let enumIndexContent = '\n\n[div boxDiv boxFullWidth]\n--\n';
// Special handling
if (['Enumeration', 'Class', 'Interface'].includes(node.kindString)) {
enumIndexContent += `${this.createTypeDocTable(
node.children?.map((childNode) =>
this.convertNodeToLink(childNode),
) || [],
3,
)}`;
}
enumIndexContent += '\n--\n\n';
// Crate content for children ( Enum members , Parameters, Properties, etc..)
const groupContent = node.groups
?.map((group) => {
const groupHeading = `== ${group.title}`;
return [
groupHeading,
...group.children.map((id) => {
return this.convertTypeDocNode(this.childrenIdMap[id]);
}),
].join('\n\n');
})
.join('\n\n');
return [
pageTitle,
TypeDocInternalParser.parseComment(node.comment),
TypeDocInternalParser.parseTags(node.comment?.tags),
'== Index',
enumIndexContent,
mainPageContent,
groupContent,
].join('\n\n');
};
private handleEnumMember = (enumMember: EnumerationMemberNode) => {
// debugger;
const sourceContent = TypeDocInternalParser.parseSources(
enumMember.sources,
);
return [
`=== ${enumMember.name}`,
'[div typeDocBlock boxFullWidth]\n--',
`\`${enumMember.name}:= ${enumMember.defaultValue}\`\n`,
TypeDocInternalParser.parseComment(enumMember.comment),
sourceContent,
TypeDocInternalParser.parseTags(enumMember.comment?.tags),
'--',
].join('\n');
};
private handleFunctionNode = (node: FunctionNode) => {
const name = node.name !== 'constructor' ? `=== ${node.name}` : '';
let overwrites = '';
if (node.overwrites) {
overwrites = `[definedInTag]#Overrides ${this.parseTypeDocType(
node.overwrites,
)}#`;
}
let inheritedFrom = '';
if (node.inheritedFrom) {
inheritedFrom = `[definedInTag]#Inherited from ${this.parseTypeDocType(
node.inheritedFrom,
)}#`;
}
const signatureContent = this.handleCallSignatureNodes(node.signatures);
const sources = TypeDocInternalParser.parseSources(node.sources);
return [
name,
TypeDocInternalParser.parseComment(node.comment),
'[div typeDocBlock boxFullWidth]\n--',
signatureContent,
sources,
overwrites,
inheritedFrom,
TypeDocInternalParser.parseTags(node.comment?.tags),
'--',
].join('\n\n');
};
private handleParameterNode = (node: ParameterNode) => {
return [
`${node.name}::: ${node.flags.isOptional ? '_Optional_\n' : ''}`,
`* ${node.name}: ${this.parseTypeDocType(node.type, true)}${
node?.defaultValue ? ` = ${node.defaultValue}` : ''
}`,
TypeDocInternalParser.parseComment(node.comment),
this.handleTypeNode(node.type),
TypeDocInternalParser.parseTags(node.comment?.tags),
].join('\n\n');
};
private handlePropertyNode = (node: ParameterNode) => {
const sig = `\`${node.name}: ${this.parseTypeDocType(node.type, true)}${
node?.defaultValue ? ` = ${node.defaultValue}` : ''
}\``;
return [
`=== ${node.name}`,
'[div typeDocBlock boxFullWidth]\n--',
sig,
// TODO : move this iwth aobve line
node.flags.isOptional ? '_Optional_' : '',
TypeDocInternalParser.parseComment(node.comment),
this.handleTypeNode(node.type),
TypeDocInternalParser.parseTags(node.comment?.tags),
'--\n',
].join('\n\n');
};
private handleCallSignatureNode = (node: SignatureNode) => {
const parmContent = node.parameters
?.map(this.convertTypeDocNode)
.join('\n\n');
return [
TypeDocInternalParser.parseComment(node.comment),
node.parameters?.length ? '**Function Parameters**' : '',
parmContent,
'**Returns**',
TypeDocInternalParser.parseTypeDocType(node.type, true),
// SCAL-182339
// this.handleTypeNode(node.type),
TypeDocInternalParser.parseTags(node.comment?.tags),
].join('\n\n');
};
private handleCallSignatureNodes = (nodes: SignatureNode[]) => {
let content = '';
content += nodes
.map((node) => {
return [
`\`${node.name}${TypeDocInternalParser.parseCallSignature(
node,
false,
)}\``,
this.handleCallSignatureNode(node),
].join('\n\n');
})
.join('\n\n');
return content;
};
private handleTypeLiteralNode = (node: TypeLiteralNode | undefined) => {
if (!node) return '';
let content = '';
if (node.indexSignature?.parameters) {
content += 'Index Signature Parameters\n\n';
content += node.indexSignature.parameters
.map(this.convertTypeDocNode)
.join('\n\n');
} else if (node.signatures) {
node.signatures.forEach((sigNode) => {
content += `\`${TypeDocInternalParser.parseCallSignature(
sigNode,
true,
)}\`\n\n`;
content += `${this.handleCallSignatureNode(sigNode)}\n\n`;
});
} else if (node.children) {
content += 'Parameters\n\n';
content += node.children
.map((child) =>
this.handleParameterNode(child as ParameterNode),
)
.join('\n\n');
}
return content;
};
private handleTypeNode = (node: TypeDocType | undefined) => {
const content = [
TypeDocInternalParser.parseComment(node?.declaration?.comment),
TypeDocInternalParser.parseTags(node?.declaration?.comment?.tags),
this.handleTypeLiteralNode(node?.declaration),
].join('\n\n');
return content;
};
private handleTypeAliasNode = (node: TypeAliasNode) => {
return [
`= ${node.name}`,
`\`${node.name} : ${this.parseTypeDocType(node.type, true)}\``,
TypeDocInternalParser.parseComment(node.comment),
TypeDocInternalParser.parseSources(node.sources),
`${this.handleTypeNode(node.type)}`,
].join('\n\n');
};
private convertTypeDocNode = (
rootNode: TypeDocNode | undefined,
): string => {
if (!rootNode) return '';
switch (rootNode.kindString) {
case TypeDocReflectionKind.Enumeration: {
return this.handleMainNode(rootNode);
}
case TypeDocReflectionKind.Class: {
return this.handleMainNode(rootNode);
}
case TypeDocReflectionKind.Interface: {
return this.handleMainNode(rootNode);
}
case TypeDocReflectionKind.EnumerationMember: {
return this.handleEnumMember(rootNode as EnumerationMemberNode);
}
case TypeDocReflectionKind.Constructor: {
return this.handleFunctionNode(rootNode as FunctionNode);
}
case TypeDocReflectionKind.Method: {
return this.handleFunctionNode(rootNode as FunctionNode);
}
case TypeDocReflectionKind.Function: {
return this.handleFunctionNode(rootNode as FunctionNode);
}
case TypeDocReflectionKind.Parameter: {
return this.handleParameterNode(rootNode as ParameterNode);
}
case TypeDocReflectionKind.Property: {
return this.handlePropertyNode(rootNode as ParameterNode);
}
case TypeDocReflectionKind.TypeAlias: {
return this.handleTypeAliasNode(rootNode as TypeAliasNode);
}
case TypeDocReflectionKind.TypeLiteral: {
return this.handleTypeLiteralNode(rootNode as TypeLiteralNode);
}
case TypeDocReflectionKind.CallSignature: {
return this.handleCallSignatureNode(rootNode as SignatureNode);
}
default: {
console.error(
`No handler defined for : ${rootNode.kindString}, Name : ${rootNode.name}`,
);
return '';
}
}
};
private getPageId = (node: TypeDocNode) => {
return encodePageId(`${node.kindString}_${node.name}`);
};
public handleProjectNode = (
node: TypeDocNode,
indexPageId = 'VisualEmbedSdk',
callBack: (pageId: string, content: string) => void,
) => {
const projectNode = node;
this.generateMap(projectNode);
// creating an index page
let indexPageContent = '= Visual Embed SDK\n\n';
/* const indexPageHeading = '';
*/
const indexPageHeading = this.getHeadingString({
title: indexPageId,
pageId: indexPageId,
description: node?.comment?.shortText,
});
let sideNavContent = `* link:{{navprefix}}/${encodePageId(
indexPageId,
)}[Visual Embed SDK Reference]\n`;
projectNode?.groups?.forEach((group) => {
// create table group content
let groupContent = `== ${group.title}\n\n[div boxDiv boxFullWidth]\n--\n`;
groupContent += this.createTypeDocTable(
group.children.map((id) =>
this.convertNodeToLink(this.childrenIdMap[id]),
),
3,
);
groupContent += '--\n\n';
const groupPageId = this.childrenIdMap[group.children[0]]
.kindString;
const groupHeading = this.getHeadingString({
title: group.title,
pageId: groupPageId,
description: group.title,
});
sideNavContent += `** link:{{navprefix}}/${encodePageId(
groupPageId,
)}[${groupPageId}]\n`;
group.children.forEach((id) => {
const child = this.childrenIdMap[id];
const pageId = `${child.kindString}_${child.name}`;
const heading = this.getHeadingString({
title: child.name,
pageId,
description: child?.comment?.shortText,
});
sideNavContent += `*** link:{{navprefix}}/${encodePageId(
pageId,
)}[${child.name}]\n`;
const content = this.convertTypeDocNode(child);
callBack(pageId, `${heading}\n\n${content}`);
});
callBack(groupPageId, `${groupHeading}\n\n${groupContent}`);
indexPageContent += groupContent;
});
callBack('VisualEmbedSdkNavLinks', sideNavContent);
callBack(indexPageId, `${indexPageHeading}\n\n${indexPageContent}`);
let customSideNavContent = '';
Object.keys(this.groupMap).forEach((k) => {
customSideNavContent += `*** ${k}\n`;
this.groupMap[k].forEach((toLinkNode) => {
const linkToNode = `link:{{navprefix}}/${this.getPageId(
toLinkNode,
)}`;
customSideNavContent += `**** [.${this.getTypeCSSClass(
toLinkNode,
)}]#${linkToNode}[${toLinkNode.name}]#\n`;
});
customSideNavContent += '\n';
});
callBack('CustomSideNav', customSideNavContent);
};
}
class TypedocConverter {
private typedDocParser = new TypeDocParser();
constructor(branch: string) {
TypeDocInternalParser.convertNameToLink = this.typedDocParser.convertNameToLink;
TypeDocInternalParser.GITHUB_LINK = `https://github.com/thoughtspot/visual-embed-sdk/blob/${branch}/src`;
console.info('Source link : ', TypeDocInternalParser.GITHUB_LINK);
}
private writeFile(filePath: string, content: string): void {
const folderPath = path.dirname(filePath);
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
}
console.info('File created : ', filePath);
fs.writeFileSync(filePath, content);
}
public generateFiles = (typedocNode: TypeDocNode) => {
// starting node should be a project node
if (typedocNode.kindString !== TypeDocReflectionKind.Project) {
return;
}
const indexPageId = 'VisualEmbedSdk';
this.typedDocParser.handleProjectNode(
typedocNode,
indexPageId,
(pageId, content) => {
const updatedPageId = pageId.replace('_', '/');
const filePath =
pageId === 'VisualEmbedSdkNavLinks' ||
pageId === 'CustomSideNav'
? `modules/ROOT/pages/common/generated/typedoc/${updatedPageId}.adoc`
: `modules/ROOT/pages/generated/typedoc/${updatedPageId}.adoc`;
this.writeFile(filePath, content);
},
);
};
}
const getFileFromUrl = async (url: string) => {
console.log('Reading from remote');
const data = await nodeFetch(url);
return data.text();
};
const getFileFromLocal = async (filePath: string) => {
console.log('Reading from local');
const fileContent = await fs.promises.readFile(filePath, 'utf8');