-2

I am completely new to F#.

I am trying to parse a markdown file which has some verbatim code sections in it, separated by the ``` blocks. These blocks neednot be the only characters in a line and

\``` this is some code ``` and this is not

is a valid line as well.

If possible, I'd appreciate if the solution uses pure Fsharp way of doing things and perhaps functional designs patterns like Active Patterns.

So far I haven't gone much further than just reading the file.

open System
open System.IO

let readFile path =
    File.ReadAllText(path)
    // File.ReadAllLines

[<EntryPoint>]
let main argv = 
    readFile "src/main.md"
        |> String.collect (fun ch -> sprintf "%c " ch)
        |> printfn "%s" 
    0

EDIT: I do not want to use any libraries. In Python, this would be a one line code; re.search('start;(.*)end', s)

Rishav Sharan
  • 2,763
  • 8
  • 39
  • 55
  • 1
    You probably got marked down because you have not showed any effort in trying something and showing your effort and asking about where you are stuck. Also have you looked at MarkDig or another MD library? – Devon Burriss Jul 20 '19 at 00:18
  • You can of course use regex in .NET as well: https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expressions I do have a solution that searches 1 char at a time and is "functional" but honestly using regex would be less code and likely faster. – Devon Burriss Jul 20 '19 at 06:45

1 Answers1

3

The best way to parse Markdown is to use a library designed to parse Markdown, such as FSharp.Formatting. It's really not a wheel you'll want to reinvent, especially if you're new to F#. Usage example:

open FSharp.Markdown

let markdownText = """
# Some document

Here is a document with a code block

```fsharp
let example = "Foo
```

Here is more text"""

let parsed = Markdown.Parse(markdownText)
printfn "%A" parsed

And since you're interested in an F#-idiomatic solution, take a look at the source for FSharp.Formatting's Markdown parser: plenty of discriminated unions, active patterns, and so on.

rmunn
  • 34,942
  • 10
  • 74
  • 105