Skip to content

Commit

Permalink
Implement suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: Saswata Mukherjee <[email protected]>
  • Loading branch information
saswatamcode committed Aug 22, 2021
1 parent d61b1dc commit 1be5367
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 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
4 changes: 2 additions & 2 deletions pkg/transform/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ 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"`
// 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/mdox4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extraInputGlobs:
- "testdata/test.md"
- "testdata/teststatic"

glueLink: "https://github.com/bwplotka/mdox/tree/main"
linkPrefixForNonMarkdownResources: "https://github.com/bwplotka/mdox/tree/main"
gitIgnored: true
localLinksStyle:
hugo:
Expand Down
28 changes: 14 additions & 14 deletions pkg/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ func Dir(ctx context.Context, logger log.Logger, config []byte) error {
logger: logger,

linkTransformer: &relLinkTransformer{
localLinksStyle: c.LocalLinksStyle,
inputDir: c.InputDir,
glueLink: c.GlueLink,
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 @@ -269,11 +269,11 @@ func (t *transformer) transformFile(path string, info os.FileInfo, err error) er
type relLinkTransformer struct {
localLinksStyle LocalLinksStyle

inputDir string
outputDir string
glueLink 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 @@ -307,8 +307,8 @@ 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 != "" {
// 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
Expand All @@ -328,7 +328,7 @@ func (r *relLinkTransformer) TransformDestination(ctx mdformatter.SourceContext,
if err != nil {
return nil, err
}
return []byte(r.glueLink + "/" + originalRelPath), nil
return []byte(r.linkPrefixForNonMarkdownResources + "/" + originalRelPath), nil
}

currDest := oldRelDest
Expand Down

0 comments on commit 1be5367

Please sign in to comment.