-
Notifications
You must be signed in to change notification settings - Fork 34
/
DocmaWeb.Utils.js
1880 lines (1779 loc) · 54.1 KB
/
DocmaWeb.Utils.js
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
/* global */
/* eslint max-depth:0, no-var:0, prefer-template:0, prefer-arrow-callback:0 */
// Note: This is for use in the browser. ES2015 rules don't apply here (yet).
/**
* Docma (web) core class.
* See {@link ?api=docma-web|documentation}.
* @name DocmaWeb
* @class
*/
// --------------------------------
// NAMESPACE: DocmaWeb.Utils
// https://github.com/onury/docma
// --------------------------------
/**
* Utilities for inspecting JSDoc documentation and symbols; and parsing
* documentation data into proper HTML.
* @name DocmaWeb.Utils
* @type {Object}
* @namespace
*/
var Utils = {};
/** @private */
function getStr(value) {
return value && value.trim() !== '' ? value : null;
}
/**
* Cleans the given symbol name.
* e.g. <anonymous>~obj.doStuff —> obj.doStuff
* @private
*/
function cleanName(name) {
return (name || '').replace(/([^>]+>)?~?(.*)/, '$2')
.replace(/^(module\.)?exports\./, '');
}
/** @private */
function getMetaCodeName(symbol) {
return cleanName(Utils.notate(symbol, 'meta.code.name') || '');
}
/** @private */
function identity(o) {
return o;
}
/**
* Gets the type of the given object.
* @name DocmaWeb.Utils.type
* @function
* @static
*
* @param {*} obj - Object to be inspected.
* @returns {String} - Lower-case name of the type.
*/
Utils.type = function (obj) {
return Object.prototype.toString.call(obj).match(/\s(\w+)/i)[1].toLowerCase();
};
/**
* Gets the value of the target property by the given dot
* {@link https://github.com/onury/notation|notation}.
* @name DocmaWeb.Utils.notate
* @function
* @static
*
* @param {Object} obj - Source object.
* @param {String} notation - Path of the property in dot-notation.
*
* @returns {*} - The value of the notation. If the given notation does
* not exist, safely returns `undefined`.
*
* @example
* var symbol = { code: { meta: { type: "MethodDefinition" } } };
* DocmaWeb.Utils.notate(symbol, "code.meta.type"); // returns "MethodDefinition"
*/
Utils.notate = function (obj, notation) {
if (typeof obj !== 'object') return;
var o,
props = !Array.isArray(notation)
? notation.split('.')
: notation,
prop = props[0];
if (!prop) return;
o = obj[prop];
if (props.length > 1) {
props.shift();
return Utils.notate(o, props);
}
return o;
};
/**
* Gets the short name of the given symbol.
* JSDoc overwrites the `longname` and `name` of the symbol, if it has an
* alias. This returns the correct short name.
* @name DocmaWeb.Utils.getName
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {String}
*/
Utils.getName = function (symbol) {
// if @alias is set, the original (long) name is only found at meta.code.name
if (symbol.alias) {
var codeName = getMetaCodeName(symbol);
if (codeName) return codeName.replace(/.*?[#.~:](\w+)$/i, '$1');
}
return symbol.name;
};
/**
* Gets the original long name of the given symbol.
* JSDoc overwrites the `longname` and `name` of the symbol, if it has an
* alias. This returns the correct long name.
* @name DocmaWeb.Utils.getLongName
* @function
* @alias getFullName
* @static
*
* @param {Object} symbol - Documented symbol object.
* @returns {String}
*/
Utils.getLongName = function (symbol) {
var longName = cleanName(symbol.longname);
var metaCodeName = getMetaCodeName(symbol) || longName;
var memberOf = cleanName(symbol.memberof || '');
// JSDoc bug: if the constructor is not marked with @constructs, the
// longname is incorrect. e.g. `ClassName#ClassName`. So we return
// (clean) meta.code.name in this case. e.g. `ClassName`
if (symbol.name === memberOf && Utils.isConstructor(symbol)) {
return metaCodeName;
}
// if @alias is set, the original (long) name is generally found at
// meta.code.name
var codeName = symbol.alias ? metaCodeName : longName;
if (!memberOf) return codeName;
var re = new RegExp('^' + memberOf + '[#.~:]'),
dot = symbol.scope === 'instance' ? '#' : '.';
return re.test(codeName) ? codeName : memberOf + dot + codeName;
};
Utils.getFullName = Utils.getLongName;
/**
* Gets the code name of the given symbol.
* @name DocmaWeb.Utils.getCodeName
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {String} - If no code name, falls back to long name.
*/
Utils.getCodeName = function (symbol) {
return getMetaCodeName(symbol) || Utils.getLongName(symbol);
};
/**
* Gets the first matching symbol by the given name.
* @name DocmaWeb.Utils.getSymbolByName
* @function
*
* @param {Array|Object} docsOrApis - Documentation array or APIs object
* with signature `{ documentation:Array, symbols:Array }`.
* @param {String} name - Symbol name to be checked. Better, pass the
* `longname` (or `$longname`). It will still find a short name but it'll
* return the first occurence if there are multiple symbols with the same
* short name. e.g. `create` is ambiguous but `Docma.create` is unique.
*
* @returns {Object} - Symbol object if found. Otherwise, returns `null`.
*/
Utils.getSymbolByName = function (docsOrApis, name) {
var i, symbol, docs, found;
if (Utils.type(docsOrApis) === 'object') {
var apiNames = Object.keys(docsOrApis);
for (i = 0; i < apiNames.length; i++) {
docs = docsOrApis[apiNames[i]].documentation;
found = Utils.getSymbolByName(docs, name);
if (found) return found;
}
return null;
}
docs = docsOrApis;
for (i = 0; i < docs.length; i++) {
symbol = docs[i];
if (symbol.name === name
|| symbol.longname === name
|| Utils.getFullName(symbol) === name) {
return symbol;
}
if (symbol.$members) {
found = Utils.getSymbolByName(symbol.$members, name);
if (found) return found;
}
}
return null;
};
/**
* Gets the number of levels for the given symbol or name. e.g.
* `mylib.prop` has 2 levels.
* @name DocmaWeb.Utils.getLevels
* @function
*
* @param {Object|String} symbol - Documented symbol object or long name.
* @returns {Number}
*/
Utils.getLevels = function (symbol) {
var longname = (typeof symbol === 'string' ? symbol : symbol.$longname) || '';
longname = cleanName(longname);
// colon (:) is not a level separator. JSDoc uses colon in cases like:
// `obj~event:ready` or `module:someModule`
return longname
? ((longname || '').split(/[.#~]/) || []).length
: 0;
};
/**
* Gets the parent symbol name from the given symbol object or symbol's name
* (notation). Note that, this will return the parent name even if the parent
* symbol does not exist in the documentation. If there is no parent, returns
* `""` (empty string).
* @name DocmaWeb.Utils.getParentName
* @function
*
* @param {Object|String} symbol - Documented symbol object or long name.
* @returns {Number}
*/
Utils.getParentName = function (symbol) {
var longname;
if (typeof symbol !== 'string') {
if (symbol.memberof) return cleanName(symbol.memberof);
longname = cleanName(symbol.$longname);
} else {
longname = cleanName(symbol);
}
// colon (:) is not a level separator. JSDoc uses colon in cases like:
// `obj~event:ready` or `module:someModule`
if (!longname || !(/[.#~]/g).test(longname)) return '';
return longname.replace(/[.#~][^.#~]*$/, '');
};
/**
* Gets the parent symbol object from the given symbol object or symbol's
* name.
* @name DocmaWeb.Utils.getParent
* @function
*
* @param {Object|String} symbol - Documented symbol object or long name.
* @returns {String} - `null` if symbol has no parent.
*/
Utils.getParent = function (docs, symbol) {
var sym = typeof symbol === 'string'
? Utils.getSymbolByName(docs, symbol)
: symbol;
if (!sym) return null;
var parentName = (sym && cleanName(sym.memberof)) || Utils.getParentName(symbol);
if (parentName) return Utils.getSymbolByName(docs, parentName);
return null;
};
/**
* Checks whether the given symbol is deprecated.
* @name DocmaWeb.Utils.isDeprecated
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isDeprecated = function (symbol) {
return symbol.deprecated;
};
/**
* Checks whether the given symbol has global scope.
* @name DocmaWeb.Utils.isGlobal
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isGlobal = function (symbol) {
return symbol.scope === 'global';
};
/**
* Checks whether the given symbol is a namespace.
* @name DocmaWeb.Utils.isNamespace
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isNamespace = function (symbol) {
return symbol.kind === 'namespace';
};
/**
* Checks whether the given symbol is a module.
* @name DocmaWeb.Utils.isModule
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isModule = function (symbol) {
return symbol.kind === 'module';
};
/**
* Checks whether the given symbol is marked as a mixin (is intended to be
* added to other objects).
* @name DocmaWeb.Utils.isMixin
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isMixin = function (symbol) {
return symbol.kind === 'mixin';
};
/**
* Checks whether the given symbol is a class.
* @name DocmaWeb.Utils.isClass
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isClass = function (symbol) {
return symbol.kind === 'class'
&& Utils.notate(symbol, 'meta.code.type') !== 'MethodDefinition'; // constructor if MethodDefinition
// && Utils.notate(symbol, 'meta.code.type') === 'ClassDeclaration';
};
/**
* Checks whether the given symbol is marked as a constant.
* @name DocmaWeb.Utils.isConstant
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isConstant = function (symbol) {
return symbol.kind === 'constant';
};
/**
* Checks whether the given symbol is a constructor.
* @name DocmaWeb.Utils.isConstructor
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isConstructor = function (symbol) {
return symbol.kind === 'class'
&& Utils.notate(symbol, 'meta.code.type') === 'MethodDefinition';
};
/**
* Checks whether the given symbol is a static member.
* @name DocmaWeb.Utils.isStaticMember
* @function
* @alias isStatic
* @static
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isStaticMember = function (symbol) {
return symbol.scope === 'static';
};
/**
* Alias for `Utils.isStaticMember`
* @private
*/
Utils.isStatic = Utils.isStaticMember;
/**
* Checks whether the given symbol has an inner scope.
* @name DocmaWeb.Utils.isInner
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isInner = function (symbol) {
return symbol.scope === 'inner';
};
/**
* Checks whether the given symbol is an instance member.
* @name DocmaWeb.Utils.isInstanceMember
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isInstanceMember = function (symbol) {
return symbol.scope === 'instance';
};
/**
* Checks whether the given symbol is marked as an interface that other symbols
* can implement.
* @name DocmaWeb.Utils.isInterface
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isInterface = function (symbol) {
return symbol.scope === 'interface';
};
/**
* Checks whether the given symbol is a method (function).
* @name DocmaWeb.Utils.isMethod
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isMethod = function (symbol) {
var codeType = Utils.notate(symbol, 'meta.code.type');
return symbol.kind === 'function'
|| codeType === 'FunctionExpression'
|| codeType === 'FunctionDeclaration';
// for getters/setters codeType might return 'MethodDefinition'
// so we leave it out.
};
Utils.isFunction = Utils.isMethod;
/**
* Checks whether the given symbol is an instance method.
* @name DocmaWeb.Utils.isInstanceMethod
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isInstanceMethod = function (symbol) {
return Utils.isInstanceMember(symbol) && Utils.isMethod(symbol);
};
/**
* Checks whether the given symbol is a static method.
* @name DocmaWeb.Utils.isStaticMethod
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isStaticMethod = function (symbol) {
return Utils.isStaticMember(symbol) && Utils.isMethod(symbol);
};
/**
* Checks whether the given symbol is a property (and not a method/function).
* @name DocmaWeb.Utils.isProperty
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isProperty = function (symbol) {
return symbol.kind === 'member' && !Utils.isMethod(symbol);
};
/**
* Checks whether the given symbol is an instance property.
* @name DocmaWeb.Utils.isInstanceProperty
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isInstanceProperty = function (symbol) {
return Utils.isInstanceMember(symbol) && Utils.isProperty(symbol);
};
/**
* Checks whether the given symbol is a static property.
* @name DocmaWeb.Utils.isStaticProperty
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isStaticProperty = function (symbol) {
return Utils.isStaticMember(symbol) && Utils.isProperty(symbol);
};
/**
* Checks whether the given symbol is a custom type definition.
* @name DocmaWeb.Utils.isTypeDef
* @function
* @alias isCustomType
* @static
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isTypeDef = function (symbol) {
return symbol.kind === 'typedef';
};
/**
* Alias for `Utils.isTypeDef`
* @private
*/
Utils.isCustomType = Utils.isTypeDef;
/**
* Checks whether the given symbol is a callback definition.
* @name DocmaWeb.Utils.isCallback
* @function
* @static
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isCallback = function (symbol) {
var typeNames = (symbol.type || {}).names || [];
return symbol.kind === 'typedef'
&& (symbol.comment || '').indexOf('@callback ' + symbol.longname) >= 0
&& (typeNames.length === 1 && typeNames[0] === 'function');
};
/**
* Checks whether the given symbol is an enumeration.
* @name DocmaWeb.Utils.isEnum
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isEnum = function (symbol) {
return Boolean(symbol.isEnum);
};
/**
* Checks whether the given symbol is an event.
* @name DocmaWeb.Utils.isEvent
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isEvent = function (symbol) {
return symbol.kind === 'event';
};
/**
* Checks whether the given symbol is defined outside of the current package.
* @name DocmaWeb.Utils.isExternal
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isExternal = function (symbol) {
return symbol.kind === 'external';
};
/**
* Checks whether the given symbol is a generator function.
* @name DocmaWeb.Utils.isGenerator
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isGenerator = function (symbol) {
return symbol.generator && symbol.kind === 'function';
};
/**
* Checks whether the given symbol is read-only.
* @name DocmaWeb.Utils.isReadOnly
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isReadOnly = function (symbol) {
return symbol.readonly;
};
/**
* Checks whether the given symbol has `public` access.
* @name DocmaWeb.Utils.isPublic
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isPublic = function (symbol) {
return typeof symbol.access !== 'string' || symbol.access === 'public';
};
/**
* Checks whether the given symbol has `private` access.
* @name DocmaWeb.Utils.isPrivate
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isPrivate = function (symbol) {
return symbol.access === 'private';
};
/**
* Checks whether the given symbol has `package` private access; indicating
* that the symbol is available only to code in the same directory as the
* source file for this symbol.
* @name DocmaWeb.Utils.isPackagePrivate
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isPackagePrivate = function (symbol) {
return symbol.access === 'package';
};
/**
* Checks whether the given symbol has `protected` access.
* @name DocmaWeb.Utils.isProtected
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isProtected = function (symbol) {
return symbol.access === 'protected';
};
/**
* Checks whether the given symbol is undocumented.
* This checks if the symbol has any comments.
* @name DocmaWeb.Utils.isUndocumented
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.isUndocumented = function (symbol) {
// we could use the `undocumented` property but it still seems buggy.
// https://github.com/jsdoc3/jsdoc/issues/241
// `undocumented` is omitted (`undefined`) for documented symbols.
// return symbol.undocumented !== true;
return !symbol.comments;
};
/**
* Checks whether the given symbol has description.
* @name DocmaWeb.Utils.hasDescription
* @function
*
* @param {Object} symbol - Documented symbol object.
* @returns {Boolean}
*/
Utils.hasDescription = function (symbol) {
return Boolean(getStr(symbol.classdesc) || getStr(symbol.description));
};
/**
* Removes leading spaces and dashes. Useful when displaying symbol
* descriptions.
* @name DocmaWeb.Utils.trimLeft
* @function
*
* @param {String} string - String to be trimmed.
* @returns {String}
*/
Utils.trimLeft = function (string) {
// remove leading space and dashes.
return string.replace(/^[\s\n\r\-—]*/, '');
};
/**
* Removes leading and trailing new lines.
* @name DocmaWeb.Utils.trimNewLines
* @function
*
* @param {String} string - String to be trimmed.
* @returns {String}
*/
Utils.trimNewLines = function (string) {
return string.replace(/^[\r\n]+|[\r\n]+$/, '');
};
/**
* Converts back-ticks to HTML code tags.
* @name DocmaWeb.Utils.parseTicks
* @function
*
* @param {String} string
* String to be parsed.
*
* @returns {String}
*/
Utils.parseTicks = function (string) {
return string
.replace(/(```\s*)([\s\S]*?)(\s*```)/g, function (match, p1, p2) { // , p3, offset, string
return Utils.normalizeTabs(Utils._wrapCode(p2, true, true).replace(/`/g, '`'));
})
.replace(/(`)(.*?)(`)/g, function (match, p1, p2) { // , p3, offset, string
return Utils._wrapCode(p2, true);
});
};
/**
* Converts new lines to HTML paragraphs.
* @name DocmaWeb.Utils.parseNewLines
* @function
*
* @param {String} string
* String to be parsed.
* @param {Object} [options]
* Parse options.
* @param {Boolean} [options.keepIfSingle=false]
* If `true`, lines will not be converted to paragraphs.
*
* @returns {String}
*/
Utils.parseNewLines = function (string, options) {
options = options || {};
return Utils._tokenize(string, function (block, isCode) {
if (isCode) return block;
var parts = block.split(/[\r\n]{2,}/);
if (parts.length <= 1 && options.keepIfSingle) return block;
return parts.map(function (part) {
return '<p>' + part + '</p>';
}).join('');
}).join('');
};
/**
* Converts JSDoc `@link` directives to HTML anchor tags.
* @name DocmaWeb.Utils.parseLinks
* @function
*
* @param {String} string
* String to be parsed.
* @param {Object} [options]
* Parse options.
* @param {String} [options.target]
* Href target. e.g. `"_blank"`
*
* @returns {String}
*/
Utils.parseLinks = function (string, options) {
options = options || {};
var re = /\{@link +([^}]*?)\}/g;
var out = string.replace(re, function (match, p1) { // , offset, string
var link, label,
parts = p1.split('|');
if (parts.length === 1) {
link = label = parts[0].trim(); // eslint-disable-line
} else {
link = parts[0].trim();
label = parts[1].trim();
}
// label = Utils.parseTicks(label);
// if the link is a symbol, prepend with a hash to trigger the bookmark when clicked
// if (symbolNames && symbolNames.indexOf(link) >= 0) {..}
// if no slash, treat this as a bookmark
// if ((/\//i).test(link) === false) {
// return '<a href="#' + link + '">' + label + '</a>';
// }
var target = options.target
? ' target="' + options.target + '"'
: '';
return '<a href="' + link + '"' + target + '>' + label + '</a>';
});
return Utils.parseTicks(out);
};
/**
* Parses the given string into proper HTML. Removes leading whitespace,
* converts new lines to paragraphs, ticks to code tags and JSDoc links to
* anchors.
* @name DocmaWeb.Utils.parse
* @function
*
* @param {String} string
* String to be parsed.
* @param {Object} [options]
* Parse options.
* @param {Object} [options.keepIfSingle=false]
* If enabled, single lines will not be converted to paragraphs.
* @param {String} [options.target]
* Href target for links. e.g. `"_blank"`
*
* @returns {String}
*/
Utils.parse = function (string, options) {
options = options || {};
string = Utils.trimLeft(string);
string = Utils.parseNewLines(string, options);
string = Utils.parseTicks(string);
return Utils.parseLinks(string, options);
};
/**
* Normalizes the number of spaces/tabs to multiples of 2 spaces, in the
* beginning of each line. Useful for fixing mixed indets of a description
* or example.
* @name DocmaWeb.Utils.normalizeTabs
* @function
*
* @param {String} string
* String to process.
*
* @returns {String}
*/
Utils.normalizeTabs = function (string) {
var m = string.match(/^\s*/gm),
min = Infinity;
m.forEach(function (wspace, index) {
// tabs to spaces
wspace = wspace.replace(/\t/g, ' ');
// ignoring first line's indent
if (index > 0) min = Math.min(wspace.length, min);
});
// replace the minimum indent from all lines (except first)
if (min !== Infinity) {
var re = new RegExp('^\\s{' + min + '}', 'g');
string = string.replace(re, '');
}
// replace all leading spaces from first line
string = string.replace(/^\s*/, '');
var spaces;
return string.replace(/([\r\n]+)(\s+)/gm, function (match, p1, p2) { // , offset, string
// convert tabs to spaces
spaces = p2.replace(/\t/g, ' ');
// convert indent to multiples of 2
spaces = new Array(spaces.length - (spaces.length % 2) + 1).join(' ');
return p1 + spaces;
});
};
/**
* Builds a string of keywords from the given symbol.
* This is useful for filter/search features of a template.
* @name DocmaWeb.Utils.getKeywords
* @function
*
* @param {Object} symbol - Target documentation symbol.
* @returns {String}
*/
Utils.getKeywords = function (symbol) {
if (typeof symbol === 'string') return symbol.toLowerCase();
var k = Utils.getFullName(symbol) + ' '
+ symbol.longname + ' '
+ symbol.name + ' '
+ (symbol.alias || '') + ' '
+ (symbol.memberOf || '') + ' '
+ (symbol.$kind || '') + ' '
+ (symbol.scope || '') + ' '
+ (symbol.classdesc || '') + ' '
+ (symbol.description || '') + ' '
+ (symbol.filename || '') + ' '
+ (symbol.readonly ? 'readonly' : '')
+ (symbol.isEnum ? 'enum' : '');
if (Utils.isConstructor(symbol)) k += ' constructor';
if (Utils.isMethod(symbol)) k += ' method';
if (Utils.isProperty(symbol)) k += ' property';
return k.replace(/[><"'`\n\r]/g, '').toLowerCase();
};
/**
* Gets code file information from the given symbol.
* @name DocmaWeb.Utils.getCodeFileInfo
* @function
*
* @param {Object} symbol - Target documentation symbol.
* @returns {Object}
*/
Utils.getCodeFileInfo = function (symbol) {
return {
filename: Utils.notate(symbol, 'meta.filename'),
lineno: Utils.notate(symbol, 'meta.lineno'),
path: Utils.notate(symbol, 'meta.path')
};
};
/**
* Gets Docma route link for the given symbol or symbol name.
* @name DocmaWeb.Utils.getSymbolLink
* @function
* @static
*
* @param {Array|Object} docsOrApis - Documentation array or APIs object
* with signature `{ documentation:Array, symbols:Array }`.
* @param {Object|String} symbolOrName - Either the symbol itself or the
* name of the symbol.
*
* @returns {String} - Empty string if symbol is not found.
*/
Utils.getSymbolLink = function (docsOrApis, symbolOrName) {
if (typeof symbolOrName !== 'string') {
return symbolOrName.$docmaLink;
}
var symbol = Utils.getSymbolByName(docsOrApis, symbolOrName);
return symbol ? symbol.$docmaLink : '';
};
var reEndBrackets = /\[\]$/;
// regexp for inspecting type parts such as `Promise<Boolean|String>[]` or
// simply `Boolean`. this also removes/ignores dots from types such as
// Array.<String>
var reTypeParts = /^([^<]+?)(?:\.)?(?:<([^>]+)>)?(\[\])?$/;
function _link(docsOrApis, type, options) {
var endBrackets = reEndBrackets.test(type) ? '[]' : '';
var t = type.replace(reEndBrackets, '');
var opts = options || {};
var link;
var target = '';
if (opts.linkType !== 'internal') {
link = Utils._getTypeExternalLink(t);
if (link) target = ' target="_blank"';
}
if (!link && opts.linkType !== 'external') link = Utils.getSymbolLink(docsOrApis, t);
if (link) type = '<a href="' + link + '"' + target + '>' + (opts.displayText || t) + endBrackets + '</a>';
return type;
}
/**
* Gets Docma route link for the given symbol or symbol name and returns a
* string with anchor tags.
* @private
*
* @param {Array|Object} docsOrApis - Documentation array or APIs object
* with signature `{ documentation:Array, symbols:Array }`.
* @param {String} type - Symbol name or type.
* @param {String} [options] - Options
* @param {String} [options.displayText] - Alternative display text to
* be placed within the anchor tag.
* @param {String} [options.linkType] - Set to `"internal"` (Docma
* symbol link) or `"external"` (JS or Web-API MDN link), or omit to
* get any of them, if found.
*
* @returns {String}
*/
Utils._parseAnchorLinks = function (docsOrApis, strType, options) {
// see reTypeParts and reEndBrackets
var m = strType.match(reTypeParts);
if (!m || !m[1]) return '';
// maybe we have end brackets e.g. Boolean[] or Promise<Boolean>[]
var endBrackets = m[3] || '';
var sTypes = '';
// check for sub-types e.g. Promise<Boolean|String>
if (m[2]) {
sTypes = m[2].split('|');
sTypes = sTypes.map(function (t) {
return _link(docsOrApis, t, options);
}).join('<span class="code-delim">|</span>');
if (sTypes) sTypes = '<' + sTypes + '>';
}
return _link(docsOrApis, m[1], options) + sTypes + endBrackets;
};
/**
* Gets the types of the symbol as a string (joined with pipes `|`).
* @name DocmaWeb.Utils.getTypes
* @function
*
* @param {Array|Object} docsOrApis - Documentation array or APIs object
* with signature `{ documentation:Array, symbols:Array }`.
* @param {Object} symbol - Target documentation symbol.
* @param {Object} [options] - Options.
* @param {Boolean|String} [options.links=false] - Whether to add
* HTML anchor links to output. Set to `"internal"` to link
* internally (to Docma route with symbol hash, if found) or
* `"external"` to link externally (to MDN URL if this is a
* JS/Web-API built-in type/object) or `true` to try linking either
* to an internal or external target, which ever is found.
*
* @returns {String}
*
* @example
* var symbol = { "type": { "names": ["Number", "String"] } };
* DocmaWeb.Utils.getTypes(docs, symbol); // "Number|String"