diff --git a/deployment/schema.json b/deployment/schema.json index 13487b5..815dada 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." + }] + }, "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", @@ -137,6 +149,9 @@ "headingKind": { "$ref": "#/definitions/headingKind" }, + "unindentCodeBlocks": { + "$ref": "#/definitions/unindentCodeBlocks" + }, "listIndentKind": { "$ref": "#/definitions/listIndentKind" }, diff --git a/src/configuration/builder.rs b/src/configuration/builder.rs index dbc7b04..bda9401 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 style of list indentation to use. /// Default: `ListIndentKind::CommonMark` pub fn list_indent_kind(&mut self, value: ListIndentKind) -> &mut Self { @@ -153,6 +159,7 @@ 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") @@ -160,7 +167,7 @@ mod tests { .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); } diff --git a/src/configuration/resolve_config.rs b/src/configuration/resolve_config.rs index f49dbd5..87562a2 100644 --- a/src/configuration/resolve_config.rs +++ b/src/configuration/resolve_config.rs @@ -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, diff --git a/src/configuration/types.rs b/src/configuration/types.rs index 88dd462..68f0099 100644 --- a/src/configuration/types.rs +++ b/src/configuration/types.rs @@ -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, diff --git a/src/generation/generate.rs b/src/generation/generate.rs index 749c113..fe085d8 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -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 }; diff --git a/tests/specs/CodeBlocks/CodeBlocks_UnindentCodeBlocks_Disabled.txt b/tests/specs/CodeBlocks/CodeBlocks_UnindentCodeBlocks_Disabled.txt new file mode 100644 index 0000000..1b1fe22 --- /dev/null +++ b/tests/specs/CodeBlocks/CodeBlocks_UnindentCodeBlocks_Disabled.txt @@ -0,0 +1,10 @@ +~~ unindentCodeBlocks: false ~~ +!! 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.