Skip to content

Commit c7b1ec5

Browse files
authored
chore: add syntax highlighting to generated overview docs (#2333)
1 parent a3f6d3e commit c7b1ec5

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"gulp-sourcemaps": "^1.6.0",
8080
"gulp-transform": "^1.1.0",
8181
"gulp-typescript": "^2.13.6",
82+
"highlight.js": "^9.9.0",
8283
"jasmine-core": "^2.4.1",
8384
"karma": "^1.1.1",
8485
"karma-browserstack-launcher": "^1.0.1",

tools/gulp/tasks/docs.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import gulp = require('gulp');
22
const markdown = require('gulp-markdown');
33
const transform = require('gulp-transform');
4+
const hljs = require('highlight.js');
45
import {task} from 'gulp';
56
import * as path from 'path';
67

7-
import {SOURCE_ROOT, PROJECT_ROOT} from '../constants';
8-
import {
9-
execNodeTask
10-
} from '../task_helpers';
11-
12-
const typedocPath = path.relative(PROJECT_ROOT, path.join(SOURCE_ROOT, 'lib/typedoc.json'));
13-
148
// Our docs contain comments of the form `<!-- example(...) -->` which serve as placeholders where
159
// example code should be inserted. We replace these comments with divs that have a
1610
// `material-docs-example` attribute which can be used to locate the divs and initialize the example
@@ -19,7 +13,18 @@ const EXAMPLE_PATTERN = /<!--\W*example\(([^)]+)\)\W*-->/g;
1913

2014
gulp.task('docs', () => {
2115
return gulp.src(['src/lib/**/*.md'])
22-
.pipe(markdown())
16+
.pipe(markdown({
17+
// Add syntax highlight using highlight.js
18+
highlight: (code: string, language: string) => {
19+
if (language) {
20+
// highlight.js expects "typescript" written out, while Github supports "ts".
21+
let lang = language.toLowerCase() === 'ts' ? 'typescript' : language;
22+
return hljs.highlight(lang, code).value;
23+
}
24+
25+
return code;
26+
}
27+
}))
2328
.pipe(transform((content: string) =>
2429
content.toString().replace(EXAMPLE_PATTERN, (match: string, name: string) =>
2530
`<div material-docs-example="${name}"></div>`)))

0 commit comments

Comments
 (0)