Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from Code-Hex/add/readme-toc
Browse files Browse the repository at this point in the history
Add readme toc
  • Loading branch information
Code-Hex authored Mar 30, 2021
2 parents 6ae8e1a + 633c9c2 commit a82102c
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ This todo list is written the order of priority.
- https://github.com/Code-Hex/gqldoc-actions
- [ ] Subscription
- [ ] Directive and it's location.
- [ ] Put README.md in output directory
- [x] ~~Put README.md in output directory~~
- TOC purpose
- [ ] Custom Template
- [ ] Render HTML
Expand Down
13 changes: 13 additions & 0 deletions example/github/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# API Reference

View reference documentation to learn about the data types available in your GraphQL API schema.


- [Query](queries.md)
- [Mutations](mutations.md)
- [Objects](objects.md)
- [Interfaces](interfaces.md)
- [Enums](enums.md)
- [Unions](unions.md)
- [Input objects](input_objects.md)
- [Scalars](scalars.md)
9 changes: 9 additions & 0 deletions example/starwars/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# API Reference

View reference documentation to learn about the data types available in your GraphQL API schema.


- [Query](queries.md)
- [Objects](objects.md)
- [Interfaces](interfaces.md)
- [Scalars](scalars.md)
7 changes: 7 additions & 0 deletions internal/markdown/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# API Reference

View reference documentation to learn about the data types available in your GraphQL API schema.

{{ range $i := .Items }}
- [{{ $i.Type }}]({{ $i.Link }})
{{- end }}
105 changes: 93 additions & 12 deletions internal/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"strings"
"text/template"
"unsafe"

"github.com/Code-Hex/gqldoc/internal/introspection"
Expand Down Expand Up @@ -50,40 +51,120 @@ func (m *Config) MakeLinkFromType(typ *introspection.Type) (string, error) {
}

func (m *Config) Render(s *introspection.Schema) error {
readmeContent := &README{}

if s.QueryType != nil {
if err := m.renderQuery(s); err != nil {
return errors.WithStack(err)
}
readmeContent.Items = append(readmeContent.Items, &READMEItem{
Type: "Query",
Link: "queries.md",
})
}
if s.MutationType != nil {
if err := m.renderMutation(s); err != nil {
return errors.WithStack(err)
}
readmeContent.Items = append(readmeContent.Items, &READMEItem{
Type: "Mutations",
Link: "mutations.md",
})
}
if len(s.Types) > 0 {
types := make(map[introspection.TypeKind][]*introspection.Types, len(s.Types))
for _, typ := range s.Types {
types[typ.Kind] = append(types[typ.Kind], typ)
}
if err := m.renderObjects(types[introspection.OBJECT]); err != nil {
return errors.WithStack(err)
if typs := types[introspection.OBJECT]; len(typs) > 0 {
if err := m.renderObjects(typs); err != nil {
return errors.WithStack(err)
}
readmeContent.Items = append(readmeContent.Items, &READMEItem{
Type: "Objects",
Link: "objects.md",
})
}
if err := m.renderInterfaces(types[introspection.INTERFACE]); err != nil {
return errors.WithStack(err)
if typs := types[introspection.INTERFACE]; len(typs) > 0 {
if err := m.renderInterfaces(typs); err != nil {
return errors.WithStack(err)
}
readmeContent.Items = append(readmeContent.Items, &READMEItem{
Type: "Interfaces",
Link: "interfaces.md",
})
}
if err := m.renderEnums(types[introspection.ENUM]); err != nil {
return errors.WithStack(err)
if typs := types[introspection.ENUM]; len(typs) > 0 {
if err := m.renderEnums(typs); err != nil {
return errors.WithStack(err)
}
readmeContent.Items = append(readmeContent.Items, &READMEItem{
Type: "Enums",
Link: "enums.md",
})
}
if err := m.renderUnions(types[introspection.UNION]); err != nil {
return errors.WithStack(err)
if typs := types[introspection.UNION]; len(typs) > 0 {
if err := m.renderUnions(typs); err != nil {
return errors.WithStack(err)
}
readmeContent.Items = append(readmeContent.Items, &READMEItem{
Type: "Unions",
Link: "unions.md",
})
}
if err := m.renderInputObjects(types[introspection.INPUT_OBJECT]); err != nil {
return errors.WithStack(err)
if typs := types[introspection.INPUT_OBJECT]; len(typs) > 0 {
if err := m.renderInputObjects(typs); err != nil {
return errors.WithStack(err)
}
readmeContent.Items = append(readmeContent.Items, &READMEItem{
Type: "Input objects",
Link: "input_objects.md",
})
}
if err := m.renderScalars(types[introspection.SCALAR]); err != nil {
return errors.WithStack(err)
if typs := types[introspection.SCALAR]; len(typs) > 0 {
if err := m.renderScalars(typs); err != nil {
return errors.WithStack(err)
}
readmeContent.Items = append(readmeContent.Items, &READMEItem{
Type: "Scalars",
Link: "scalars.md",
})
}
}

if err := m.renderREADME(readmeContent); err != nil {
return errors.WithStack(err)
}

return nil
}

//go:embed README.md
var readme string

type README struct {
Items []*READMEItem
}

type READMEItem struct {
Type string
Link string
}

func (m *Config) renderREADME(r *README) error {
f, err := m.Create("README.md")
if err != nil {
return errors.WithStack(err)
}
defer f.Close()

t, err := template.New("README").Parse(readme)
if err != nil {
return errors.WithStack(err)
}

if err := t.Execute(f, r); err != nil {
return errors.WithStack(err)
}
return nil
}

0 comments on commit a82102c

Please sign in to comment.