diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index 6a5ea3748..67a62dcf6 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -244,9 +244,65 @@ let private WrapParagraph (text: string) (maxCharsPerLine: int) : string = processWords String.Empty String.Empty words +let ExtractParagraphs(text: string) = + let lines = text.Split(Environment.NewLine) |> Seq.toList + let codeBlockStartOrEndToken = "```" + + let rec processLines + (remainingLines: List) + (currentParagraph: List) + (paragraphs: List) + (insideCodeBlock: bool) + = + match remainingLines with + | [] -> + if not currentParagraph.IsEmpty then + String.Join(Environment.NewLine, List.rev currentParagraph) + :: paragraphs + else + paragraphs + | line :: rest -> + if String.IsNullOrWhiteSpace line then + if insideCodeBlock then + processLines + rest + (line :: currentParagraph) + paragraphs + insideCodeBlock + else if not currentParagraph.IsEmpty then + let newParagraph = + String.Join( + Environment.NewLine, + List.rev currentParagraph + ) + + processLines + rest + List.Empty + (newParagraph :: paragraphs) + insideCodeBlock + else + processLines rest List.Empty paragraphs insideCodeBlock + elif line.Trim().StartsWith codeBlockStartOrEndToken then + let newInsideCodeBlock = not insideCodeBlock + + processLines + rest + (line :: currentParagraph) + paragraphs + newInsideCodeBlock + else + processLines + rest + (line :: currentParagraph) + paragraphs + insideCodeBlock + + List.rev <| processLines lines List.Empty List.Empty false + let WrapText (text: string) (maxCharsPerLine: int) : string = let wrappedParagraphs = - text.Split $"{Environment.NewLine}{Environment.NewLine}" + ExtractParagraphs text |> Seq.map(fun paragraph -> WrapParagraph paragraph maxCharsPerLine) String.Join(