22// See LICENSE in the project root for license information.
33
44import * as colors from 'colors' ;
5- import * as fsx from 'fs-extra' ;
65import * as path from 'path' ;
76import yaml = require( 'js-yaml' ) ;
87
98import { DocItemSet } from '../utils/DocItemSet' ;
109import { IYamlTocItem } from './IYamlTocFile' ;
1110import { IYamlItem } from './IYamlApiFile' ;
1211import { YamlDocumenter } from './YamlDocumenter' ;
13- import { Text } from '@microsoft/node-core-library' ;
12+ import { Text , FileSystem } from '@microsoft/node-core-library' ;
1413
1514interface ISnippetsFile {
1615 /**
@@ -44,7 +43,8 @@ export class OfficeYamlDocumenter extends YamlDocumenter {
4443 const snippetsFilePath : string = path . join ( inputFolder , 'snippets.yaml' ) ;
4544
4645 console . log ( 'Loading snippets from ' + snippetsFilePath ) ;
47- const snippetsContent : string = fsx . readFileSync ( snippetsFilePath ) . toString ( ) ;
46+
47+ const snippetsContent : string = FileSystem . readFile ( snippetsFilePath ) ;
4848 this . _snippets = yaml . load ( snippetsContent , { filename : snippetsFilePath } ) ;
4949 }
5050
@@ -68,25 +68,6 @@ export class OfficeYamlDocumenter extends YamlDocumenter {
6868
6969 protected onCustomizeYamlItem ( yamlItem : IYamlItem ) : void { // override
7070 const nameWithoutPackage : string = yamlItem . uid . replace ( / ^ [ ^ . ] + \. / , '' ) ;
71-
72- const snippets : string [ ] | undefined = this . _snippets [ nameWithoutPackage ] ;
73- if ( snippets ) {
74- delete this . _snippets [ nameWithoutPackage ] ;
75-
76- if ( ! yamlItem . remarks ) {
77- yamlItem . remarks = '' ;
78- }
79-
80- yamlItem . remarks += '\n\n#### Examples\n' ;
81- for ( const snippet of snippets ) {
82- if ( snippet . search ( / a w a i t / ) === - 1 ) {
83- yamlItem . remarks += '\n```javascript\n' + snippet + '\n```\n' ;
84- } else {
85- yamlItem . remarks += '\n```typescript\n' + snippet + '\n```\n' ;
86- }
87- }
88- }
89-
9071 if ( yamlItem . summary ) {
9172 yamlItem . summary = this . _fixupApiSet ( yamlItem . summary , yamlItem . uid ) ;
9273 yamlItem . summary = this . _fixBoldAndItalics ( yamlItem . summary ) ;
@@ -96,7 +77,7 @@ export class OfficeYamlDocumenter extends YamlDocumenter {
9677 yamlItem . remarks = this . _fixupApiSet ( yamlItem . remarks , yamlItem . uid ) ;
9778 yamlItem . remarks = this . _fixBoldAndItalics ( yamlItem . remarks ) ;
9879 yamlItem . remarks = this . _fixCodeTicks ( yamlItem . remarks ) ;
99- yamlItem . remarks = this . _fixCodeArrows ( yamlItem . remarks ) ;
80+ yamlItem . remarks = this . _fixEscapedCode ( yamlItem . remarks ) ;
10081 }
10182 if ( yamlItem . syntax && yamlItem . syntax . parameters ) {
10283 yamlItem . syntax . parameters . forEach ( part => {
@@ -106,6 +87,24 @@ export class OfficeYamlDocumenter extends YamlDocumenter {
10687 }
10788 } ) ;
10889 }
90+
91+ const snippets : string [ ] | undefined = this . _snippets [ nameWithoutPackage ] ;
92+ if ( snippets ) {
93+ delete this . _snippets [ nameWithoutPackage ] ;
94+
95+ if ( ! yamlItem . remarks ) {
96+ yamlItem . remarks = '' ;
97+ }
98+
99+ yamlItem . remarks += '\n\n#### Examples\n' ;
100+ for ( const snippet of snippets ) {
101+ if ( snippet . search ( / a w a i t / ) === - 1 ) {
102+ yamlItem . remarks += '\n```javascript\n' + snippet + '\n```\n' ;
103+ } else {
104+ yamlItem . remarks += '\n```typescript\n' + snippet + '\n```\n' ;
105+ }
106+ }
107+ }
109108 }
110109
111110 private _fixupApiSet ( markup : string , uid : string ) : string {
@@ -137,7 +136,16 @@ export class OfficeYamlDocumenter extends YamlDocumenter {
137136 return Text . replaceAll ( text , '\\`' , '`' ) ;
138137 }
139138
140- private _fixCodeArrows ( text : string ) : string {
141- return Text . replaceAll ( text , '=>' , '=>' ) ;
139+ private _fixEscapedCode ( text : string ) : string {
140+ const backtickIndex : number = text . indexOf ( '`' ) ;
141+ if ( text . indexOf ( '`' , backtickIndex ) > 0 ) {
142+ text = Text . replaceAll ( text , '=>' , '=>' ) ;
143+ let x : number = text . indexOf ( '\\' , backtickIndex ) ;
144+ while ( x >= 0 ) {
145+ text = text . replace ( / \\ ( [ ^ \\ ] ) / , '$1' ) ;
146+ x = text . indexOf ( '\\' , x + 1 ) ;
147+ }
148+ }
149+ return text ;
142150 }
143151}
0 commit comments