Skip to content

Commit

Permalink
feat: Add a configuration option to treat level 1 headings as title
Browse files Browse the repository at this point in the history
Previously, "level 1 heading = title" was the default. Now there's a toggle for this.

The config option is not wired yet. This will be done separately.
  • Loading branch information
artempyanykh committed Nov 22, 2024
1 parent 6989b93 commit 59cd523
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
22 changes: 19 additions & 3 deletions Marksman/Config.fs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ type Config = {
caCreateMissingFileEnable: option<bool>
coreMarkdownFileExtensions: option<array<string>>
coreTextSync: option<TextSync>
coreTitleFromHeading: option<bool>
coreIncrementalReferences: option<bool>
coreParanoid: option<bool>
complWikiStyle: option<ComplWikiStyle>
Expand All @@ -139,6 +140,7 @@ type Config = {
caCreateMissingFileEnable = Some true
coreMarkdownFileExtensions = Some [| "md"; "markdown" |]
coreTextSync = Some Full
coreTitleFromHeading = Some true
coreIncrementalReferences = Some false
coreParanoid = Some false
complWikiStyle = Some TitleSlug
Expand All @@ -150,6 +152,7 @@ type Config = {
caCreateMissingFileEnable = None
coreMarkdownFileExtensions = None
coreTextSync = None
coreTitleFromHeading = None
coreIncrementalReferences = None
coreParanoid = None
complWikiStyle = None
Expand All @@ -176,6 +179,11 @@ type Config = {
|> Option.orElse Config.Default.coreTextSync
|> Option.get

member this.CoreTitleFromHeading() =
this.coreTitleFromHeading
|> Option.orElse Config.Default.coreTitleFromHeading
|> Option.get

member this.CoreIncrementalReferences() =
this.coreIncrementalReferences
|> Option.orElse Config.Default.coreIncrementalReferences
Expand All @@ -187,9 +195,13 @@ type Config = {
|> Option.get

member this.ComplWikiStyle() =
this.complWikiStyle
|> Option.orElse Config.Default.complWikiStyle
|> Option.get
match this.complWikiStyle with
| Some x -> x
| None ->
if this.CoreTitleFromHeading() then
Option.get Config.Default.complWikiStyle
else
ComplWikiStyle.FileStem

member this.ComplCandidates() =
this.complCandidates
Expand All @@ -209,6 +221,8 @@ let private configOfTable (table: TomlTable) : LookupResult<Config> =
let! coreTextSync = getFromTableOpt<string> table [] [ "core"; "text_sync" ]
let coreTextSync = coreTextSync |> Option.bind TextSync.ofStringOpt

let! coreTitleFromHeading = getFromTableOpt<bool> table [] [ "core"; "title_from_heading" ]

let! coreIncrementalReferences =
getFromTableOpt<bool> table [] [ "core"; "incremental_references" ]

Expand Down Expand Up @@ -239,6 +253,7 @@ let private configOfTable (table: TomlTable) : LookupResult<Config> =
caCreateMissingFileEnable = caCreateMissingFileEnable
coreMarkdownFileExtensions = coreMarkdownFileExtensions
coreTextSync = coreTextSync
coreTitleFromHeading = coreTitleFromHeading
coreIncrementalReferences = coreIncrementalReferences
coreParanoid = coreParanoid
complWikiStyle = complWikiStyle
Expand All @@ -258,6 +273,7 @@ module Config =
hi.coreMarkdownFileExtensions
|> Option.orElse low.coreMarkdownFileExtensions
coreTextSync = hi.coreTextSync |> Option.orElse low.coreTextSync
coreTitleFromHeading = hi.coreTitleFromHeading |> Option.orElse low.coreTitleFromHeading
coreIncrementalReferences =
hi.coreIncrementalReferences
|> Option.orElse low.coreIncrementalReferences
Expand Down
16 changes: 16 additions & 0 deletions Tests/ConfigTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Marksman.ConfigTests

open System.IO
open System.Reflection
open FSharpPlus.Data.Validation
open Xunit

open Marksman.Config
Expand Down Expand Up @@ -189,3 +190,18 @@ let testDefault () =
let content = using (new StreamReader(content)) (fun f -> f.ReadToEnd())
let parsed = Config.tryParse content
Assert.Equal(Some Config.Default, parsed)

[<Fact>]
let testDefault_titleVsCompletionStyle () =
let content =
"""
[core]
title_from_heading = false
"""

let actual =
Config.tryParse content
|> Option.defaultWith (fun () -> failwith "Expected a successful parse")

Assert.False(actual.CoreTitleFromHeading())
Assert.Equal(ComplWikiStyle.FileStem, actual.ComplWikiStyle())
5 changes: 5 additions & 0 deletions Tests/default.marksman.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ markdown.file_extensions = ["md", "markdown"]
# sync which result in slightly correpted state and are really hard
# to diagnose.
text_sync = "full"
# When set to true, level 1 headings will be treated as document titles
# (this includes an assumption of having a single title in the document).
# Setting this to false automatically changes the default wiki link
# completion style to a file-based one.
title_from_heading = true
# Use incremental resolution of project-wide references.
# This is much more efficient but is currently experimental
incremental_references = false
Expand Down

0 comments on commit 59cd523

Please sign in to comment.