@@ -21,11 +21,15 @@ import {
2121 IYamlSyntax ,
2222 IYamlParameter
2323} from './IYamlApiFile' ;
24+ import {
25+ IYamlTocFile ,
26+ IYamlTocItem
27+ } from './IYamlTocFile' ;
2428import { RenderingHelpers } from '../RenderingHelpers' ;
2529import { MarkupBuilder } from '../MarkupBuilder' ;
2630import { MarkdownRenderer , IMarkdownRenderApiLinkArgs } from '../MarkdownRenderer' ;
2731
28- const yamlSchema : JsonSchema = JsonSchema . fromFile ( path . join ( __dirname , 'typescript.schema.json' ) ) ;
32+ const yamlApiSchema : JsonSchema = JsonSchema . fromFile ( path . join ( __dirname , 'typescript.schema.json' ) ) ;
2933
3034/**
3135 * Writes documentation in the Universal Reference YAML file format, as defined by typescript.schema.json.
@@ -47,6 +51,8 @@ export class YamlGenerator {
4751 for ( const docPackage of this . _docItemSet . docPackages ) {
4852 this . _visitDocItems ( docPackage , undefined ) ;
4953 }
54+
55+ this . _writeTocFile ( this . _docItemSet . docPackages ) ;
5056 }
5157
5258 private _visitDocItems ( docItem : DocItem , parentYamlFile : IYamlApiFile | undefined ) : boolean {
@@ -75,7 +81,13 @@ export class YamlGenerator {
7581 }
7682 }
7783
78- this . _writeYamlFile ( newYamlFile , docItem ) ;
84+ const yamlFilePath : string = this . _getYamlFilePath ( docItem ) ;
85+
86+ if ( docItem . kind === DocItemKind . Package ) {
87+ console . log ( 'Writing ' + this . _getYamlFilePath ( docItem ) ) ;
88+ }
89+
90+ this . _writeYamlFile ( newYamlFile , yamlFilePath , 'UniversalReference' , yamlApiSchema ) ;
7991
8092 if ( parentYamlFile ) {
8193 if ( ! parentYamlFile . references ) {
@@ -93,6 +105,41 @@ export class YamlGenerator {
93105 return true ;
94106 }
95107
108+ /**
109+ * Write the table of contents
110+ */
111+ private _writeTocFile ( docItems : DocItem [ ] ) : void {
112+ const tocFile : IYamlTocFile = {
113+ items : this . _buildTocItems ( docItems )
114+ } ;
115+ const tocFilePath : string = path . join ( this . _outputFolder , 'toc.yml' ) ;
116+ console . log ( 'Writing ' + tocFilePath ) ;
117+ this . _writeYamlFile ( tocFile , tocFilePath , '' , undefined ) ;
118+ }
119+
120+ private _buildTocItems ( docItems : DocItem [ ] ) : IYamlTocItem [ ] {
121+ const tocItems : IYamlTocItem [ ] = [ ] ;
122+ for ( const docItem of docItems ) {
123+ if ( this . _shouldEmbed ( docItem . kind ) ) {
124+ // Don't generate table of contents items for embedded definitions
125+ continue ;
126+ }
127+
128+ const tocItem : IYamlTocItem = {
129+ name : docItem . name ,
130+ href : this . _getRelativeUrlForDocItem ( docItem , this . _outputFolder )
131+ } ;
132+
133+ tocItems . push ( tocItem ) ;
134+
135+ const childItems : IYamlTocItem [ ] = this . _buildTocItems ( docItem . children ) ;
136+ if ( childItems . length > 0 ) {
137+ tocItem . items = childItems ;
138+ }
139+ }
140+ return tocItems ;
141+ }
142+
96143 private _shouldEmbed ( docItemKind : DocItemKind ) : boolean {
97144 switch ( docItemKind ) {
98145 case DocItemKind . Class :
@@ -244,16 +291,7 @@ export class YamlGenerator {
244291 }
245292
246293 const currentFolder : string = path . dirname ( this . _getYamlFilePath ( containingDocItem ) ) ;
247- const targetFilePath : string = this . _getYamlFilePath ( nonEmbeddedDocItem ) ;
248- const relativePath : string = path . relative ( currentFolder , targetFilePath ) ;
249- let relativeUrl : string = relativePath
250- . replace ( / [ \\ ] / g, '/' ) // replace all backslashes with slashes
251- . replace ( / \. [ ^ \. \\ / ] + $ / , '' ) ; // remove file extension
252-
253- if ( relativeUrl . substr ( 0 , 1 ) !== '.' ) {
254- // If the path doesn't already start with "./", then add this prefix.
255- relativeUrl = './' + relativeUrl ;
256- }
294+ const relativeUrl : string = this . _getRelativeUrlForDocItem ( nonEmbeddedDocItem , currentFolder ) ;
257295
258296 // Do we need to link to a fragment within the page?
259297 if ( nonEmbeddedDocItem !== result . docItem ) {
@@ -270,23 +308,42 @@ export class YamlGenerator {
270308 } ) ;
271309 }
272310
273- private _writeYamlFile ( yamlFile : IYamlApiFile , docItem : DocItem ) : void {
274- const yamlFilePath : string = this . _getYamlFilePath ( docItem ) ;
275-
276- if ( docItem . kind === DocItemKind . Package ) {
277- console . log ( 'Writing ' + this . _getYamlFilePath ( docItem ) ) ;
311+ // Returns a relative URL such as "../package/MyClass"
312+ private _getRelativeUrlForDocItem ( docItem : DocItem , currentFolder : string ) : string {
313+ const targetFilePath : string = this . _getYamlFilePath ( docItem ) ;
314+ const relativePath : string = path . relative ( currentFolder , targetFilePath ) ;
315+ let relativeUrl : string = relativePath
316+ . replace ( / [ \\ ] / g, '/' ) // replace all backslashes with slashes
317+ . replace ( / \. [ ^ \. \\ / ] + $ / , '' ) ; // remove file extension
318+
319+ if ( relativeUrl . substr ( 0 , 1 ) !== '.' ) {
320+ // If the path doesn't already start with "./", then add this prefix.
321+ relativeUrl = './' + relativeUrl ;
278322 }
323+ return relativeUrl ;
324+ }
325+
326+ private _writeYamlFile ( dataObject : { } , filePath : string , yamlMimeType : string ,
327+ schema : JsonSchema | undefined ) : void {
279328
280- JsonFile . validateNoUndefinedMembers ( yamlFile ) ;
329+ JsonFile . validateNoUndefinedMembers ( dataObject ) ;
281330
282- const stringified : string = '### YamlMime:UniversalReference\n' + yaml . safeDump ( yamlFile , {
331+ let stringified : string = yaml . safeDump ( dataObject , {
283332 lineWidth : 120
284333 } ) ;
334+
335+ if ( yamlMimeType ) {
336+ stringified = `### YamlMime:${ yamlMimeType } \n` + stringified ;
337+ }
338+
285339 const normalized : string = stringified . split ( '\n' ) . join ( '\r\n' ) ;
286340
287- fsx . mkdirsSync ( path . dirname ( yamlFilePath ) ) ;
288- fsx . writeFileSync ( yamlFilePath , normalized ) ;
289- yamlSchema . validateObject ( yamlFile , yamlFilePath ) ;
341+ fsx . mkdirsSync ( path . dirname ( filePath ) ) ;
342+ fsx . writeFileSync ( filePath , normalized ) ;
343+
344+ if ( schema ) {
345+ schema . validateObject ( dataObject , filePath ) ;
346+ }
290347 }
291348
292349 /**
0 commit comments