From 0e9b157c6b9e522e928234f0bf09fc8ebd03d3b7 Mon Sep 17 00:00:00 2001 From: Parham Date: Thu, 3 Aug 2023 16:44:41 +0330 Subject: [PATCH] FileConventions: fix WrapText function Ignore codeblocks while splitting the text into paragraphs because there might be a codeblock with multiple paragraphs. Fixes https://github.com/nblockchain/conventions/issues/117 --- src/FileConventions/Library.fs | 54 +++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index 6a5ea3748..2b0d92489 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -244,9 +244,61 @@ let private WrapParagraph (text: string) (maxCharsPerLine: int) : string = processWords String.Empty String.Empty words +let ExtractParagraphs(text: string) = + let lines = text.Split('\n') |> Seq.toList + + let rec processLines + (remainingLines: List) + (currentParagraph: List) + (paragraphs: List) + (insideCodeBlock: bool) + = + match remainingLines with + | [] -> + if currentParagraph <> [] then + List.singleton(String.Join("\n", List.rev currentParagraph)) + @ paragraphs + else + paragraphs + | line :: rest -> + if String.IsNullOrWhiteSpace(line) then + if insideCodeBlock then + processLines + rest + (line :: currentParagraph) + paragraphs + insideCodeBlock + else if currentParagraph <> [] then + let newParagraph = + String.Join("\n", List.rev currentParagraph) + + processLines + rest + [] + (newParagraph :: paragraphs) + insideCodeBlock + else + processLines rest [] paragraphs insideCodeBlock + elif line.Trim().StartsWith("```") then + let newInsideCodeBlock = not insideCodeBlock + + processLines + rest + (line :: currentParagraph) + paragraphs + newInsideCodeBlock + else + processLines + rest + (line :: currentParagraph) + paragraphs + insideCodeBlock + + List.rev <| processLines lines [] [] 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(