Skip to content

Commit e097409

Browse files
authored
Merge pull request #23 from gopherguides/filename-for-doc-json
Filename for doc json
2 parents ba89818 + c70552d commit e097409

File tree

4 files changed

+8
-2
lines changed

4 files changed

+8
-2
lines changed

cmd/hype/cli/testdata/encode/json/success/execute-file.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,6 @@
9393
"section_id": 1,
9494
"snippets": {},
9595
"title": "JSON Encoding",
96-
"type": "*hype.Document"
96+
"type": "*hype.Document",
97+
"filename": "module.md"
9798
}

cmd/hype/cli/testdata/encode/json/success/parse-file.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,6 @@
9393
"section_id": 1,
9494
"snippets": {},
9595
"title": "JSON Encoding",
96-
"type": "*hype.Document"
96+
"type": "*hype.Document",
97+
"filename": "module.md"
9798
}

document.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Document struct {
2424
SectionID int `json:"section_id,omitempty"`
2525
Snippets Snippets `json:"snippets,omitempty"`
2626
Title string `json:"title,omitempty"`
27+
Filename string `json:"filename,omitempty"`
2728
}
2829

2930
func (doc *Document) MarshalJSON() ([]byte, error) {
@@ -40,6 +41,7 @@ func (doc *Document) MarshalJSON() ([]byte, error) {
4041
Snippets Snippets `json:"snippets,omitempty"`
4142
Title string `json:"title,omitempty"`
4243
Type string `json:"type"`
44+
Filename string `json:"filename,omitempty"`
4345
}{
4446
Type: fmt.Sprintf("%T", doc),
4547
Parser: doc.Parser,
@@ -49,6 +51,7 @@ func (doc *Document) MarshalJSON() ([]byte, error) {
4951
Title: doc.Title,
5052
Nodes: doc.Nodes,
5153
ID: doc.ID,
54+
Filename: doc.Filename,
5255
}
5356

5457
return json.Marshal(x)

parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ func (p *Parser) newDoc() (*Document, error) {
453453
Root: p.Root,
454454
SectionID: p.Section,
455455
Snippets: p.Snippets,
456+
Filename: p.fileName,
456457
}
457458

458459
return doc, nil

0 commit comments

Comments
 (0)