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 backMatter support #47

Merged
merged 1 commit into from
Jun 24, 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
25 changes: 25 additions & 0 deletions pkg/mdformatter/mdformatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ type FrontMatterTransformer interface {
Close(ctx SourceContext) error
}

type BackMatterTransformer interface {
TransformBackMatter(ctx SourceContext) ([]byte, error)
Close(ctx SourceContext) error
}

type LinkTransformer interface {
TransformDestination(ctx SourceContext, destination []byte) ([]byte, error)
Close(ctx SourceContext) error
Expand All @@ -49,6 +54,7 @@ type Formatter struct {
ctx context.Context

fm FrontMatterTransformer
bm BackMatterTransformer
link LinkTransformer
cb CodeBlockTransformer
}
Expand All @@ -63,6 +69,13 @@ func WithFrontMatterTransformer(fm FrontMatterTransformer) Option {
}
}

// WithBackMatterTransformer allows you to override the default BackMatterTransformer.
func WithBackMatterTransformer(bm BackMatterTransformer) Option {
return func(m *Formatter) {
m.bm = bm
}
}

// WithLinkTransformer allows you to override the default LinkTransformer.
func WithLinkTransformer(l LinkTransformer) Option {
return func(m *Formatter) {
Expand Down Expand Up @@ -276,6 +289,18 @@ func (f *Formatter) Format(file *os.File, out io.Writer) error {
}
}

if f.bm != nil {
back, err := f.bm.TransformBackMatter(sourceCtx)
if err != nil {
return err
}

content = append(content, back...)
if err := f.fm.Close(sourceCtx); err != nil {
return err
}
}

// Hack: run Convert two times to ensure deterministic whitespace alignment.
// This also immediately show transformers which are not working well together etc.
tmp := bytes.Buffer{}
Expand Down
20 changes: 15 additions & 5 deletions pkg/transform/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ type TransformationConfig struct {
Path string

// FrontMatter holds front matter transformations.
FrontMatter *FrontMatterConfig `yaml:"frontMatter"`
FrontMatter *MatterConfig `yaml:"frontMatter"`

// BackMatter holds back matter transformations.
BackMatter *MatterConfig `yaml:"backMatter"`
}

func (tr TransformationConfig) targetRelPath(relPath string) (_ string, err error) {
Expand Down Expand Up @@ -106,11 +109,11 @@ func (tr TransformationConfig) targetRelPath(relPath string) (_ string, err erro
return filepath.Join(targetDir, targetSuffix), nil
}

type FrontMatterConfig struct {
type MatterConfig struct {
_template *template.Template

// Template represents Go template that will be rendered as font matter.
// This will override any existing font matter.1
// Template represents Go template that will be rendered as matter.
// This will override any existing matter.
// TODO(bwplotka): Add add only option?
Template string
}
Expand Down Expand Up @@ -158,7 +161,14 @@ func ParseConfig(c []byte) (Config, error) {
if f.FrontMatter != nil {
f.FrontMatter._template, err = template.New("").Parse(f.FrontMatter.Template)
if err != nil {
return Config{}, errors.Wrapf(err, "compiling template %v", f.FrontMatter.Template)
return Config{}, errors.Wrapf(err, "compiling frontMatter template %v", f.FrontMatter.Template)
}
}

if f.BackMatter != nil {
f.BackMatter._template, err = template.New("").Parse(f.BackMatter.Template)
if err != nil {
return Config{}, errors.Wrapf(err, "compiling backMatter template %v", f.BackMatter.Template)
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/transform/testdata/expected/test3/3/Proposals/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Proposals
slug: README.md
lastmod: 'TODO: Allow testing last mod .Origin.LastMod'
---

[RelLink](../_index.md)

[RelLink](../Team/doc.md)

This should not work: [RelLink](../../test.md)

---

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/README.md) :heart:
11 changes: 11 additions & 0 deletions pkg/transform/testdata/expected/test3/3/Team/doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Some Doc

[RelLink](#some-doc)

[RelLink](../_index.md#group-handbook)

[RelLink](../Proposals/README.md)

---

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:
23 changes: 23 additions & 0 deletions pkg/transform/testdata/expected/test3/3/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: Group Handbook
cascade:
- _target:
path: /**
type: docs
---

Yolo

[RelLink](Proposals/README.md)

[RelLink](Team/doc.md)

![Image](logo.png)

![Image](images/logo2.png)

![Outside](../../../../main.go)

---

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/README.md) :heart:
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions pkg/transform/testdata/mdox3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: 1

inputDir: "testdata/testproj"
outputDir: "testdata/tmp/test3/3"

gitIgnored: true

transformations:
- glob: "README.md"
path: _index.md
frontMatter:
template: |
title: "{{ .Origin.FirstHeader }}"

cascade:
- type: "docs"
_target:
path: "/**"
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:


- glob: "**/README.md"
frontMatter:
template: |
title: "{{ .Origin.FirstHeader }}"
lastmod: "TODO: Allow testing last mod .Origin.LastMod"
slug: "{{ .Target.FileName }}"
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:


- glob: "**.md"
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:


- glob: "**"
path: "/../3static/**"
67 changes: 58 additions & 9 deletions pkg/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,30 @@ func (t *transformer) transformFile(path string, info os.FileInfo, err error) er
opts = append(opts, mdformatter.WithFrontMatterTransformer(&frontMatterTransformer{
localLinksStyle: t.c.LocalLinksStyle,
c: tr.FrontMatter,
origin: FrontMatterOrigin{
origin: MatterOrigin{
Filename: originFilename,
FirstHeader: firstHeader,
LastMod: info.ModTime().String(),
},
target: FrontMatterTarget{
target: MatterTarget{
FileName: targetFilename,
},
}))
}

if tr.BackMatter != nil {
if !isMDFile(target) {
return errors.Errorf("back matter option set on file that after transformation is non-markdown: %v", target)
}
_, originFilename := filepath.Split(path)
_, targetFilename := filepath.Split(target)
opts = append(opts, mdformatter.WithBackMatterTransformer(&backMatterTransformer{
b: tr.BackMatter,
origin: MatterOrigin{
Filename: originFilename,
LastMod: info.ModTime().String(),
},
target: MatterTarget{
FileName: targetFilename,
},
}))
Expand Down Expand Up @@ -276,28 +294,36 @@ func (r *relLinkTransformer) Close(mdformatter.SourceContext) error { return nil

type frontMatterTransformer struct {
localLinksStyle LocalLinksStyle
c *FrontMatterConfig
c *MatterConfig

// Vars.
origin MatterOrigin
target MatterTarget
}

type backMatterTransformer struct {
b *MatterConfig

// Vars.
origin FrontMatterOrigin
target FrontMatterTarget
origin MatterOrigin
target MatterTarget
}

type FrontMatterOrigin struct {
type MatterOrigin struct {
Filename string
FirstHeader string
LastMod string
}

type FrontMatterTarget struct {
type MatterTarget struct {
FileName string
}

func (f *frontMatterTransformer) TransformFrontMatter(ctx mdformatter.SourceContext, frontMatter map[string]interface{}) ([]byte, error) {
b := bytes.Buffer{}
if err := f.c._template.Execute(&b, struct {
Origin FrontMatterOrigin
Target FrontMatterTarget
Origin MatterOrigin
Target MatterTarget
FrontMatter map[string]interface{}
}{
Origin: f.origin,
Expand All @@ -323,6 +349,29 @@ func (f *frontMatterTransformer) TransformFrontMatter(ctx mdformatter.SourceCont

func (f *frontMatterTransformer) Close(mdformatter.SourceContext) error { return nil }

func (f *backMatterTransformer) TransformBackMatter(ctx mdformatter.SourceContext) ([]byte, error) {
b := bytes.Buffer{}
if err := f.b._template.Execute(&b, struct {
Origin MatterOrigin
Target MatterTarget
}{
Origin: f.origin,
Target: f.target,
}); err != nil {
return nil, err
}

// Add demarkation to back matter.
demark := "\n\n---\n"
m := b.Bytes()
m = append([]byte(demark), m...)

// Back matter is not parsed as YAML since it is generally assumed to be a block of text.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be confusing that one is yaml, one not, maybe worth to document it better.

return m, nil
}

func (f *backMatterTransformer) Close(mdformatter.SourceContext) error { return nil }

func firstMatch(absRelPath string, trs []*TransformationConfig) (*TransformationConfig, bool) {
for _, tr := range trs {
if tr._glob.Match(absRelPath) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/transform/transform_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func TestTransform(t *testing.T) {
assertDirContent(t, filepath.Join(testData, "expected", "test2"), filepath.Join(tmpDir, "test2"))

})
t.Run("mdox3.yaml", func(t *testing.T) {
mdox2, err := ioutil.ReadFile(filepath.Join(testData, "mdox3.yaml"))
testutil.Ok(t, err)
testutil.Ok(t, transform.Dir(context.Background(), logger, mdox2))
assertDirContent(t, filepath.Join(testData, "expected", "test3"), filepath.Join(tmpDir, "test3"))

})
}

func assertDirContent(t *testing.T, expectedDir string, gotDir string) {
Expand Down