@@ -6,10 +6,19 @@ function t(text) {
66 return u ( 'text' , text ) ;
77}
88
9- function link ( text ) {
10- var docs = globalsDocs . getDoc ( text ) ;
11- if ( docs ) {
12- return u ( 'link' , { href : docs } , [ u ( 'text' , text ) ] ) ;
9+ /**
10+ * Helper used to automatically link items to global JS documentation or to internal
11+ * documentation.
12+ *
13+ * @param {String } text - text to potentially link
14+ * @param {function } [getHref] - a function that tries
15+ * to find a URL to point a named link to
16+ * @returns {Object } [mdast](https://www.npmjs.com/package/mdast) node
17+ */
18+ function link ( text , getHref , description ) {
19+ var href = ( getHref && getHref ( text ) ) || globalsDocs . getDoc ( text ) ;
20+ if ( href ) {
21+ return u ( 'link' , { href : href } , [ u ( 'text' , description || text ) ] ) ;
1322 }
1423 return u ( 'text' , text ) ;
1524}
@@ -42,8 +51,8 @@ function decorate(formatted, str, prefix) {
4251 * Helper used to format JSDoc-style type definitions into HTML or Markdown.
4352 *
4453 * @name formatType
45- * @param {Object } node type object in doctrine style
46- * @param {function(text): text } getNamedLink a function that tries
54+ * @param {Object } node - type object in doctrine style
55+ * @param {function } getHref - a function that tries
4756 * to find a URL to point a named link to
4857 * @returns {Object[] } array of [mdast](https://www.npmjs.com/package/mdast) syntax trees
4958 * @example
@@ -52,7 +61,7 @@ function decorate(formatted, str, prefix) {
5261 * // {{ type x }}
5362 * // generates String
5463 */
55- function formatType ( node , getNamedLink ) {
64+ function formatType ( node , getHref ) {
5665 var result = [ ] ;
5766
5867 if ( ! node ) {
@@ -69,25 +78,25 @@ function formatType(node, getNamedLink) {
6978 case Syntax . VoidLiteral :
7079 return [ t ( 'void' ) ] ;
7180 case Syntax . UndefinedLiteral :
72- return [ link ( 'undefined' ) ] ;
81+ return [ link ( 'undefined' , getHref ) ] ;
7382 case Syntax . NameExpression :
74- return [ link ( node . name ) ] ;
83+ return [ link ( node . name , getHref ) ] ;
7584 case Syntax . ParameterType :
76- return [ t ( node . name + ': ' ) ] . concat ( formatType ( node . expression , getNamedLink ) ) ;
85+ return [ t ( node . name + ': ' ) ] . concat ( formatType ( node . expression , getHref ) ) ;
7786
7887 case Syntax . TypeApplication :
79- return formatType ( node . expression , getNamedLink )
80- . concat ( commaList ( getNamedLink , node . applications , '.<' , '>' ) ) ;
88+ return formatType ( node . expression , getHref )
89+ . concat ( commaList ( getHref , node . applications , '.<' , '>' ) ) ;
8190 case Syntax . UnionType :
82- return commaList ( getNamedLink , node . elements , '(' , ')' , '|' ) ;
91+ return commaList ( getHref , node . elements , '(' , ')' , '|' ) ;
8392 case Syntax . ArrayType :
84- return commaList ( getNamedLink , node . elements , '[' , ']' ) ;
93+ return commaList ( getHref , node . elements , '[' , ']' ) ;
8594 case Syntax . RecordType :
86- return commaList ( getNamedLink , node . fields , '{' , '}' ) ;
95+ return commaList ( getHref , node . fields , '{' , '}' ) ;
8796
8897 case Syntax . FieldType :
8998 if ( node . value ) {
90- return [ t ( node . key + ': ' ) ] . concat ( formatType ( node . value , getNamedLink ) ) ;
99+ return [ t ( node . key + ': ' ) ] . concat ( formatType ( node . value , getHref ) ) ;
91100 }
92101 return [ t ( node . key ) ] ;
93102
@@ -101,35 +110,36 @@ function formatType(node, getNamedLink) {
101110 result . push ( t ( 'this: ' ) ) ;
102111 }
103112
104- result = result . concat ( formatType ( node [ 'this' ] , getNamedLink ) ) ;
113+ result = result . concat ( formatType ( node [ 'this' ] , getHref ) ) ;
105114
106115 if ( node . params . length !== 0 ) {
107116 result . push ( t ( ', ' ) ) ;
108117 }
109118 }
110119
111- result = result . concat ( commaList ( getNamedLink , node . params , '' , ')' ) ) ;
120+ result = result . concat ( commaList ( getHref , node . params , '' , ')' ) ) ;
112121
113122 if ( node . result ) {
114- result = result . concat ( [ t ( ': ' ) ] . concat ( formatType ( node . result , getNamedLink ) ) ) ;
123+ result = result . concat ( [ t ( ': ' ) ] . concat ( formatType ( node . result , getHref ) ) ) ;
115124 }
116125 return result ;
117126
118127 case Syntax . RestType :
119128 // note that here we diverge from doctrine itself, which
120129 // lets the expression be omitted.
121- return decorate ( formatType ( node . expression , getNamedLink ) , '...' , true ) ;
130+ return decorate ( formatType ( node . expression , getHref ) , '...' , true ) ;
122131 case Syntax . OptionalType :
123- return decorate ( formatType ( node . expression , getNamedLink ) , '=' ) . concat (
132+ return decorate ( formatType ( node . expression , getHref ) , '=' ) . concat (
124133 node . default ? t ( '(default ' + node . default + ')' ) : [ ] ) ;
125134 case Syntax . NonNullableType :
126- return decorate ( formatType ( node . expression , getNamedLink ) , '!' , node . prefix ) ;
135+ return decorate ( formatType ( node . expression , getHref ) , '!' , node . prefix ) ;
127136 case Syntax . NullableType :
128- return decorate ( formatType ( node . expression , getNamedLink ) , '?' , node . prefix ) ;
137+ return decorate ( formatType ( node . expression , getHref ) , '?' , node . prefix ) ;
129138
130139 default :
131140 throw new Error ( 'Unknown type ' + node . type ) ;
132141 }
133142}
134143
144+ module . exports . link = link ;
135145module . exports . formatType = formatType ;
0 commit comments