Skip to content

Commit 14fd45f

Browse files
committed
Address feedback
1 parent effeb6b commit 14fd45f

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

tools/markdown-to-html/index.bzl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@ def _markdown_to_html(ctx):
77
args = ctx.actions.args()
88
expected_outputs = [];
99

10+
# Add the bazel bin directory to the command arguments. The script needs to know about
11+
# the output directory because the input files are not in the same location as the bazel
12+
# bin directory.
13+
args.add(ctx.bin_dir.path)
14+
1015
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]
16+
# Remove the extension from the input file path. Note that we should not use `.replace`
17+
# here because the extension can be also in upper case.
18+
basename = input_file.basename[:-len(".md")]
19+
20+
# For each input file "xxx.md", we want to write an output file "xxx.html"
21+
expected_outputs += [ctx.actions.declare_file("%s.html" % basename)]
1422

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))
23+
# Add the input file to the command line arguments that will be passed to the
24+
# transform-markdown executable.
25+
args.add(input_file.path)
1826

1927
# Run the transform markdown executable that transforms the specified source files.
2028
# Note that we should specify the outputs here because Bazel can then throw an error

tools/markdown-to-html/transform-markdown.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
*/
55

66
import {readFileSync, writeFileSync} from 'fs';
7+
import {join} from 'path';
78

89
// These types lack type definitions.
910
const marked = require('marked');
1011
const highlightJs = require('highlight.js');
1112

13+
// Regular expression that matches the markdown extension of a given path.
14+
const markdownExtension = /.md$/;
15+
1216
// Setup the default options for converting markdown to HTML.
1317
marked.setOptions({
1418
// Implement a highlight function that converts the code block into a highlighted
@@ -23,15 +27,14 @@ marked.setOptions({
2327
});
2428

2529
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('='));
30+
// The script expects the bazel-bin path as first argument. All remaining arguments will be
31+
// considered as markdown input files that need to be transformed.
32+
const [bazelBinPath, ...inputFiles] = process.argv.slice(2);
3133

3234
// Walk through each input file and write transformed markdown output to the specified
33-
// output path.
34-
inputFiles.forEach(([inputPath, outputPath]) => {
35+
// Bazel bin directory.
36+
inputFiles.forEach(inputPath => {
37+
const outputPath = join(bazelBinPath, inputPath.replace(markdownExtension, '.html'));
3538
writeFileSync(outputPath, marked(readFileSync(inputPath, 'utf8')));
3639
});
3740
}

0 commit comments

Comments
 (0)