Skip to content

Commit

Permalink
feat(markdown): sort types
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtian committed Oct 28, 2022
1 parent 9e1e6e5 commit 5b2643f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 24 deletions.
23 changes: 19 additions & 4 deletions docs/doc_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ Config doc.
|test3.[].[2]|string|true|||
|C.[]|any|true|||

## Test1
## ext.Hook
Hook hook config.

| Key | Type | Require | Default | Describe |
|----------|----------|-----|------------------|--------------|
|a|string|true|||
|name|string|false|example|hook name.|
|commands.[]|string|true||command list.|
|envs.{string}|string|true||env key map.|
|mode|[Mode](#ext.Mode)|true|1|run mode.|

## ext.Hook
Hook hook config.
Expand All @@ -29,18 +33,29 @@ Hook hook config.
|envs.{string}|string|true||env key map.|
|mode|[Mode](#ext.Mode)|true|1|run mode.|

## Test1
| Key | Type | Require | Default | Describe |
|----------|----------|-----|------------------|--------------|
|a|string|true|||

## ext.Mode
**Type:** int

Mode mode define.

| Enum Value | Describe |
|----------|--------------|
|1|mode q.|
|2|mode a.|

## ext.Mode
**Type:** int
Mode mode define.

| Enum Value | Describe |
|----------|--------------|
|1|mode q.|
|2|mode a.|

---
**github.com/eleztian/type2md/test.Config**
GENERATED BY THE COMMAND [type2md](https://github.com/eleztian/type2md)

1 change: 1 addition & 0 deletions struct_info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

type StructInfo struct {
Name string `json:"name"`
Describe string `json:"describe"`
Fields []FieldInfo `json:"fields"`
Enums *EnumInfo `json:"enums"`
Expand Down
68 changes: 48 additions & 20 deletions struct_markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ func (mk *Markdown) Generate(data map[string]StructInfo) []byte {

buf.WriteString("# " + mk.Title + "\n")

types := make([]string, 0)
tpMap := make(map[string]struct{})
genStruct := func(structInfo StructInfo) {
buf.WriteString(structInfo.Describe)
buf.WriteString("\n")
if len(structInfo.Describe) != 0 {
buf.WriteString(structInfo.Describe)
buf.WriteString("\n")
}

buf.WriteString(
"| Key | Type | Require | Default | Describe |\n" +
Expand All @@ -31,6 +35,10 @@ func (mk *Markdown) Generate(data map[string]StructInfo) []byte {
buf.WriteString("|")
if item.Reference != "" {
buf.WriteString(fmt.Sprintf("[%s](#%s)", item.Type, mk.ObjTitleFunc(item.Reference, item.Type)))
key := genKey(item.Reference, item.Type)
if _, ok := tpMap[key]; !ok {
types = append(types, key)
}
} else {
buf.WriteString(item.Type)
}
Expand Down Expand Up @@ -61,9 +69,11 @@ func (mk *Markdown) Generate(data map[string]StructInfo) []byte {
}

genEnums := func(enums *EnumInfo, desc string) {
buf.WriteString(fmt.Sprintf("**Type:** %s\n\n", enums.Type))
buf.WriteString(desc)
buf.WriteString("\n")
buf.WriteString(fmt.Sprintf("**Type:** %s\n", enums.Type))
if len(desc) != 0 {
buf.WriteString(desc)
buf.WriteString("\n")
}

if len(enums.Names) != 0 {
buf.WriteString("| Enum Value | Describe |\n")
Expand All @@ -84,32 +94,50 @@ func (mk *Markdown) Generate(data map[string]StructInfo) []byte {
delete(data, mk.MainStructName)
}

for name, structInfo := range data {
var (
modPath string
typeName string
)
lastIdx := strings.LastIndex(name, ".")
if lastIdx < 0 {
typeName = name
} else {
typeName = name[lastIdx+1:]
modPath = name[:lastIdx]
for idx := 0; idx < len(types); idx++ {
name := types[idx]
structInfo, ok := data[name]
if !ok {
continue
}
modPath, typeName := parseKey(name)

buf.WriteString(fmt.Sprintf("\n## %s\n", mk.ObjTitleFunc(modPath, typeName)))
if structInfo.Enums != nil {
buf.WriteString(fmt.Sprintf("\n## %s\n", mk.ObjTitleFunc(modPath, typeName)))
genEnums(structInfo.Enums, structInfo.Describe)
} else {
buf.WriteString(fmt.Sprintf("\n## %s\n", mk.ObjTitleFunc(modPath, typeName)))
genStruct(structInfo)
}
}

buf.WriteString(fmt.Sprintf("\n\n---\n"+
buf.WriteString(fmt.Sprintf("\n---\n"+
"**%s**\n"+
"GENERATED BY THE COMMAND [type2md](https://github.com/eleztian/type2md)\n\n",
"GENERATED BY THE COMMAND [type2md](https://github.com/eleztian/type2md)\n",
mk.MainStructName))

return buf.Bytes()
}

func parseKey(name string) (string, string) {
var (
modPath string
typeName string
)
lastIdx := strings.LastIndex(name, ".")
if lastIdx < 0 {
typeName = name
} else {
typeName = name[lastIdx+1:]
modPath = name[:lastIdx]
}

return modPath, typeName
}

func genKey(modPath, typeName string) string {
if modPath == "" {
return typeName
}

return modPath + "." + typeName
}

0 comments on commit 5b2643f

Please sign in to comment.