Skip to content

[Feature Request] Extract classes from code blocks #184

Closed
@jamesst20

Description

@jamesst20

Request

Add the ability to transform code block attributes into classes.

Exemple

\  ```rust,ignore,does_not_compile
\  {{#rustdoc_include ../listings/ch09-error-handling/listing-09-10/src/main.rs}}
\  ```

Right now only the first attributes is kept: rust.

Here I believe it's where the code attributes are being stripped.

impl<'book> CodeBlock<'book> {
pub fn new(kind: &'book CodeBlockKind<'_>, cfg: Option<&'book mdbook::config::Code>) -> Self {
// MdBook supports custom attributes in code block info strings.
// Attributes are separated by a comma, space, or tab from the language name.
// See https://rust-lang.github.io/mdBook/format/mdbook.html#rust-code-block-attributes
// This processes and strips out the attributes.
let (language, mut attributes) = {
let info_string = match kind {
CodeBlockKind::Indented => "",
CodeBlockKind::Fenced(info_string) => info_string,
};
let mut parts = info_string.split([',', ' ', '\t']).map(|part| part.trim());
(parts.next(), parts)
};
match language {
Some("rust") => Self::Rust,
language => {
let hidelines_override =
attributes.find_map(|attr| attr.strip_prefix("hidelines="));
let hidelines_prefix = hidelines_override.or_else(|| {
// Respect [output.html.code.hidelines]
Some(cfg?.hidelines.get(language?)?.as_str())
});
Self::Other {
language,
hidelines_prefix,
}
}
}
}
}

If we could keep classes like does_not_compile, it would grant the ability to style those code blocks to add some visual In the Epub that it doesn't compile.

Thank you :)!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions