@@ -11,6 +11,11 @@ import * as path from 'path';
11
11
// viewer.
12
12
const EXAMPLE_PATTERN = / < ! - - \W * e x a m p l e \( ( [ ^ ) ] + ) \) \W * - - > / g;
13
13
14
+ // Markdown files can contain links to other markdown files.
15
+ // Most of those links don't work in the Material docs, because the paths are invalid in the
16
+ // documentation page. Using a RegExp to rewrite links in HTML files to work in the docs.
17
+ const LINK_PATTERN = / ( < a [ ^ > ] * ) h r e f = " ( [ ^ " ] * ) " / g;
18
+
14
19
gulp . task ( 'docs' , ( ) => {
15
20
return gulp . src ( [ 'src/lib/**/*.md' , 'guides/*.md' ] )
16
21
. pipe ( markdown ( {
@@ -25,9 +30,7 @@ gulp.task('docs', () => {
25
30
return code ;
26
31
}
27
32
} ) )
28
- . pipe ( transform ( ( content : string ) =>
29
- content . toString ( ) . replace ( EXAMPLE_PATTERN , ( match : string , name : string ) =>
30
- `<div material-docs-example="${ name } "></div>` ) ) )
33
+ . pipe ( transform ( transformMarkdownFiles ) )
31
34
. pipe ( gulp . dest ( 'dist/docs' ) ) ;
32
35
} ) ;
33
36
@@ -37,3 +40,32 @@ task('api', () => {
37
40
const dgeni = new Dgeni ( [ docsPackage ] ) ;
38
41
return dgeni . generate ( ) ;
39
42
} ) ;
43
+
44
+ /** Updates the markdown file's content to work inside of the docs app. */
45
+ function transformMarkdownFiles ( buffer : Buffer , file : any ) : string {
46
+ let content = buffer . toString ( 'utf-8' ) ;
47
+
48
+ /* Replace <!-- example(..) --> comments with HTML elements. */
49
+ content = content . replace ( EXAMPLE_PATTERN , ( match : string , name : string ) =>
50
+ `<div material-docs-example="${ name } "></div>`
51
+ ) ;
52
+
53
+ content = content . replace ( LINK_PATTERN , ( match : string , pre : string , link : string ) =>
54
+ `${ pre } href="${ fixMarkdownDocLinks ( link , file . path ) } "`
55
+ ) ;
56
+
57
+ return content ;
58
+ }
59
+
60
+ /** Fixes paths in the markdown files to work in the material-docs-io. */
61
+ function fixMarkdownDocLinks ( link : string , filePath : string ) : string {
62
+ if ( link . startsWith ( 'http' ) && filePath . indexOf ( 'guides/' ) === - 1 ) {
63
+ return link ;
64
+ }
65
+
66
+ let baseName = path . basename ( link , path . extname ( link ) ) ;
67
+
68
+ // Temporary link the file to the /guide URL because that's the route where the
69
+ // guides can be loaded in the Material docs.
70
+ return `guide/${ baseName } ` ;
71
+ }
0 commit comments