@@ -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,36 @@ 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
+ /* Replaces the URL in anchor elements inside of compiled markdown files. */
54
+ content = content . replace ( LINK_PATTERN , ( match : string , head : string , link : string ) =>
55
+ // The head is the first match of the RegExp and is necessary to ensure that the RegExp matches
56
+ // an anchor element. The head will be then used to re-create the existing anchor element.
57
+ // If the head is not prepended to the replaced value, then the first match will be lost.
58
+ `${ head } href="${ fixMarkdownDocLinks ( link , file . path ) } "`
59
+ ) ;
60
+
61
+ return content ;
62
+ }
63
+
64
+ /** Fixes paths in the markdown files to work in the material-docs-io. */
65
+ function fixMarkdownDocLinks ( link : string , filePath : string ) : string {
66
+ if ( link . startsWith ( 'http' ) && filePath . indexOf ( 'guides/' ) === - 1 ) {
67
+ return link ;
68
+ }
69
+
70
+ let baseName = path . basename ( link , path . extname ( link ) ) ;
71
+
72
+ // Temporary link the file to the /guide URL because that's the route where the
73
+ // guides can be loaded in the Material docs.
74
+ return `guide/${ baseName } ` ;
75
+ }
0 commit comments