Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a configuration option to treat level 1 headings as title #366

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading