@@ -46,25 +46,53 @@ module.exports = function dabFactory(ngIoProjPath) {
4646 log . info ( containerName , 'wrote' , Object . keys ( dataMap ) . length , 'entries to' , dataFilePath ) ;
4747 }
4848
49+ function _adjustDocsRelativeLinks ( $ , div ) {
50+ // Omit leading https://angular.io so links work for local test sites.
51+ const urlToDocs = '/docs/dart/latest/' ;
52+ const urlToExamples = 'http://angular-examples.github.io/' ;
53+ const docsLinkList = div . find ( 'a[href^="docs/"],a[href^="examples/"]' ) ;
54+ docsLinkList . each ( ( i , elt ) => {
55+ const href = $ ( elt ) . attr ( 'href' ) ;
56+ const matches = href . match ( / ( \w + ) \/ ( .* ) $ / ) ;
57+ // TODO: support links to chapters of other languages, e.g., 'docs/ts/latest/...'.
58+ const urlStart = matches [ 1 ] === 'docs' ? urlToDocs : urlToExamples ;
59+ const absHref = urlStart + matches [ 2 ] ;
60+ log . info ( `Found angular.io relative link: ${ href } --> ${ absHref } ` ) ;
61+ $ ( elt ) . attr ( 'href' , absHref ) ;
62+ } ) ;
63+ }
64+
4965 function _insertExampleFragments ( enclosedByName , eltId , $ , div ) {
50- const fragDir = path . join ( dartPkgConfigInfo . ngIoDartApiDocPath , '../../../_fragments/_api ' ) ;
66+ const fragDirBase = path . join ( dartPkgConfigInfo . ngIoDartApiDocPath , '../../../_fragments/' ) ;
5167 const exList = div . find ( 'p:contains("{@example")' ) ;
5268 exList . each ( ( i , elt ) => {
5369 const text = $ ( elt ) . text ( ) ;
5470 log . debug ( `Found example: ${ enclosedByName } ${ eltId } ` , text ) ;
55- const matches = text . match ( / { @ e x a m p l e \s + ( [ ^ \s ] + ) ( \s + r e g i o n = [ \' \" ] ? ( \w + ) [ \' \" ] ? ) ? \s * } / ) ;
71+ const matches = text . match ( / ^ \s * { @ e x a m p l e \s + ( [ ^ \s ] + ) ( \s + r e g i o n = [ \' \" ] ? ( [ - \w ] + ) [ \' \" ] ? ) ? \s * } ( [ \s \S ] * ) $ / ) ;
5672 if ( ! matches ) {
5773 log . warn ( enclosedByName , eltId , 'has an invalidly formed @example tag:' , text ) ;
5874 return true ;
5975 }
76+ // const [, exRelPath, /*regionTagAndValue*/, region, rest] = matches;
77+ const rest = matches [ 4 ] . trim ( ) ;
78+ if ( rest ) log . warn ( enclosedByName , eltId , '@example must be the only element in a paragraph, but found:' , text ) ;
6079 const exRelPath = matches [ 1 ] ;
6180 const region = matches [ 3 ] ;
6281
63- const dir = path . dirname ( exRelPath )
82+ let exRelPathParts = path . dirname ( exRelPath ) . split ( path . sep ) ;
83+ let fragDir ;
84+ if ( exRelPathParts [ 0 ] === 'docs' ) {
85+ // Path is to a docs example, not an API example.
86+ const exampleName = exRelPathParts [ 1 ] ;
87+ fragDir = path . join ( fragDirBase , exampleName , 'dart' ) ;
88+ exRelPathParts = exRelPathParts . slice ( 2 ) ;
89+ } else {
90+ fragDir = path . join ( fragDirBase , '_api' ) ;
91+ }
6492 const extn = path . extname ( exRelPath ) ;
6593 const baseName = path . basename ( exRelPath , extn ) ;
6694 const fileNameNoExt = baseName + ( region ? `-${ region } ` : '' )
67- const exFragPath = path . resolve ( fragDir , dir , `${ fileNameNoExt } ${ extn } .md` ) ;
95+ const exFragPath = path . resolve ( fragDir , ... exRelPathParts , `${ fileNameNoExt } ${ extn } .md` ) ;
6896 if ( ! fs . existsSync ( exFragPath ) ) {
6997 log . warn ( 'Fragment not found:' , exFragPath ) ;
7098 return true ;
@@ -80,7 +108,8 @@ module.exports = function dabFactory(ngIoProjPath) {
80108 function _extractAndWrapInCodeTags ( md ) {
81109 const lines = md . split ( '\n' ) ;
82110 // Drop first and last lines that are the code markdown tripple ticks (and last \n):
83- lines . shift ( ) ; lines . pop ( ) ; lines . pop ( ) ;
111+ lines . shift ( ) ;
112+ while ( lines && lines . pop ( ) . trim ( ) !== '```' ) { }
84113 const code = lines . map ( ( line ) => encoder . htmlEncode ( line ) ) . join ( '\n' ) ;
85114 // TS uses format="linenums"; removing that for now.
86115 return `<code-example language="dart">${ code } \n</code-example>` ;
@@ -98,6 +127,7 @@ module.exports = function dabFactory(ngIoProjPath) {
98127 const div = $ ( 'div.body.container' ) ;
99128 $ ( 'div.sidebar-offcanvas-left' ) . remove ( ) ;
100129 const baseNameNoExtn = path . basename ( e . path , '.html' ) ;
130+ _adjustDocsRelativeLinks ( $ , div ) ;
101131 _insertExampleFragments ( e . enclosedByQualifiedName , baseNameNoExtn , $ , div ) ;
102132
103133 const outFileNoExtn = path . join ( destDirPath , baseNameNoExtn ) ;
0 commit comments