Skip to content

Commit 9005812

Browse files
authored
Add test for mdbook sources from different places (bazelbuild#4029)
This adds a test demonstrating how `mdbook` can stitch together sources from multiple locations in the repository. We have to mirror the workspace structure in a temporary build directory, enabling `mdbook` to resolve relative links between files regardless of their package or generation method.
1 parent d0f2bce commit 9005812

7 files changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
2+
load("//:defs.bzl", "mdbook")
3+
4+
# This shows how mdBook can include sources from anywhere in the
5+
# repository. However, because mdBook only processes files located
6+
# under the 'src' directory (as configured in book.toml), any files
7+
# from outside the book's package must be rebased into the 'src'
8+
# directory using genrules or other means. The rule's process_wrapper
9+
# ensures that the relative directory structure of these "stitched"
10+
# files is preserved in the temporary build directory.
11+
12+
mdbook(
13+
name = "stitched",
14+
srcs = [
15+
# The main navigation file for the book.
16+
"//test/stitched/src:SUMMARY.md",
17+
# A file rebased from a sibling directory into the book's src/
18+
# directory.
19+
"//test/stitched/src:rebased",
20+
# A file generated within a subpackage and placed in the
21+
# book's src/ directory.
22+
"//test/stitched/src:generated",
23+
],
24+
book = "book.toml",
25+
)
26+
27+
build_test(
28+
name = "stitched_test",
29+
targets = [":stitched"],
30+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[book]
2+
authors = ["Stitched Test"]
3+
language = "en"
4+
src = "src"
5+
title = "Stitched Test Book"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports_files(["chapter_2.md"])
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Chapter 2
2+
Other source.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
exports_files(["SUMMARY.md"])
2+
3+
# Rebase a file from a different directory into the book's source
4+
# directory. This is necessary because mdBook only processes files
5+
# located under the 'src' directory (as configured in book.toml).
6+
genrule(
7+
name = "rebased",
8+
srcs = ["//test/stitched/other_srcs:chapter_2.md"],
9+
outs = ["chapter_2.md"],
10+
cmd = "cp $< $@",
11+
visibility = ["//test/stitched:__pkg__"],
12+
)
13+
14+
genrule(
15+
name = "generated",
16+
outs = ["chapter_3.md"],
17+
cmd = "echo '# Chapter 3\nGenerated source.' > $@",
18+
visibility = ["//test/stitched:__pkg__"],
19+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Summary
2+
3+
- [Chapter 1 (Local source)](chapter_1.md)
4+
- [Chapter 2 (Rebased from sibling directory)](chapter_2.md)
5+
- [Chapter 3 (Generated in subpackage)](chapter_3.md)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Chapter 1
2+
Local source.

0 commit comments

Comments
 (0)