Skip to content

Commit

Permalink
added file name to json output
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Feb 12, 2024
1 parent 7991606 commit 328ccd8
Show file tree
Hide file tree
Showing 4 changed files with 11 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 @@ -92,5 +92,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 @@ -92,5 +92,6 @@
"section_id": 1,
"snippets": {},
"title": "JSON Encoding",
"type": "*hype.Document"
"type": "*hype.Document",
"filename": "module.md"
}
6 changes: 6 additions & 0 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type Document struct {
SectionID int `json:"section_id,omitempty"`
Snippets Snippets `json:"snippets,omitempty"`
Title string `json:"title,omitempty"`
ID string `json:"id,omitempty"`
Filename string `json:"filename,omitempty"`
}

func (doc *Document) MarshalJSON() ([]byte, error) {
Expand All @@ -38,6 +40,8 @@ func (doc *Document) MarshalJSON() ([]byte, error) {
Snippets Snippets `json:"snippets,omitempty"`
Title string `json:"title,omitempty"`
Type string `json:"type"`
ID string `json:"id,omitempty"`
Filename string `json:"filename,omitempty"`
}{
Type: fmt.Sprintf("%T", doc),
Parser: doc.Parser,
Expand All @@ -46,6 +50,8 @@ func (doc *Document) MarshalJSON() ([]byte, error) {
Snippets: doc.Snippets,
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 @@ -422,6 +422,7 @@ func (p *Parser) newDoc() *Document {
Root: p.Root,
SectionID: p.Section,
Snippets: p.Snippets,
Filename: p.fileName,
}

return doc
Expand Down

0 comments on commit 328ccd8

Please sign in to comment.