Skip to content

Commit

Permalink
Do not require newline before fenced code block (fix #342)
Browse files Browse the repository at this point in the history
  • Loading branch information
tpetricek committed Oct 2, 2015
1 parent 5d4b440 commit 606393f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/FSharp.Markdown/MarkdownParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,18 @@ 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 ->
if String.IsNullOrWhiteSpace l then ""
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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) ->
Expand Down
6 changes: 6 additions & 0 deletions tests/FSharp.Markdown.Tests/Markdown.fs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ let ``Transform github flavored code blocks with whitespace correctly``() =
Markdown.TransformHtml doc
|> shouldEqual expected

[<Test>]
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 "<pre"

[<Test>]
let ``Transform code spans correctly``() =
let doc = "HTML contains the `<blink>` tag";
Expand Down

0 comments on commit 606393f

Please sign in to comment.