@@ -46,25 +46,53 @@ module.exports = function dabFactory(ngIoProjPath) {
46
46
log . info ( containerName , 'wrote' , Object . keys ( dataMap ) . length , 'entries to' , dataFilePath ) ;
47
47
}
48
48
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
+
49
65
function _insertExampleFragments ( enclosedByName , eltId , $ , div ) {
50
- const fragDir = path . join ( dartPkgConfigInfo . ngIoDartApiDocPath , '../../../_fragments/_api ' ) ;
66
+ const fragDirBase = path . join ( dartPkgConfigInfo . ngIoDartApiDocPath , '../../../_fragments/' ) ;
51
67
const exList = div . find ( 'p:contains("{@example")' ) ;
52
68
exList . each ( ( i , elt ) => {
53
69
const text = $ ( elt ) . text ( ) ;
54
70
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 ] * ) $ / ) ;
56
72
if ( ! matches ) {
57
73
log . warn ( enclosedByName , eltId , 'has an invalidly formed @example tag:' , text ) ;
58
74
return true ;
59
75
}
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 ) ;
60
79
const exRelPath = matches [ 1 ] ;
61
80
const region = matches [ 3 ] ;
62
81
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
+ }
64
92
const extn = path . extname ( exRelPath ) ;
65
93
const baseName = path . basename ( exRelPath , extn ) ;
66
94
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` ) ;
68
96
if ( ! fs . existsSync ( exFragPath ) ) {
69
97
log . warn ( 'Fragment not found:' , exFragPath ) ;
70
98
return true ;
@@ -80,7 +108,8 @@ module.exports = function dabFactory(ngIoProjPath) {
80
108
function _extractAndWrapInCodeTags ( md ) {
81
109
const lines = md . split ( '\n' ) ;
82
110
// 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 ( ) !== '```' ) { }
84
113
const code = lines . map ( ( line ) => encoder . htmlEncode ( line ) ) . join ( '\n' ) ;
85
114
// TS uses format="linenums"; removing that for now.
86
115
return `<code-example language="dart">${ code } \n</code-example>` ;
@@ -98,6 +127,7 @@ module.exports = function dabFactory(ngIoProjPath) {
98
127
const div = $ ( 'div.body.container' ) ;
99
128
$ ( 'div.sidebar-offcanvas-left' ) . remove ( ) ;
100
129
const baseNameNoExtn = path . basename ( e . path , '.html' ) ;
130
+ _adjustDocsRelativeLinks ( $ , div ) ;
101
131
_insertExampleFragments ( e . enclosedByQualifiedName , baseNameNoExtn , $ , div ) ;
102
132
103
133
const outFileNoExtn = path . join ( destDirPath , baseNameNoExtn ) ;
0 commit comments