@@ -16,6 +16,7 @@ type Post = {
1616 content: string
1717}
1818
19+ let contentDir = " posts"
1920
2021let markdownPipeline =
2122 MarkdownPipelineBuilder()
@@ -32,10 +33,19 @@ let getConfig (fileContent : string) =
3233 let fileContent = fileContent.Split '\n'
3334 let fileContent = fileContent |> Array.skip 1 //First line must be ---
3435 let indexOfSeperator = fileContent |> Array.findIndex isSeparator
36+ let splitKey ( line : string ) =
37+ let seperatorIndex = line.IndexOf( ':' )
38+ if seperatorIndex > 0 then
39+ let key = line.[.. seperatorIndex - 1 ]. Trim() .ToLower()
40+ let value = line.[ seperatorIndex + 1 ..]. Trim()
41+ Some( key, value)
42+ else
43+ None
3544 fileContent
3645 |> Array.splitAt indexOfSeperator
3746 |> fst
38- |> String.concat " \n "
47+ |> Seq.choose splitKey
48+ |> Map.ofSeq
3949
4050///`fileContent` - content of page to parse. Usually whole content of `.md` file
4151///returns HTML version of content of the page
@@ -54,36 +64,22 @@ let trimString (str : string) =
5464let loadFile n =
5565 let text = System.IO.File.ReadAllText n
5666
57- let config = ( getConfig text) .Split( '\n' ) |> List.ofArray
58-
67+ let config = getConfig text
5968 let content = getContent text
6069
61- let file = System.IO.Path.Combine( " posts" , ( n |> System.IO.Path.GetFileNameWithoutExtension) + " .md" ) .Replace( " \\ " , " /" )
62- let link = " /" + System.IO.Path.Combine( " posts" , ( n |> System.IO.Path.GetFileNameWithoutExtension) + " .html" ) .Replace( " \\ " , " /" )
63-
64- let title = config |> List.find ( fun n -> n.ToLower() .StartsWith " title" ) |> fun n -> n.Split( ':' ).[ 1 ] |> trimString
65-
66- let author =
67- try
68- config |> List.tryFind ( fun n -> n.ToLower() .StartsWith " author" ) |> Option.map ( fun n -> n.Split( ':' ).[ 1 ] |> trimString)
69- with
70- | _ -> None
70+ let file = System.IO.Path.Combine( contentDir, ( n |> System.IO.Path.GetFileNameWithoutExtension) + " .md" ) .Replace( " \\ " , " /" )
71+ let link = " /" + System.IO.Path.Combine( contentDir, ( n |> System.IO.Path.GetFileNameWithoutExtension) + " .html" ) .Replace( " \\ " , " /" )
7172
72- let published =
73- try
74- config |> List.tryFind ( fun n -> n.ToLower() .StartsWith " published" ) |> Option.map ( fun n -> n.Split( ':' ).[ 1 ] |> trimString |> System.DateTime.Parse)
75- with
76- | _ -> None
73+ let title = config |> Map.find " title" |> trimString
74+ let author = config |> Map.tryFind " author" |> Option.map trimString
75+ let published = config |> Map.tryFind " published" |> Option.map ( trimString >> System.DateTime.Parse)
7776
7877 let tags =
79- try
80- let x =
81- config
82- |> List.tryFind ( fun n -> n.ToLower() .StartsWith " tags" )
83- |> Option.map ( fun n -> n.Split( ':' ).[ 1 ] |> trimString |> fun n -> n.Split ',' |> Array.toList )
84- defaultArg x []
85- with
86- | _ -> []
78+ let tagsOpt =
79+ config
80+ |> Map.tryFind " tags"
81+ |> Option.map ( trimString >> fun n -> n.Split ',' |> Array.toList)
82+ defaultArg tagsOpt []
8783
8884 { file = file
8985 link = link
@@ -94,7 +90,7 @@ let loadFile n =
9490 content = content }
9591
9692let loader ( projectRoot : string ) ( siteContent : SiteContents ) =
97- let postsPath = System.IO.Path.Combine( projectRoot, " posts " )
93+ let postsPath = System.IO.Path.Combine( projectRoot, contentDir )
9894 System.IO.Directory.GetFiles postsPath
9995 |> Array.filter ( fun n -> n.EndsWith " .md" )
10096 |> Array.map loadFile
0 commit comments