Skip to main content

snippets 0.9.0 copy "snippets: ^0.9.0" to clipboard
snippets: ^0.9.0 copied to clipboard

Keep the code samples in your markdown files up to date with real, analyzed code in example/ and test/.

package:snippets pub package package publisher

This tool updates examples in fenced code blocks in markdown files to match source files in example/ or test/, using the same #region, #endregion, and #hide markers as dartdoc's {@example} directive.

Usage #

Mark a region in an example or test file:

import 'package:test/test.dart';

void main() {
  test('greets the world', () {
    // #region greeting
    final message = 'Hello, world!';
    print(message);
    // #endregion greeting
    expect(message, isNotEmpty);
  });
}

Reference the file (or #region) after @example in your markdown fence's info string:

```dart @example example/example.dart#greeting
final message = 'Hello, world!';
print(message);
```

A leading / resolves from the package root; any other path resolves relative to the markdown file. Pass indent=keep (default indent=strip) to keep shared leading indentation.

Update all markdown files locally (requires Dart 3.12+):

$ dart run snippets@ update

Validating in CI #

Check that no markdown code block is out of date:

$ dart run snippets@ validate

In GitHub Actions, call the reusable workflow (pinned to a commit SHA):

jobs:
  examples:
    uses: dart-lang/tools/.github/workflows/validate_snippets.yaml@<commit-sha>
1
likes
160
points
76
downloads

Documentation

API reference

Publisher

verified publishertools.dart.dev

Weekly Downloads

Keep the code samples in your markdown files up to date with real, analyzed code in example/ and test/.

Repository (GitHub)
View/report issues
Contributing

Topics

#documentation #examples #markdown

License

BSD-3-Clause (license)

Dependencies

args, collection, path, source_span, string_scanner

More

Packages that depend on snippets