Skip to content

Commit 1b05432

Browse files
committed
fix(docs): properly create links in guide files
* Adds a processor to the `gulp docs` task that can modify links for the Material documentation * Right now the processor just rewrites `/guides/` paths to `/guide/` and also removes the file extensions. This makes the guides work properly in the documentation.
1 parent 55b9224 commit 1b05432

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

tools/gulp/tasks/docs.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import * as path from 'path';
1111
// viewer.
1212
const EXAMPLE_PATTERN = /<!--\W*example\(([^)]+)\)\W*-->/g;
1313

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[^>]*) href="([^"]*)"/g;
18+
1419
gulp.task('docs', () => {
1520
return gulp.src(['src/lib/**/*.md', 'guides/*.md'])
1621
.pipe(markdown({
@@ -25,9 +30,7 @@ gulp.task('docs', () => {
2530
return code;
2631
}
2732
}))
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))
3134
.pipe(gulp.dest('dist/docs'));
3235
});
3336

@@ -37,3 +40,32 @@ task('api', () => {
3740
const dgeni = new Dgeni([docsPackage]);
3841
return dgeni.generate();
3942
});
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

Comments
 (0)