Skip to content

drupol/pulldown-cmark-codeblock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

A fenced code block extractor for Markdown documents built on pulldown-cmark

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

 
 
 

Contributors