Skip to content

Commit

Permalink
Add glueLink to transform config
Browse files Browse the repository at this point in the history
Signed-off-by: Saswata Mukherjee <[email protected]>
  • Loading branch information
saswatamcode committed Aug 15, 2021
1 parent bc3e6b5 commit 03342cd
Show file tree
Hide file tree
Showing 17 changed files with 146 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pkg/transform/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type Config struct {
// ExtraInputGlobs allows to bring files from outside of input dir.
ExtraInputGlobs []string `yaml:"extraInputGlobs"`

// GlueLink specifies link to be glued onto relative links which don't point to markdown or image files.
GlueLink string `yaml:"glueLink"`

// Transformations to apply for any file.
Transformations []*TransformationConfig

Expand Down
2 changes: 1 addition & 1 deletion pkg/transform/testdata/expected/test1/1/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ Yolo

![Image](images/logo2.png)

![Outside](../../../../main.go)
![Outside](../test.go)
2 changes: 1 addition & 1 deletion pkg/transform/testdata/expected/test2/2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Yolo

![Image](../images/logo2.png)

![Outside](../../../../../main.go)
![Outside](../../test.go)
2 changes: 1 addition & 1 deletion pkg/transform/testdata/expected/test3/3/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Yolo

![Image](images/logo2.png)

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

---

Expand Down
13 changes: 13 additions & 0 deletions pkg/transform/testdata/expected/test4/4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Group Handbook

Yolo

[RelLink](../test1/_index.md/)

[RelLink](../team/inner/doc.md/)

![Image](../logo.png)

![Image](../images/logo2.png)

![Outside](https://github.com/bwplotka/mdox/tree/main/testdata/test.go)
11 changes: 11 additions & 0 deletions pkg/transform/testdata/expected/test4/4/Team/inner/doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
yolo: yolo
title: Some Doc
slug: doc.md
---

[RelLink](#some-doc)

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

[RelLink](../../../test1/_index.md/)
17 changes: 17 additions & 0 deletions pkg/transform/testdata/expected/test4/4/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: Test
cascade:
- _target:
path: /**
type: docs
---

Yolo

[RelLink](test1/_index.md/)

[RelLink](team/inner/doc.md/)

![Image](logo.png)

![Image](images/logo2.png)
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pkg/transform/testdata/expected/test4/4/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions pkg/transform/testdata/expected/test4/4/test1/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Proposals
---

[RelLink](../readme.md/)

[RelLink](../team/inner/doc.md/)

This should not work: [RelLink](../_index.md/)
51 changes: 51 additions & 0 deletions pkg/transform/testdata/mdox4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
version: 1

inputDir: "testdata/testproj"
outputDir: "testdata/tmp/test4/4"
extraInputGlobs:
- "testdata/test.md"
- "testdata/teststatic"

glueLink: "https://github.com/bwplotka/mdox/tree/main"
gitIgnored: true
localLinksStyle:
hugo:
indexFileName: "_index.md"

transformations:
- glob: "../teststatic/**"
path: /favicons/**

- glob: "../test.md"
path: /_index.md
popHeader: true
frontMatter:
template: |
title: "{{ .Origin.FirstHeader }}"
cascade:
- type: "docs"
_target:
path: "/**"
- glob: "doc.md"
popHeader: true
frontMatter:
template: |
title: "{{ .Origin.FirstHeader }}"
slug: "{{ .Target.FileName }}"
- glob: "Team/doc.md"
path: inner/doc.md
popHeader: true
frontMatter:
template: |
title: "{{ .Origin.FirstHeader }}"
yolo: "yolo"
- glob: "**/README.md"
path: /test1/_index.md
popHeader: true
frontMatter:
template: |
title: "{{ .Origin.FirstHeader }}"
3 changes: 3 additions & 0 deletions pkg/transform/testdata/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Copyright (c) Bartłomiej Płotka @bwplotka
// Licensed under the Apache License 2.0.

2 changes: 1 addition & 1 deletion pkg/transform/testdata/testproj/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Yolo

![Image](images/logo2.png)

![Outside](../../../../main.go)
![Outside](../test.go)
35 changes: 35 additions & 0 deletions pkg/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ func isMDFile(path string) bool {
return filepath.Ext(path) == ".md"
}

func isImgFile(path string) bool {
switch filepath.Ext(path) {
case ".png", ".jpg", "jpeg", ".svg", ".gif", ".webp":
return true
default:
}
return false
}

func prepOutputDir(d string, gitIgnored bool) error {
_, err := os.Stat(d)
if err != nil && !os.IsNotExist(err) {
Expand Down Expand Up @@ -67,6 +76,7 @@ func Dir(ctx context.Context, logger log.Logger, config []byte) error {
linkTransformer: &relLinkTransformer{
localLinksStyle: c.LocalLinksStyle,
inputDir: c.InputDir,
glueLink: c.GlueLink,
outputDir: c.OutputDir,
oldRelPath: map[string]string{},
newRelPath: map[string]string{},
Expand Down Expand Up @@ -261,6 +271,7 @@ type relLinkTransformer struct {

inputDir string
outputDir string
glueLink string
oldRelPath map[string]string
newRelPath map[string]string
}
Expand Down Expand Up @@ -296,6 +307,30 @@ func (r *relLinkTransformer) TransformDestination(ctx mdformatter.SourceContext,
}
}

// Non md or image relative link, so needs link to be glued.
if !isMDFile(relDest) && !isImgFile(relDest) && r.glueLink != "" {
workingDir, err := os.Getwd()
if err != nil {
return nil, err
}
// Original path before transform needs to be figured out.
// Case where file is in same position after transform.
oldRelPath := filepath.Join(r.inputDir, curRelDir, relDest)
if _, err := os.Stat(oldRelPath); os.IsNotExist(err) {
// File is moved to new dir after transform.
oldRelPath = filepath.Join(workingDir, curRelDir, relDest)
if _, err := os.Stat(oldRelPath); os.IsNotExist(err) {
// File is extra input glob.
oldRelPath = filepath.Join(workingDir, relDest)
}
}
originalRelPath, err := filepath.Rel(workingDir, oldRelPath)
if err != nil {
return nil, err
}
return []byte(r.glueLink + "/" + originalRelPath), nil
}

currDest := oldRelDest
if newRelPath, ok := r.newRelPath[oldRelDest]; ok {
currDest = newRelPath
Expand Down

0 comments on commit 03342cd

Please sign in to comment.