#r "nuget: FSharp.Formatting,1.0.0" open FSharp.Formatting.Markdown open FSharp.Formatting.Common let document = """ # F# Hello world Hello world in [F#](http://fsharp.net) looks like this: printfn "Hello world!" For more see [fsharp.org][fsorg]. [fsorg]: http://fsharp.org "The F# organization." """ let parsed = Markdown.Parse(document) parsed.DefinedLinks // [fsi:val it : IDictionary =] // [fsi: dict [("fsorg", ("http://fsharp.org", Some "The F# organization."))]] // Iterate over all the paragraph elements for par in parsed.Paragraphs do match par with | Heading (size = 1; body = [ Literal (text = text) ]) -> // Recognize heading that has a simple content // containing just a literal (no other formatting) printfn "%s" text | _ -> () /// Returns all links in a specified span node let rec collectSpanLinks span = seq { match span with | DirectLink (link = url) -> yield url | IndirectLink (key = key) -> yield fst (parsed.DefinedLinks.[key]) | MarkdownPatterns.SpanLeaf _ -> () | MarkdownPatterns.SpanNode (_, spans) -> for s in spans do yield! collectSpanLinks s } /// Returns all links in the specified paragraph node let rec collectParLinks par = seq { match par with | MarkdownPatterns.ParagraphLeaf _ -> () | MarkdownPatterns.ParagraphNested (_, pars) -> for ps in pars do for p in ps do yield! collectParLinks p | MarkdownPatterns.ParagraphSpans (_, spans) -> for s in spans do yield! collectSpanLinks s } // Collect links in the entire document Seq.collect collectParLinks parsed.Paragraphs // [fsi:val it : seq =] // [fsi: seq ["http://fsharp.net"; "http://fsharp.org"]] let html = Markdown.ToHtml(parsed)