From 4684a10861947ed02aa30eb0ead9708e13b1711a Mon Sep 17 00:00:00 2001 From: Saswata Mukherjee Date: Sun, 8 Aug 2021 10:23:20 +0530 Subject: [PATCH] Add Path to transform template Signed-off-by: Saswata Mukherjee --- .../testdata/expected/test3/3/Team/doc.md | 2 +- pkg/transform/testdata/mdox3.yaml | 2 +- pkg/transform/transform.go | 22 +++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/pkg/transform/testdata/expected/test3/3/Team/doc.md b/pkg/transform/testdata/expected/test3/3/Team/doc.md index dc6a6da..8bb4852 100644 --- a/pkg/transform/testdata/expected/test3/3/Team/doc.md +++ b/pkg/transform/testdata/expected/test3/3/Team/doc.md @@ -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: diff --git a/pkg/transform/testdata/mdox3.yaml b/pkg/transform/testdata/mdox3.yaml index f337c49..f54ae1b 100644 --- a/pkg/transform/testdata/mdox3.yaml +++ b/pkg/transform/testdata/mdox3.yaml @@ -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: "**" diff --git a/pkg/transform/transform.go b/pkg/transform/transform.go index 0f5ee2e..22a35f9 100644 --- a/pkg/transform/transform.go +++ b/pkg/transform/transform.go @@ -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{ @@ -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{ @@ -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{ @@ -328,6 +349,7 @@ type MatterOrigin struct { Filename string FirstHeader string LastMod string + Path string } type MatterTarget struct {