From 0e1b39356107c1ea6de6d92a4e4915bd479cb9f3 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 | 58 +++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index 6a5ea3748..79540fa48 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(