TextUtility.NormalizeCodeBlock Method

Namespace
Kampute.DocToolkit.Support
Assembly
  • Kampute.DocToolkit.dll

Definition

Normalizes the text of a code block by removing leading and trailing empty lines, collapsing consecutive empty lines, and normalizing indentation.
public static string NormalizeCodeBlock(string text, int tabSize = 4)

Parameters

text string
The text to normalize.
tabSize int optional
The number of spaces to replace each tab with. Default is 4.

Returns

string
A string with normalized indentation, or an empty string if input is null or whitespace.

Remarks

This method performs the following operations:
  • Returns an empty string if the input is null or whitespace.
  • For single-line text, it trims and returns the text as-is.
  • For multi-line text, it:
    1. Converts tabs to spaces and removes trailing whitespace from each line.
    2. Determines the minimum indentation across all non-empty lines and removes it from each line.
    3. Removes leading and trailing empty lines.
    4. Collapses consecutive empty lines into a single empty line.