From 44edf0cff5b951c603cc1ec6a5ed6722c93e421c Mon Sep 17 00:00:00 2001 From: "Bryan A. Jones" Date: Mon, 10 Nov 2025 21:52:14 -0600 Subject: [PATCH 1/3] Fix: remove unindent from code blocks. --- src/generation/generate.rs | 2 +- tests/specs/CodeBlocks/CodeBlocks_All.txt | 10 ++++++++++ tests/specs/Lists/Lists_All.txt | 10 ---------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/generation/generate.rs b/src/generation/generate.rs index dac7875..90a958f 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -373,7 +373,7 @@ 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 backtick_text = "`".repeat(get_backtick_count(&code_text)); let indent_level = if code_block.is_fenced { 0 } else { 4 }; diff --git a/tests/specs/CodeBlocks/CodeBlocks_All.txt b/tests/specs/CodeBlocks/CodeBlocks_All.txt index 25261c8..0f182d6 100644 --- a/tests/specs/CodeBlocks/CodeBlocks_All.txt +++ b/tests/specs/CodeBlocks/CodeBlocks_All.txt @@ -233,3 +233,13 @@ other_formatted_77_formatted_80 ```format ignore other_formatted_77_formatted_80 ``` + +!! Preserve leading spaces !! +``` + one +``` + +[expect] +``` + one +``` diff --git a/tests/specs/Lists/Lists_All.txt b/tests/specs/Lists/Lists_All.txt index d2414c4..ac9776e 100644 --- a/tests/specs/Lists/Lists_All.txt +++ b/tests/specs/Lists/Lists_All.txt @@ -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. From 3bce6472e92a87adee85619e98b6d616e98388d6 Mon Sep 17 00:00:00 2001 From: "Bryan A. Jones" Date: Sat, 23 May 2026 00:49:17 +0500 Subject: [PATCH 2/3] Fix: add the unindent_code_block option. --- deployment/schema.json | 15 +++++++++++++++ src/configuration/builder.rs | 9 ++++++++- src/configuration/resolve_config.rs | 1 + src/configuration/types.rs | 1 + src/generation/generate.rs | 5 +++++ tests/specs/CodeBlocks/CodeBlocks_All.txt | 10 ---------- .../CodeBlocks_UnindentCodeBlocks_Disabled.txt | 10 ++++++++++ 7 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 tests/specs/CodeBlocks/CodeBlocks_UnindentCodeBlocks_Disabled.txt diff --git a/deployment/schema.json b/deployment/schema.json index 52ab289..ec517a7 100644 --- a/deployment/schema.json +++ b/deployment/schema.json @@ -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." + }] + }, "deno": { "description": "Top level configuration that sets the configuration to what is used in Deno.", "type": "boolean", @@ -125,6 +137,9 @@ "headingKind": { "$ref": "#/definitions/headingKind" }, + "unindentCodeBlocks": { + "$ref": "#/definitions/unindentCodeBlocks" + }, "deno": { "$ref": "#/definitions/deno" }, diff --git a/src/configuration/builder.rs b/src/configuration/builder.rs index 3f15de3..a8742cb 100644 --- a/src/configuration/builder.rs +++ b/src/configuration/builder.rs @@ -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 directive used to ignore a line. /// Default: `dprint-ignore` pub fn ignore_directive(&mut self, value: &str) -> &mut Self { @@ -147,13 +153,14 @@ mod tests { .strong_kind(StrongKind::Underscores) .unordered_list_kind(UnorderedListKind::Asterisks) .heading_kind(HeadingKind::Atx) + .unindent_code_blocks(false) .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(), 11); + assert_eq!(inner_config.len(), 12); let diagnostics = resolve_config(inner_config, &Default::default()).diagnostics; assert_eq!(diagnostics.len(), 0); } diff --git a/src/configuration/resolve_config.rs b/src/configuration/resolve_config.rs index 336d5a6..e1cf76a 100644 --- a/src/configuration/resolve_config.rs +++ b/src/configuration/resolve_config.rs @@ -61,6 +61,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), ignore_directive: get_value( &mut config, "ignoreDirective", diff --git a/src/configuration/types.rs b/src/configuration/types.rs index 40fcc5a..01ac80b 100644 --- a/src/configuration/types.rs +++ b/src/configuration/types.rs @@ -14,6 +14,7 @@ pub struct Configuration { pub strong_kind: StrongKind, pub unordered_list_kind: UnorderedListKind, pub heading_kind: HeadingKind, + pub unindent_code_blocks: bool, pub ignore_directive: String, pub ignore_file_directive: String, pub ignore_start_directive: String, diff --git a/src/generation/generate.rs b/src/generation/generate.rs index 90a958f..75b19bf 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -374,6 +374,11 @@ 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 = 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 }; diff --git a/tests/specs/CodeBlocks/CodeBlocks_All.txt b/tests/specs/CodeBlocks/CodeBlocks_All.txt index 0f182d6..25261c8 100644 --- a/tests/specs/CodeBlocks/CodeBlocks_All.txt +++ b/tests/specs/CodeBlocks/CodeBlocks_All.txt @@ -233,13 +233,3 @@ other_formatted_77_formatted_80 ```format ignore other_formatted_77_formatted_80 ``` - -!! Preserve leading spaces !! -``` - one -``` - -[expect] -``` - one -``` diff --git a/tests/specs/CodeBlocks/CodeBlocks_UnindentCodeBlocks_Disabled.txt b/tests/specs/CodeBlocks/CodeBlocks_UnindentCodeBlocks_Disabled.txt new file mode 100644 index 0000000..0d796ff --- /dev/null +++ b/tests/specs/CodeBlocks/CodeBlocks_UnindentCodeBlocks_Disabled.txt @@ -0,0 +1,10 @@ +~~ unindentCodeBlocks: false ~~ +!! Preserve leading spaces !! +``` + one +``` + +[expect] +``` + one +``` From 6f572d7ceff3013f08e29ea2aeb82d6b2d89ad81 Mon Sep 17 00:00:00 2001 From: "Bryan A. Jones" Date: Sat, 23 May 2026 11:36:34 +0500 Subject: [PATCH 3/3] Fix: improve unindented code block test. --- .../CodeBlocks/CodeBlocks_UnindentCodeBlocks_Disabled.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/specs/CodeBlocks/CodeBlocks_UnindentCodeBlocks_Disabled.txt b/tests/specs/CodeBlocks/CodeBlocks_UnindentCodeBlocks_Disabled.txt index 0d796ff..1b1fe22 100644 --- a/tests/specs/CodeBlocks/CodeBlocks_UnindentCodeBlocks_Disabled.txt +++ b/tests/specs/CodeBlocks/CodeBlocks_UnindentCodeBlocks_Disabled.txt @@ -1,10 +1,10 @@ ~~ unindentCodeBlocks: false ~~ !! Preserve leading spaces !! ``` - one + one ``` [expect] ``` - one + one ```