Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions deployment/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@
"description": "Uses an underline of = or - beneath the heading text (setext headings). Only applies to level 1 and 2 headings."
}]
},
"unindentCodeBlocks": {
"description": "Whether to unindent the contents of code blocks.",
"type": "boolean",
"default": true,
"oneOf": [{
"const": true,
"description": "Removes common leading whitespace from code block contents."
}, {
"const": false,
"description": "Preserves the original indentation of code block contents."
}]
},
"listIndentKind": {
"description": "The style of indentation to use for list items. CommonMark aligns to the content column after the marker. PythonMarkdown uses a fixed 4-space indent, required by tools like mkdocs-material.",
"type": "string",
Expand Down Expand Up @@ -137,6 +149,9 @@
"headingKind": {
"$ref": "#/definitions/headingKind"
},
"unindentCodeBlocks": {
"$ref": "#/definitions/unindentCodeBlocks"
},
"listIndentKind": {
"$ref": "#/definitions/listIndentKind"
},
Expand Down
9 changes: 8 additions & 1 deletion src/configuration/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ impl ConfigurationBuilder {
self.insert("headingKind", value.to_string().into())
}

/// Whether to unindent the contents of code blocks.
/// Default: `true`
pub fn unindent_code_blocks(&mut self, value: bool) -> &mut Self {
self.insert("unindentCodeBlocks", value.into())
}

/// The style of list indentation to use.
/// Default: `ListIndentKind::CommonMark`
pub fn list_indent_kind(&mut self, value: ListIndentKind) -> &mut Self {
Expand Down Expand Up @@ -153,14 +159,15 @@ mod tests {
.strong_kind(StrongKind::Underscores)
.unordered_list_kind(UnorderedListKind::Asterisks)
.heading_kind(HeadingKind::Atx)
.unindent_code_blocks(false)
.list_indent_kind(ListIndentKind::PythonMarkdown)
.ignore_directive("test")
.ignore_file_directive("test")
.ignore_start_directive("test")
.ignore_end_directive("test");

let inner_config = config.get_inner_config();
assert_eq!(inner_config.len(), 12);
assert_eq!(inner_config.len(), 13);
let diagnostics = resolve_config(inner_config, &Default::default()).diagnostics;
assert_eq!(diagnostics.len(), 0);
}
Expand Down
1 change: 1 addition & 0 deletions src/configuration/resolve_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub fn resolve_config(
&mut diagnostics,
),
heading_kind: get_value(&mut config, "headingKind", HeadingKind::Atx, &mut diagnostics),
unindent_code_blocks: get_value(&mut config, "unindentCodeBlocks", true, &mut diagnostics),
list_indent_kind: get_value(&mut config, "listIndentKind", ListIndentKind::CommonMark, &mut diagnostics),
ignore_directive: get_value(
&mut config,
Expand Down
1 change: 1 addition & 0 deletions src/configuration/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct Configuration {
pub strong_kind: StrongKind,
pub unordered_list_kind: UnorderedListKind,
pub heading_kind: HeadingKind,
pub unindent_code_blocks: bool,
pub list_indent_kind: ListIndentKind,
pub ignore_directive: String,
pub ignore_file_directive: String,
Expand Down
7 changes: 6 additions & 1 deletion src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,12 @@ fn gen_block_quote(block_quote: &BlockQuote, context: &mut Context) -> PrintItem
fn gen_code_block(code_block: &CodeBlock, context: &mut Context) -> PrintItems {
let mut items = PrintItems::new();
let code_text = get_code_text(code_block, context);
let code_text = utils::unindent(code_text.trim_end());
let code_text = code_text.trim_end();
let code_text = if context.configuration.unindent_code_blocks {
utils::unindent(code_text)
} else {
Cow::Borrowed(code_text)
};
let backtick_text = "`".repeat(get_backtick_count(&code_text));
let indent_level = if code_block.is_fenced { 0 } else { 4 };

Expand Down
10 changes: 10 additions & 0 deletions tests/specs/CodeBlocks/CodeBlocks_UnindentCodeBlocks_Disabled.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
~~ unindentCodeBlocks: false ~~
!! Preserve leading spaces !!
```
one
```

[expect]
```
one
```
10 changes: 0 additions & 10 deletions tests/specs/Lists/Lists_All.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,6 @@ Here is a paragraph

1. test

!! code block in list that is indented too much should unindent back !!
- List

Code block indented too much.

[expect]
- List

Code block indented too much.

!! html in list !!
- Testing this out.

Expand Down
Loading