pulldown-cmark-codeblock 0.1.0

A fenced code block extractor for Markdown documents built on pulldown-cmark
Documentation
  • Coverage
  • 100%
    17 out of 17 items documented1 out of 9 items with examples
  • Size
  • Source code size: 41.57 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 585.78 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • drupol/pulldown-cmark-codeblock
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • drupol

GitHub stars Crates.io Version Crates.io License Donate!

pulldown-cmark-codeblock

Extract Markdown code blocks from Markdown documents parsed with pulldown-cmark.

pulldown-cmark already exposes the fenced code block info string through Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(info_string))). This crate builds on top of that lower-level event stream and returns complete, ready-to-use code block records.

  • fenced or indented block kind
  • language parsed from the first info string word
  • raw info string
  • remaining attributes as a raw string or token iterator
  • code block source text
  • byte range covering the whole block
  • zero-based line range covering the whole block
  • indentation before the opening marker
use pulldown_cmark_codeblock::{code_blocks, CodeBlockKind};

let markdown = "# Title\n\n```rust runnable key=value\nfn main() {}\n```\n";
let block = code_blocks(markdown).next().unwrap();

assert!(matches!(block.kind, CodeBlockKind::Fenced(_)));
assert_eq!(block.language.as_deref(), Some("rust"));
assert_eq!(block.info_string, "rust runnable key=value");
assert_eq!(block.attributes.as_deref(), Some("runnable key=value"));
assert_eq!(block.attributes().collect::<Vec<_>>(), ["runnable", "key=value"]);
assert_eq!(block.source, "fn main() {}\n");
assert_eq!(block.line_range, 2..5);

The detailed API documentation is written in src/lib.rs with crate-level //! rustdoc comments, so it is generated directly by cargo doc and published with the crate documentation.