@@ -40,7 +40,7 @@ module internal Formatting =
4040 mdlinkResolver = mdlinkResolver
4141 )
4242 | OutputKind.Html ->
43- let sb = new System.Text.StringBuilder()
43+ let sb = System.Text.StringBuilder()
4444 use wr = new StringWriter( sb)
4545
4646 HtmlFormatting.formatAsHtml
@@ -105,11 +105,17 @@ module internal Formatting =
105105 | LiterateSource.Markdown text -> text
106106 | LiterateSource.Script snippets ->
107107 [ for Snippet(_ name, lines) in snippets do
108- for ( Line( line, _) ) in lines do
108+ for Line( line, _) in lines do
109109 yield line ]
110110 |> String.concat " \n "
111111
112- let transformDocument ( doc : LiterateDocument ) ( outputPath : string ) ctx =
112+ let transformDocument
113+ // This array was sorted in BuildCommand.fs
114+ ( filesWithFrontMatter : FrontMatterFile array )
115+ ( doc : LiterateDocument )
116+ ( outputPath : string )
117+ ctx
118+ =
113119
114120 let findInFrontMatter key =
115121 match doc.Paragraphs with
@@ -126,10 +132,14 @@ module internal Formatting =
126132 None)
127133 | _ -> None
128134
129- let category = findInFrontMatter " category"
135+ let mkValidIndex ( value : string ) =
136+ match System.Int32.TryParse value with
137+ | true , i -> Some i
138+ | false , _ -> None
130139
131- let categoryIndex = findInFrontMatter " categoryindex"
132- let index = findInFrontMatter " index"
140+ let category = findInFrontMatter " category"
141+ let categoryIndex = findInFrontMatter " categoryindex" |> Option.bind mkValidIndex
142+ let index = findInFrontMatter " index" |> Option.bind mkValidIndex
133143 let titleFromFrontMatter = findInFrontMatter " title"
134144
135145 // If we want to include the source code of the script, then process
@@ -167,10 +177,62 @@ module internal Formatting =
167177 // Replace all special elements with ordinary Html/Latex Markdown
168178 let doc = Transformations.replaceLiterateParagraphs ctx doc
169179
180+ // construct previous and next urls
181+ let nextPreviousPageSubstitutions =
182+ let getLinksFromCurrentPageIdx currentPageIdx =
183+ match currentPageIdx with
184+ | None -> []
185+ | Some currentPageIdx ->
186+ let previousPage =
187+ filesWithFrontMatter
188+ |> Array.tryItem ( currentPageIdx - 1 )
189+ |> Option.bind ( fun { FileName = fileName } ->
190+ ctx.MarkdownDirectLinkResolver fileName
191+ |> Option.map ( fun link -> ParamKeys.`` fsdocs-previous-page-link `` , link))
192+ |> Option.toList
193+
194+ let nextPage =
195+ filesWithFrontMatter
196+ |> Array.tryItem ( currentPageIdx + 1 )
197+ |> Option.bind ( fun { FileName = fileName } ->
198+ ctx.MarkdownDirectLinkResolver fileName
199+ |> Option.map ( fun link -> ParamKeys.`` fsdocs-next-page-link `` , link))
200+ |> Option.toList
201+
202+ previousPage @ nextPage
203+
204+ match index, categoryIndex with
205+ | None, None
206+ | None, Some _ ->
207+ // Typical uses case here is the main index page.
208+ // If there is no frontmatter there, we want to propose the first available page
209+ filesWithFrontMatter
210+ |> Array.tryHead
211+ |> Option.bind ( fun { FileName = fileName } ->
212+ ctx.MarkdownDirectLinkResolver fileName
213+ |> Option.map ( fun link -> ParamKeys.`` fsdocs-next-page-link `` , link))
214+ |> Option.toList
215+
216+ | Some currentPageIdx, None ->
217+ let currentPageIdx =
218+ filesWithFrontMatter
219+ |> Array.tryFindIndex ( fun { Index = idx } -> idx = currentPageIdx)
220+
221+ getLinksFromCurrentPageIdx currentPageIdx
222+ | Some currentPageIdx, Some currentCategoryIdx ->
223+ let currentPageIdx =
224+ filesWithFrontMatter
225+ |> Array.tryFindIndex ( fun { Index = idx ; CategoryIndex = cIdx } ->
226+ cIdx = currentCategoryIdx && idx = currentPageIdx)
227+
228+ getLinksFromCurrentPageIdx currentPageIdx
229+
170230 let substitutions0 =
171- [ ParamKeys.`` fsdocs-page-title `` , pageTitle; ParamKeys.`` fsdocs-page-source `` , doc.SourceFile ]
172- @ ctx.Substitutions
173- @ sourceSubstitutions
231+ [ yield ParamKeys.`` fsdocs-page-title `` , pageTitle
232+ yield ParamKeys.`` fsdocs-page-source `` , doc.SourceFile
233+ yield ! ctx.Substitutions
234+ yield ! sourceSubstitutions
235+ yield ! nextPreviousPageSubstitutions ]
174236
175237 let formattedDocument =
176238 format
0 commit comments