Test pages that include other files
Many doc sets build pages from shared files: a setup snippet included in five tutorials, or a partial that holds one procedure. Doc Detective reads a page the way a reader sees it. Before it looks for tests, it replaces each include directive with the included file’s content. As a result:
- A test in a snippet runs inside each page that includes it, in reading order.
- A test can start on the page and continue in a snippet.
- A partial, such as
_setup.md, doesn’t run on its own, out of context.
Include resolution is on by default. You don’t need to configure anything for the built-in syntax below.
Supported include syntax
Doc Detective reads each included file with its own format’s syntax, so includes can nest. A Markdown snippet can include another snippet, and so on. A file whose extension matches no file type, such as .txt, is read with the including file’s syntax.
AsciiDoc
- A path can use attributes, such as
{snippets}, that an:attribute:entry defines earlier in the file. tag=loginincludes the lines betweentag::login[]andend::login[]in the target.tags=login;logoutincludes several regions.- A backslash escapes a directive:
\include::setup.adoc[]stays as text.
MkDocs snippets
- A path resolves against the working directory first, then against the including file’s folder, as MkDocs resolves it against its base path.
"path:login"includes the section between--8<-- [start:login]and--8<-- [end:login]in the target.- An indented directive indents every included line, so a snippet included inside a list item stays in the list.
mdBook
:loginincludes the lines betweenANCHOR: loginandANCHOR_END: loginin the target.- A backslash escapes a directive:
\{{#include setup.md}}stays as text.
MDX components
- Docusaurus
@site/paths resolve from the nearest folder that holdsdocusaurus.config.*. - Mintlify
/snippets/paths resolve from the nearest folder that holdsdocs.jsonormint.json. - Starlight and Astro
import { Content } from '...',{ Content as Name }, andimport * as Name from '...'with<Name.Content />all work. - Package imports and path aliases, such as
~/, aren’t followed. - Components inside a code fence are examples, not includes, so Doc Detective leaves them alone.
- When a Markdown (
.md) file with HTML comments is included into an.mdxpage, its comments become MDX comments, so inline test statements in it still work.
reStructuredText
- A path that starts with
/resolves from the nearest folder that holdsconf.py, the Sphinx source folder. :start-line:and:end-line:count lines from 0, as docutils does.:start-after:and:end-before:select the text between two markers.- An indented directive indents every included line, so a snippet included inside a directive stays in it.
:literal:and:code:includes, and docutils standard includes such as<isonum.txt>, show text rather than content, so Doc Detective leaves them alone.- An include inside a code block, a literal block, or a comment is an example, not an include, so Doc Detective leaves it alone.
How paths resolve
A relative path resolves against the including file’s folder first, then against the working directory. MkDocs snippets try the working directory first. The MDX roots above have their own rules.
Every included file must be inside the working directory, after symbolic links resolve. To include a file above the current folder, run Doc Detective from a folder that contains both files.
Doc Detective reads only local files. It doesn’t follow includes in a remote input, or an include whose target is a URL.
Partials
A file that another input includes doesn’t run on its own. It runs inside each page that includes it. For example, with --input docs/, docs/_setup.md runs only as part of the pages that include it, not as a separate spec.
When a partial is included by two pages, its tests run once in each page.
A file listed in beforeAll or afterAll still runs in that phase, even when a page in input includes it. Its tests then also run inside that page.
To run a partial by itself, name it alone:
Results, badges, and verified dates
Test and step location values in results point at the file that holds the statement. When a statement comes from an included file, location gains a file field with that file’s absolute path, and line, startIndex, and endIndex refer to that file. The spec’s contentPath and IDs still name the page.
Doc Detective writes freshness badges and verified dates from Last verified on into the file that holds the test statement. A test in a snippet updates the snippet.
When an include can’t resolve
A broken include never fails a run. Doc Detective prints a warning on stderr and keeps going. The directive stays in the page as text, except when a tag, section, anchor, or line range isn’t found: then the directive includes nothing. The run’s exit code doesn’t change.
Paths in the messages are absolute. An AsciiDoc include with opts=optional doesn’t warn when its file is missing.
To see which partials Doc Detective skipped as standalone inputs, run with --log-level debug.
MkDocs block-form snippets and directives in a remote input stay as text. An AsciiDoc tags=* wildcard warns that it can’t find *, and includes nothing.
Turn include resolution off
To read each file on its own, set resolveIncludes to false in your config:
Or set it for a single run through the environment:
With resolution off, partials run on their own again.
Add include syntax for a custom format
For syntax Doc Detective doesn’t know, add includeStatements to a file type. Each entry is a regular expression whose first group in parentheses matches the path to include. Doc Detective includes the whole file. ^ and $ match at line boundaries.
For example, to follow Jekyll’s include_relative tag in Markdown:
A file type that’s named after or extends a built-in format keeps that format’s include syntax and adds yours.
DITA
DITA reuse works differently. Conrefs, conkeyrefs, and keyrefs resolve when Doc Detective processes a DITA map with DITA-OT. See DITA.
Next steps
- See the Markdown, AsciiDoc, reStructuredText, and custom input format pages.
- See inline tests to put tests in your snippets.
- See the configuration reference for
resolveIncludesandfileTypes.