Automatically lint Markdown files in your Bazel workspace with
markdownlint (via markdownlint-cli) when you bazel build.
Start linting your Markdown source files in a few steps:
- Use an
http_archiveworkspace rule to add this project as an external workspace. - Declare a
markdown_librarywith some Markdown source files. - Enable the
markdownlint_aspectin your.bazelrc. - Use a
local_markdownlint_repositoryrepository rule to find a copy ofmarkdownlintand wrap it in a toolchain.
See examples/use_local_markdownlint for a simple but complete example.
Add something like this to your WORKSPACE file:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
DWTJ_RULES_MARKDOWN_COMMIT = "5fcac481becb6e57c7240ea42652ad9ddef36181"
DWTJ_RULES_MARKDOWN_SHA256 = "b4250fc7c13b55df27507e8d53da4fab61cfdc7c3f4b6002a8a312796f678a91"
http_archive(
name = "dwtj_rules_markdown",
sha256 = DWTJ_RULES_MARKDOWN_SHA256,
strip_prefix = "dwtj_rules_markdown-{}".format(DWTJ_RULES_MARKDOWN_COMMIT),
url = "https://github.com/dwtj/dwtj_rules_markdown/archive/{}.zip".format(DWTJ_RULES_MARKDOWN_COMMIT),
)Add something like this to your BUILD files:
load("@dwtj_rules_markdown//markdown:defs.bzl", "markdown_library")
markdown_library(
name = "md",
srcs = [
"foo.md",
"bar.md",
],
)Add this to your workspace's .bazelrc file:
build --aspects @dwtj_rules_markdown//markdown:aspects.bzl%markdownlint_aspect
Create a markdownlint-cli config file in JSON format for your toolchain
to use by default. To use markdownlint-cli's default behavior, just write the
empty object to this file. E.g.,
echo '{}' > .markdownlint.jsonAdd this to your WORKSPACE file:
load("@dwtj_rules_markdown//markdown:repositories.bzl", "local_markdownlint_repository")
local_markdownlint_repository(
name = 'local_markdownlint',
config = "@//:.markdownlint.json",
)
load('@local_markdownlint//:defs.bzl', 'register_local_markdownlint_toolchain')
register_local_markdownlint_toolchain()This will search your system path for a markdownlint executable to use.
-
markdown_library: This Bazel rule lets you declare a set of Markdown files to be linted. -
markdownlint_aspect: This Bazel aspect is what actually adds lint actions to the Bazel action graph. -
markdownlint_toolchain: This Bazel rule can be used to declare instances of the//markdown/toolchains/lint:toolchain_typetoolchain type. Such a toolchain instance includes the metadata needed to locate amarkdownlintbinary. (Some background on Bazel toolchains and toolchain resolution is provided here.)
TODO(dwtj): Keep drafting this section.
Markdown files are used in very flexible ways. There isn't one definitive way to process them in a build system. It may in fact be most common to not process them at all (or at least leave the job to GitHub).
This has influenced how we designed these rules. In particular, we've chosen
to put processing of markdown files into Bazel aspects; the
markdown_library rule does not itself create any build actions.
TODO(dwtj): Keep drafting this section.