From 606393f223af8aa05a36348de609ff8f1675fbca Mon Sep 17 00:00:00 2001 From: Tomas Petricek Date: Thu, 1 Oct 2015 21:29:44 -0400 Subject: [PATCH] Do not require newline before fenced code block (fix #342) --- src/FSharp.Markdown/MarkdownParser.fs | 10 ++++++++-- tests/FSharp.Markdown.Tests/Markdown.fs | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/FSharp.Markdown/MarkdownParser.fs b/src/FSharp.Markdown/MarkdownParser.fs index 8518175b0..352239ce5 100644 --- a/src/FSharp.Markdown/MarkdownParser.fs +++ b/src/FSharp.Markdown/MarkdownParser.fs @@ -285,7 +285,7 @@ let (|HorizontalRule|_|) (line:string) = loop (0, 0, 0) 0 /// Recognizes a code block - lines starting with four spaces (including blank) -let (|CodeBlock|_|) = function +let (|NestedCodeBlock|_|) = function | Lines.TakeStartingWithOrBlank " " (Lines.TrimBlank lines, rest) when lines <> [] -> let code = [ for l in lines -> @@ -293,6 +293,10 @@ let (|CodeBlock|_|) = function elif l.Length > 4 then l.Substring(4, l.Length - 4) else l ] Some((if rest.IsEmpty then code else code @ [""]), rest, "", "") + | _ -> None + +/// Recognizes a fenced code block - starting and ending with ``` +let (|FencedCodeBlock|_|) = function | String.StartsWithTrim "```" header :: lines -> let code, rest = lines |> List.partitionUntil (fun line -> line.Contains "```") // langString is the part after ``` and ignoredString is the rest until the line ends. @@ -508,6 +512,7 @@ let (|BlockquoteStart|_|) (line:string) = let (|TakeParagraphLines|_|) input = match List.partitionWhileLookahead (function | Heading _ -> false + | FencedCodeBlock _ -> false | BlockquoteStart _::_ -> false | String.WhiteSpace::_ -> false | _ -> true) input with @@ -565,7 +570,8 @@ let rec parseParagraphs (ctx:ParsingContext) lines = seq { | LinkDefinition ((key, link), Lines.TrimBlankStart lines) -> ctx.Links.Add(key, getLinkAndTitle link) yield! parseParagraphs ctx lines - | CodeBlock(code, Lines.TrimBlankStart lines, langString, ignoredLine) -> + | NestedCodeBlock(code, Lines.TrimBlankStart lines, langString, ignoredLine) + | FencedCodeBlock(code, Lines.TrimBlankStart lines, langString, ignoredLine) -> yield CodeBlock(code |> String.concat ctx.Newline, langString, ignoredLine) yield! parseParagraphs ctx lines | Blockquote(body, Lines.TrimBlankStart rest) -> diff --git a/tests/FSharp.Markdown.Tests/Markdown.fs b/tests/FSharp.Markdown.Tests/Markdown.fs index 37e3fde9c..6124dcf26 100644 --- a/tests/FSharp.Markdown.Tests/Markdown.fs +++ b/tests/FSharp.Markdown.Tests/Markdown.fs @@ -243,6 +243,12 @@ let ``Transform github flavored code blocks with whitespace correctly``() = Markdown.TransformHtml doc |> shouldEqual expected +[] +let ``Fenced code blocks do not require newline`` () = + let doc = "> This is an annnotation\r\n> ```vb\r\n> Module\r\n> ```" + let actual = Markdown.TransformHtml(doc) + actual |> should contain "] let ``Transform code spans correctly``() = let doc = "HTML contains the `` tag";