Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Origin.Path to transform template #71

Merged
merged 1 commit into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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