diff --git a/docs/doc_config.md b/docs/doc_config.md index cb779cb..61937bf 100644 --- a/docs/doc_config.md +++ b/docs/doc_config.md @@ -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. @@ -29,9 +33,13 @@ 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 | @@ -39,8 +47,15 @@ Mode mode define. |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) - diff --git a/struct_info.go b/struct_info.go index bf0c0d5..0cedd83 100644 --- a/struct_info.go +++ b/struct_info.go @@ -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"` diff --git a/struct_markdown.go b/struct_markdown.go index 84382c5..5a46149 100644 --- a/struct_markdown.go +++ b/struct_markdown.go @@ -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" + @@ -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) } @@ -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") @@ -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 +}