Skip to content

Commit

Permalink
Add Path to transform template (#71)
Browse files Browse the repository at this point in the history
Signed-off-by: Saswata Mukherjee <[email protected]>
  • Loading branch information
saswatamcode authored Aug 10, 2021
1 parent 13fc035 commit dd2d810
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/transform/testdata/expected/test3/3/Team/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

---

Found a typo, inconsistency or missing information in our docs? Help us to improve [Thanos](https://thanos.io) documentation by proposing a fix [on GitHub here](https://github.com/thanos-io/thanos/edit/main/docs/doc.md) :heart:
Found a typo, inconsistency or missing information in our docs? Help us to improve [Thanos](https://thanos.io) documentation by proposing a fix [on GitHub here](https://github.com/thanos-io/thanos/edit/main/testdata/testproj/Team/doc.md) :heart:
2 changes: 1 addition & 1 deletion pkg/transform/testdata/mdox3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ transformations:
backMatter:
template: |
Found a typo, inconsistency or missing information in our docs?
Help us to improve [Thanos](https://thanos.io) documentation by proposing a fix [on GitHub here](https://github.com/thanos-io/thanos/edit/main/docs/{{ .Origin.Filename }}) :heart:
Help us to improve [Thanos](https://thanos.io) documentation by proposing a fix [on GitHub here](https://github.com/thanos-io/thanos/edit/main/{{ .Origin.Path }}) :heart:
- glob: "**"
Expand Down
22 changes: 22 additions & 0 deletions pkg/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ func (t *transformer) transformFile(path string, info os.FileInfo, err error) er
}
}

wd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "get working dir for Path")
}
originPath, err := filepath.Rel(wd, path)
if err != nil {
return errors.Wrap(err, "rel path to working dir")
}

_, originFilename := filepath.Split(path)
_, targetFilename := filepath.Split(target)
opts = append(opts, mdformatter.WithFrontMatterTransformer(&frontMatterTransformer{
Expand All @@ -202,6 +211,7 @@ func (t *transformer) transformFile(path string, info os.FileInfo, err error) er
origin: MatterOrigin{
Filename: originFilename,
FirstHeader: firstHeader,
Path: originPath,
LastMod: info.ModTime().String(),
},
target: MatterTarget{
Expand All @@ -214,12 +224,23 @@ func (t *transformer) transformFile(path string, info os.FileInfo, err error) er
if !isMDFile(target) {
return errors.Errorf("back matter option set on file that after transformation is non-markdown: %v", target)
}

wd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "get working dir for Path")
}
originPath, err := filepath.Rel(wd, path)
if err != nil {
return errors.Wrap(err, "rel path to working dir")
}

_, originFilename := filepath.Split(path)
_, targetFilename := filepath.Split(target)
opts = append(opts, mdformatter.WithBackMatterTransformer(&backMatterTransformer{
b: tr.BackMatter,
origin: MatterOrigin{
Filename: originFilename,
Path: originPath,
LastMod: info.ModTime().String(),
},
target: MatterTarget{
Expand Down Expand Up @@ -328,6 +349,7 @@ type MatterOrigin struct {
Filename string
FirstHeader string
LastMod string
Path string
}

type MatterTarget struct {
Expand Down

0 comments on commit dd2d810

Please sign in to comment.