Skip to content

Commit

Permalink
Add linkPrefixForNonMarkdownResources to transform config (#76)
Browse files Browse the repository at this point in the history
* Add glueLink to transform config

Signed-off-by: Saswata Mukherjee <[email protected]>

* Implement suggestions

Signed-off-by: Saswata Mukherjee <[email protected]>
  • Loading branch information
saswatamcode authored Aug 24, 2021
1 parent f16709a commit db5c373
Show file tree
Hide file tree
Showing 19 changed files with 157 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ We use *breaking* word for marking changes that are not backward compatible (rel
* [#69](https://github.com/bwplotka/mdox/pull/69) Add timeout option to `links.validate.config`
* [#71](https://github.com/bwplotka/mdox/pull/71) Add `Origin.Path` to transform template
* [#75](https://github.com/bwplotka/mdox/pull/75) Add `soft-wraps` flag to preserve soft line breaks
* [#76](https://github.com/bwplotka/mdox/pull/76) Add `linkPrefixForNonMarkdownResources` to transform config

### Fixed

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ As seen above,
* `inputDir`: It's a relative (to PWD) path that assumes input directory for markdown files and assets.
* `outputDir`: It's a relative (to PWD) output directory where you can expect all files to land in. Typically that can be a `content` dir which Hugo uses as an input.
* `extraInputGlobs`: It allows you to bring files from outside of `inputDir`.
* `linkPrefixForNonMarkdownResources`: It specifies the link to be glued onto relative links which don't point to markdown or image files.
* `gitIgnored`: It specifies whether `outputDir` should be git-ignored.
* `localLinksStyle`: It sets the linking style to be applied. If empty, mdox assumes default style.
Expand Down
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"`

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

// 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"

linkPrefixForNonMarkdownResources: "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)
53 changes: 44 additions & 9 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 @@ -65,11 +74,12 @@ func Dir(ctx context.Context, logger log.Logger, config []byte) error {
logger: logger,

linkTransformer: &relLinkTransformer{
localLinksStyle: c.LocalLinksStyle,
inputDir: c.InputDir,
outputDir: c.OutputDir,
oldRelPath: map[string]string{},
newRelPath: map[string]string{},
localLinksStyle: c.LocalLinksStyle,
inputDir: c.InputDir,
outputDir: c.OutputDir,
oldRelPath: map[string]string{},
newRelPath: map[string]string{},
linkPrefixForNonMarkdownResources: c.LinkPrefixForNonMarkdownResources,
},
}

Expand Down Expand Up @@ -259,10 +269,11 @@ func (t *transformer) transformFile(path string, info os.FileInfo, err error) er
type relLinkTransformer struct {
localLinksStyle LocalLinksStyle

inputDir string
outputDir string
oldRelPath map[string]string
newRelPath map[string]string
inputDir string
outputDir string
oldRelPath map[string]string
newRelPath map[string]string
linkPrefixForNonMarkdownResources string
}

func (r *relLinkTransformer) TransformDestination(ctx mdformatter.SourceContext, destination []byte) ([]byte, error) {
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 prefixed.
if r.linkPrefixForNonMarkdownResources != "" && !isMDFile(relDest) && !isImgFile(relDest) {
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.linkPrefixForNonMarkdownResources + "/" + originalRelPath), nil
}

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

0 comments on commit db5c373

Please sign in to comment.