Skip to content

Commit effeb6b

Browse files
committed
build: transform guide markdown files with bazel
* Initial implementation of a rule that can transform markdown files to the equivalent HTML output. This is the foundation for packaging the guides into the `//src/material-examples` package (see: #13454)
1 parent 26c73e0 commit effeb6b

File tree

8 files changed

+135
-1
lines changed

8 files changed

+135
-1
lines changed

.circleci/config.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ jobs:
8888
- *restore_cache
8989
- *copy_bazel_config
9090

91-
# TODO(jelbourn): Update this command to run all tests if the Bazel issues have been fixed.
9291
- run: bazel build src/...
9392
- run: bazel test src/...
9493

guides/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package(default_visibility=["//visibility:public"])
2+
3+
load("//tools/markdown-to-html:index.bzl", "markdown_to_html")
4+
5+
markdown_to_html(
6+
name = "guides",
7+
srcs = glob(["**/*.md"]),
8+
)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
"karma-sauce-launcher": "^1.2.0",
117117
"karma-sourcemap-loader": "^0.3.7",
118118
"magic-string": "^0.22.4",
119+
"marked": "^0.5.1",
119120
"minimatch": "^3.0.4",
120121
"minimist": "^1.2.0",
121122
"mock-fs": "^4.7.0",

tools/markdown-to-html/BUILD.bazel

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")
4+
load("//tools:defaults.bzl", "ts_library")
5+
6+
ts_library(
7+
name = "transform-markdown",
8+
srcs = [":transform-markdown.ts"],
9+
deps = ["@matdeps//@types/node"],
10+
tsconfig = ":tsconfig.json",
11+
)
12+
13+
nodejs_binary(
14+
name = "markdown-to-html",
15+
entry_point = "angular_material/tools/markdown-to-html/transform-markdown.js",
16+
data = [
17+
"@matdeps//highlight.js",
18+
"@matdeps//marked",
19+
"@matdeps//source-map-support",
20+
":transform-markdown",
21+
],
22+
)
23+
24+

tools/markdown-to-html/index.bzl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Implementation of the "markdown_to_html" rule. The implementation runs the transform
3+
executable in order to create the outputs for the specified source files.
4+
"""
5+
def _markdown_to_html(ctx):
6+
input_files = ctx.files.srcs;
7+
args = ctx.actions.args()
8+
expected_outputs = [];
9+
10+
for input_file in input_files:
11+
basename = input_file.basename.replace('.md', '')
12+
output_file = ctx.actions.declare_file("%s.html" % basename)
13+
expected_outputs += [output_file]
14+
15+
# Add the input file and it's related output to the arguments that
16+
# will be passed to the transformer executable.
17+
args.add("%s=%s" % (input_file.path, output_file.path))
18+
19+
# Run the transform markdown executable that transforms the specified source files.
20+
# Note that we should specify the outputs here because Bazel can then throw an error
21+
# if the script didn't generate the required outputs.
22+
ctx.actions.run(
23+
inputs = input_files,
24+
executable = ctx.executable._transform_markdown,
25+
outputs = expected_outputs,
26+
arguments = [args],
27+
)
28+
29+
return DefaultInfo(files = depset(expected_outputs))
30+
31+
"""
32+
Rule definition for the "markdown_to_html" rule that can accept arbritary source files
33+
that will be transformed into HTML files. The outputs can be referenced through the
34+
default output provider.
35+
"""
36+
markdown_to_html = rule(
37+
implementation = _markdown_to_html,
38+
attrs = {
39+
"srcs": attr.label_list(allow_files = [".md"]),
40+
41+
# Executable for this rule that is responsible for converting the specified
42+
# markdown files into HTML files.
43+
"_transform_markdown": attr.label(
44+
default = Label("//tools/markdown-to-html"),
45+
executable = True,
46+
cfg = "host"
47+
)},
48+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Script that will be used by the markdown_to_html Bazel rule in order to transform
3+
* multiple markdown files into the equivalent HTML output.
4+
*/
5+
6+
import {readFileSync, writeFileSync} from 'fs';
7+
8+
// These types lack type definitions.
9+
const marked = require('marked');
10+
const highlightJs = require('highlight.js');
11+
12+
// Setup the default options for converting markdown to HTML.
13+
marked.setOptions({
14+
// Implement a highlight function that converts the code block into a highlighted
15+
// HTML snippet that uses HighlightJS.
16+
highlight: (code: string, language: string): string => {
17+
if (language) {
18+
return highlightJs.highlight(
19+
language.toLowerCase() === 'ts' ? 'typescript' : language, code).value;
20+
}
21+
return code;
22+
}
23+
});
24+
25+
if (require.main === module) {
26+
// The script expects the input files to be specified in the following format:
27+
// {input_file_path}={output_file_path}
28+
// We have to know the output paths because the input path and output path differ
29+
// fundamentally within the Bazel sandbox.
30+
const inputFiles = process.argv.slice(2).map(argument => argument.split('='));
31+
32+
// Walk through each input file and write transformed markdown output to the specified
33+
// output path.
34+
inputFiles.forEach(([inputPath, outputPath]) => {
35+
writeFileSync(outputPath, marked(readFileSync(inputPath, 'utf8')));
36+
});
37+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2015"],
4+
"module": "commonjs",
5+
"target": "es5",
6+
"sourceMap": true,
7+
"types": ["node"]
8+
},
9+
"bazelOptions": {
10+
"suppressTsconfigOverrideWarnings": true
11+
}
12+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7469,6 +7469,11 @@ marked@^0.3.2:
74697469
resolved "https://registry.yarnpkg.com/marked/-/marked-0.3.19.tgz#5d47f709c4c9fc3c216b6d46127280f40b39d790"
74707470
integrity sha512-ea2eGWOqNxPcXv8dyERdSr/6FmzvWwzjMxpfGB/sbMccXoct+xY+YukPD+QTUZwyvK7BZwcr4m21WBOW41pAkg==
74717471

7472+
marked@^0.5.1:
7473+
version "0.5.1"
7474+
resolved "https://registry.yarnpkg.com/marked/-/marked-0.5.1.tgz#062f43b88b02ee80901e8e8d8e6a620ddb3aa752"
7475+
integrity sha512-iUkBZegCZou4AdwbKTwSW/lNDcz5OuRSl3qdcl31Ia0B2QPG0Jn+tKblh/9/eP9/6+4h27vpoh8wel/vQOV0vw==
7476+
74727477
matchdep@^2.0.0:
74737478
version "2.0.0"
74747479
resolved "https://registry.yarnpkg.com/matchdep/-/matchdep-2.0.0.tgz#c6f34834a0d8dbc3b37c27ee8bbcb27c7775582e"

0 commit comments

Comments
 (0)