mirror of
https://github.com/kovetskiy/mark.git
synced 2026-05-02 13:22:40 +00:00
feat: add support for --content-appearance
This commit is contained in:
committed by
Manuel Rüger
parent
d2717f031d
commit
9516939c7d
@@ -116,7 +116,7 @@ func processFile(
|
||||
|
||||
parents := strings.Split(cmd.String("parents"), cmd.String("parents-delimiter"))
|
||||
|
||||
meta, markdown, err := metadata.ExtractMeta(markdown, cmd.String("space"), cmd.Bool("title-from-h1"), cmd.Bool("title-from-filename"), file, parents, cmd.Bool("title-append-generated-hash"))
|
||||
meta, markdown, err := metadata.ExtractMeta(markdown, cmd.String("space"), cmd.Bool("title-from-h1"), cmd.Bool("title-from-filename"), file, parents, cmd.Bool("title-append-generated-hash"), cmd.String("content-appearance"))
|
||||
if err != nil {
|
||||
fatalErrorHandler.Handle(err, "unable to extract metadata from file %q", file)
|
||||
return nil
|
||||
|
||||
@@ -12,8 +12,9 @@ func runWithArgs(args []string) error {
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{Name: "title-from-h1"},
|
||||
&cli.BoolFlag{Name: "title-from-filename"},
|
||||
&cli.StringFlag{Name: "content-appearance"},
|
||||
},
|
||||
Before: CheckMutuallyExclusiveTitleFlags,
|
||||
Before: CheckFlags,
|
||||
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||
return nil
|
||||
},
|
||||
@@ -50,3 +51,26 @@ func TestCheckMutuallyExclusiveTitleFlags(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestContentAppearanceFlagValidation(t *testing.T) {
|
||||
t.Run("fixed is accepted", func(t *testing.T) {
|
||||
err := runWithArgs([]string{"cmd", "--content-appearance", "fixed"})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("full-width is accepted", func(t *testing.T) {
|
||||
err := runWithArgs([]string{"cmd", "--content-appearance", "full-width"})
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("invalid value is rejected", func(t *testing.T) {
|
||||
err := runWithArgs([]string{"cmd", "--content-appearance", "nope"})
|
||||
if err == nil {
|
||||
t.Errorf("expected error, got nil")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package util
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
altsrc "github.com/urfave/cli-altsrc/v3"
|
||||
altsrctoml "github.com/urfave/cli-altsrc/v3/toml"
|
||||
@@ -164,6 +165,15 @@ var Flags = []cli.Flag{
|
||||
Usage: "The delimiter used for the parents list",
|
||||
Sources: cli.NewValueSourceChain(cli.EnvVar("MARK_PARENTS_DELIMITER"), altsrctoml.TOML("parents-delimiter", altsrc.NewStringPtrSourcer(&filename))),
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "content-appearance",
|
||||
Value: "",
|
||||
Usage: "default content appearance for pages without a Content-Appearance header. Possible values: full-width, fixed.",
|
||||
Sources: cli.NewValueSourceChain(
|
||||
cli.EnvVar("MARK_CONTENT_APPEARANCE"),
|
||||
altsrctoml.TOML("content-appearance", altsrc.NewStringPtrSourcer(&filename)),
|
||||
),
|
||||
},
|
||||
&cli.FloatFlag{
|
||||
Name: "mermaid-scale",
|
||||
Value: 1.0,
|
||||
@@ -204,10 +214,24 @@ var Flags = []cli.Flag{
|
||||
},
|
||||
}
|
||||
|
||||
// CheckMutuallyExclusiveTitleFlags checks if both title-from-h1 and title-from-filename are set
|
||||
func CheckMutuallyExclusiveTitleFlags(context context.Context, command *cli.Command) (context.Context, error) {
|
||||
// CheckFlags validates combinations and values of global flags.
|
||||
func CheckFlags(context context.Context, command *cli.Command) (context.Context, error) {
|
||||
if command.Bool("title-from-h1") && command.Bool("title-from-filename") {
|
||||
return context, errors.New("flags --title-from-h1 and --title-from-filename are mutually exclusive. Please specify only one")
|
||||
}
|
||||
|
||||
contentAppearance := command.String("content-appearance")
|
||||
if contentAppearance != "" {
|
||||
switch contentAppearance {
|
||||
case "full-width", "fixed":
|
||||
// ok
|
||||
default:
|
||||
return context, fmt.Errorf(
|
||||
"invalid value for --content-appearance: %q (expected: full-width or fixed)",
|
||||
contentAppearance,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return context, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user