Skip to content

Commit

Permalink
Merge pull request #23 from gopherguides/filename-for-doc-json
Browse files Browse the repository at this point in the history
Filename for doc json
  • Loading branch information
markbates authored Feb 12, 2024
2 parents ba89818 + c70552d commit e097409
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cmd/hype/cli/testdata/encode/json/success/execute-file.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@
"section_id": 1,
"snippets": {},
"title": "JSON Encoding",
"type": "*hype.Document"
"type": "*hype.Document",
"filename": "module.md"
}
3 changes: 2 additions & 1 deletion cmd/hype/cli/testdata/encode/json/success/parse-file.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@
"section_id": 1,
"snippets": {},
"title": "JSON Encoding",
"type": "*hype.Document"
"type": "*hype.Document",
"filename": "module.md"
}
3 changes: 3 additions & 0 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Document struct {
SectionID int `json:"section_id,omitempty"`
Snippets Snippets `json:"snippets,omitempty"`
Title string `json:"title,omitempty"`
Filename string `json:"filename,omitempty"`
}

func (doc *Document) MarshalJSON() ([]byte, error) {
Expand All @@ -40,6 +41,7 @@ func (doc *Document) MarshalJSON() ([]byte, error) {
Snippets Snippets `json:"snippets,omitempty"`
Title string `json:"title,omitempty"`
Type string `json:"type"`
Filename string `json:"filename,omitempty"`
}{
Type: fmt.Sprintf("%T", doc),
Parser: doc.Parser,
Expand All @@ -49,6 +51,7 @@ func (doc *Document) MarshalJSON() ([]byte, error) {
Title: doc.Title,
Nodes: doc.Nodes,
ID: doc.ID,
Filename: doc.Filename,
}

return json.Marshal(x)
Expand Down
1 change: 1 addition & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ func (p *Parser) newDoc() (*Document, error) {
Root: p.Root,
SectionID: p.Section,
Snippets: p.Snippets,
Filename: p.fileName,
}

return doc, nil
Expand Down

0 comments on commit e097409

Please sign in to comment.