Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
67 changes: 67 additions & 0 deletions internal/docgen/mintlify.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ func (MintlifyFormatter) Synopsis(meta CommandMeta) string {
func (MintlifyFormatter) FlagsSection(flags, inherited string) string {
flags = linkifyKosliDocsURLs(flags)
flags = escapeMintlifyProse(flags)
flags = backtickFlags(flags)
Comment thread
mbevc1 marked this conversation as resolved.
inherited = linkifyKosliDocsURLs(inherited)
inherited = escapeMintlifyProse(inherited)
inherited = backtickFlags(inherited)
var b strings.Builder
if flags != "" {
b.WriteString("## Flags\n")
Expand Down Expand Up @@ -170,6 +172,71 @@ var htmlTags = map[string]bool{
// single quotes are reserved for example/placeholder URLs.
var singleQuotedURLPattern = regexp.MustCompile(`'(https?://[^\s']+)'`)

// flagPattern matches the body of a CLI flag like --name or -x. The caller
// must check the preceding character to ensure it is a real flag boundary
// (start-of-string or a non-word, non-slash, non-dash character).
var flagPattern = regexp.MustCompile(`^-{1,2}[a-zA-Z][\w-]*`)
Comment thread
mbevc1 marked this conversation as resolved.

// backtickFlags wraps every CLI flag mention (--flag, -x) in backticks so
// Mintlify's smart-typography renderer does not turn `--` into an em dash
// inside Markdown table cells. Idempotent: flags already inside inline-code
// spans or fenced code blocks are left alone.
func backtickFlags(s string) string {
parts := strings.Split(s, "```")
for i := 0; i < len(parts); i += 2 {
Comment thread
mbevc1 marked this conversation as resolved.
parts[i] = backtickFlagsFragment(parts[i])
}
return strings.Join(parts, "```")
}

func isFlagBoundaryChar(c byte) bool {
// A flag must be preceded by start-of-string or a character that is not
// a word char, slash, dash, or backtick.
if c == '/' || c == '-' || c == '`' {
return false
}
if c == '_' {
return false
}
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') {
return false
}
return true
}
Comment thread
mbevc1 marked this conversation as resolved.

func backtickFlagsFragment(s string) string {
var b strings.Builder
i := 0
for i < len(s) {
if s[i] == '`' {
b.WriteByte(s[i])
i++
for i < len(s) && s[i] != '`' {
b.WriteByte(s[i])
i++
}
if i < len(s) {
b.WriteByte(s[i])
i++
}
continue
}
boundary := i == 0 || isFlagBoundaryChar(s[i-1])
if boundary && (s[i] == '-') {
if loc := flagPattern.FindStringIndex(s[i:]); loc != nil {
b.WriteByte('`')
b.WriteString(s[i : i+loc[1]])
b.WriteByte('`')
i += loc[1]
continue
}
}
b.WriteByte(s[i])
i++
}
return b.String()
}

func escapeProseFragment(s string) string {
// Escape curly braces: {expr} -> \{expr\}
s = strings.ReplaceAll(s, "{", "\\{")
Expand Down
84 changes: 84 additions & 0 deletions internal/docgen/mintlify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,90 @@ func TestEscapeMintlifyProse(t *testing.T) {
}
}

func TestBacktickFlags(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "double-dash flag at start",
input: "--api-token",
want: "`--api-token`",
},
{
name: "single-dash flag at start",
input: "-x",
want: "`-x`",
},
{
name: "flag in middle of sentence",
input: "use --api-token to authenticate",
want: "use `--api-token` to authenticate",
},
{
name: "flag in table cell",
input: "| --api-token | API token |",
want: "| `--api-token` | API token |",
},
{
name: "two adjacent flags",
input: "--foo --bar",
want: "`--foo` `--bar`",
},
{
name: "comma-separated flags",
input: "--foo,--bar",
want: "`--foo`,`--bar`",
},
{
name: "already-backticked flag is left alone",
input: "use `--api-token` here",
want: "use `--api-token` here",
},
{
name: "hyphenated word is not a flag",
input: "this is built-in behaviour",
want: "this is built-in behaviour",
},
{
name: "code fence content is untouched",
input: "see ```\nkosli foo --bar\n``` for example",
want: "see ```\nkosli foo --bar\n``` for example",
},
{
name: "flag with hyphen in name",
input: "use --jira-base-url here",
want: "use `--jira-base-url` here",
},
{
name: "short flag in middle",
input: "use -h for help",
want: "use `-h` for help",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := backtickFlags(tt.input)
if got != tt.want {
t.Errorf("backtickFlags(%q):\ngot: %q\nwant: %q", tt.input, got, tt.want)
}
})
}
Comment thread
mbevc1 marked this conversation as resolved.
}

func TestMintlifyFlagsSectionBackticksFlags(t *testing.T) {
f := MintlifyFormatter{}
flags := "| --api-token string | required (default $KOSLI_API_TOKEN) |\n"
got := f.FlagsSection(flags, "")
if !strings.Contains(got, "`--api-token`") {
t.Errorf("expected --api-token to be backticked, got:\n%s", got)
}
if strings.Contains(got, "| --api-token string |") {
t.Errorf("expected bare --api-token to be replaced, got:\n%s", got)
}
Comment thread
mbevc1 marked this conversation as resolved.
}

func TestSanitizeDescription(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading