dart-use-doc-examples
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUsing Examples in Dartdoc
在Dartdoc中使用示例
Contents
目录
- 1. The Directive
{@example} - 2. Using Regions
- 3. Hiding Setup Code
- 4. Marker Filtering Rules
- 5. Placement and Path Resolution
- 6. Verification
When writing documentation that requires multi-line code examples, you should generally extract those examples into standalone files and inject them using the directive, rather than writing them inline inside comments. This ensures the examples can be analyzed, linted, and executed.
.dart{@example}///当编写需要多行代码示例的文档时,通常应将这些示例提取到独立的文件中,并使用指令注入,而不是直接写在注释内。这样可以确保示例代码能够被分析、检查和执行。
.dart{@example}///1. The {@example}
Directive
{@example}1. {@example}
指令
{@example}The directive parses an external file and resolves it into a fenced Markdown code block in the generated documentation.
{@example}Syntax:
{@example <path>[#<region>] [lang=LANGUAGE] [indent=keep|strip]}- : The path to the file. A leading
<path>evaluates from the package root. Otherwise, it is relative to the current file./ - : The language for the markdown fence. Auto-detected from the file extension (e.g.,
lang), but can be explicit (e.g.,dart).lang=text - :
indent(default) aggressively removes shared leading indentation from the code block.strip
Bad (Inline Markdown):
dart
/// Makes a client service request to the backend.
///
/// ```dart
/// final client = Client();
/// client.send();
/// ```Good (External File Injection):
dart
/// Makes a client service request to the backend.
///
/// {@example /example/client_request.dart}{@example}语法:
{@example <path>[#<region>] [lang=LANGUAGE] [indent=keep|strip]}-
:文件路径。以
<path>开头的路径从包根目录开始解析,否则相对于当前文件所在目录。/ -
:Markdown代码块的语言。会根据文件扩展名自动检测(例如
lang),也可以显式指定(例如dart)。lang=text -
:
indent(默认值)会主动移除代码块中共享的前导缩进。strip -
不推荐(内联Markdown):
dart
/// 向后端发起客户端服务请求。
///
/// ```dart
/// final client = Client();
/// client.send();
/// ```- 推荐(外部文件注入):
dart
/// 向后端发起客户端服务请求。
///
/// {@example /example/client_request.dart}2. Using Regions
2. 使用代码区域
Often, an external example file contains imports, setup, or wrappers that you don't want to show in the documentation. You can extract a specific block of code by appending to the directive path, and wrapping that code with and comments in the target file.
void main()#<region>{@example}#region#endregionDart Code (e.g., ):
/example/client.dartdart
import 'package:http/http.dart';
void main() {
// #region request_snippet
final client = Client();
client.send();
// #endregion request_snippet
}Dartdoc Usage:
dart
/// Connects the client to the server and sends a request.
///
/// {@example /example/client.dart#request_snippet}通常,外部示例文件包含导入语句、初始化代码或包装器,这些内容你可能不想在文档中展示。你可以通过在指令的路径后追加,并在目标文件中用和注释包裹指定代码块,来提取特定的代码片段。
void main(){@example}#<region>#region#endregionDart代码(例如):
/example/client.dartdart
import 'package:http/http.dart';
void main() {
// #region request_snippet
final client = Client();
client.send();
// #endregion request_snippet
}Dartdoc用法:
dart
/// 将客户端连接到服务器并发送请求。
///
/// {@example /example/client.dart#request_snippet}3. Hiding Setup Code
3. 隐藏初始化代码
If there is a specific line of code within your extracted region that is necessary for the compiler/analyzer to pass but irrelevant (or distracting) for the documentation reader, append to that line.
#hideDart Code:
dart
final mockServer = startServer(); // #hide
final data = await fetch(mockServer.url);In the generated documentation, only will be visible. The line with is completely dropped.
final data = await fetch(mockServer.url);#hide如果提取的代码区域中有某一行代码对编译器/分析器来说是必需的,但对文档读者来说无关紧要(或容易分散注意力),可以在该行末尾追加。
#hideDart代码:
dart
final mockServer = startServer(); // #hide
final data = await fetch(mockServer.url);在生成的文档中,只有会显示出来。带有的行将被完全移除。
final data = await fetch(mockServer.url);#hide4. Marker Filtering Rules
4. 标记过滤规则
When working with , , and markers, you must follow these two technical constraints:
#hide#region#endregion- Region Required: The markers are only processed and stripped when you target a specific region suffix (e.g., ). If you inject an entire file without a region suffix, the file is embedded exactly as it appears in the source, including any marker text like
{@example file.dart#region_name}.// #hide - Format Agnosticism: The marker system is completely format-agnostic. Dartdoc simply runs a regex to strip lines containing the marker strings, meaning it works identically in non-Dart files (e.g., inside YAML comments or HTML comments
# #region).<!-- #region -->
使用、和标记时,必须遵循以下两个技术约束:
#hide#region#endregion- 必须指定区域: 只有当你指定了特定的区域后缀(例如)时,标记才会被处理和移除。如果不添加区域后缀直接注入整个文件,文件会完全按照源文件的样子嵌入,包括
{@example file.dart#region_name}等标记文本。// #hide - 格式无关性: 标记系统完全与格式无关。Dartdoc只是通过正则表达式移除包含标记字符串的行,这意味着它在非Dart文件中的工作方式完全相同(例如YAML注释中的或HTML注释中的
# #region)。<!-- #region -->
5. Placement and Path Resolution
5. 放置位置与路径解析
The directive is a block-level directive. It must appear on its own line prefixed with . Its internal parser follows strict URI reference rules:
{@example}///<path>- Package-Root Paths (): Paths starting with a leading slash automatically resolve directly to the root of the Dart package. Use this when the destination file is deep.
/- Example: exactly maps to
{@example /test/data/sample.txt}.<package_root>/test/data/sample.txt
- Example:
- Relative Paths: Paths without a leading slash resolve relative to the directory of the file containing the doc comment.
- Example:
{@example ../utils/demo.dart}
- Example:
- Boundary Enforcement: Using segments to traverse upward is perfectly acceptable, but dartdoc natively stops directory traversal at the package root (it will never escape the package).
.. - No Network URLs: Absolute URIs (e.g., starting with ) are strictly not supported. The example file must sit natively somewhere in the local filesystem.
https:// - Separators & Encoding: Because dartdoc resolves the path as a URI, you must always use forward slashes () as folder separators (even on Windows). You can natively include URI-encoded characters (like
/for spaces) as permitted by URI reference rules.%20
{@example}///<path>- 包根路径(): 以斜杠开头的路径会直接解析到Dart包的根目录。当目标文件位置较深时使用此方式。
/- 示例: 精确对应
{@example /test/data/sample.txt}。<package_root>/test/data/sample.txt
- 示例:
- 相对路径: 不以斜杠开头的路径相对于包含文档注释的文件所在目录解析。
- 示例:
{@example ../utils/demo.dart}
- 示例:
- 边界限制: 使用片段向上遍历目录是允许的,但dartdoc会在包根目录处停止目录遍历(永远不会超出包的范围)。
.. - 不支持网络URL: 绝对URI(例如以开头)严格不被支持。示例文件必须位于本地文件系统中。
https:// - 分隔符与编码: 由于dartdoc将路径作为URI解析,你必须始终使用斜杠()作为文件夹分隔符(即使在Windows系统中)。你可以原生包含URI编码字符(例如用
/表示空格),这符合URI引用规则。%20
6. Verification
6. 验证
After injecting examples:
- Run on the example files to ensure the hidden setup code compiles.
dart analyze - (Optional) Run to verify that dartdoc successfully parsed the directive without throwing a "Failed to read file" or "missing region" warning.
dart doc
注入示例后:
- 对示例文件运行,确保隐藏的初始化代码可以编译通过。
dart analyze - (可选)运行,验证dartdoc是否成功解析指令,没有抛出“无法读取文件”或“区域缺失”的警告。
dart doc