Skip to content

Commit

Permalink
Fixed links.
Browse files Browse the repository at this point in the history
Signed-off-by: Bartlomiej Plotka <[email protected]>
  • Loading branch information
bwplotka committed May 18, 2021
1 parent 600e9bf commit d75d455
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pkg/transform/testdata/testproj/Proposals/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Proposals

[RelLink](../../../tmp/2/test1/_index.md)
[RelLink](../README.md)

[RelLink](../Team/doc.md)
[RelLink](../Team/doc.md)
2 changes: 1 addition & 1 deletion pkg/transform/testdata/testproj/Team/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

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

[RelLink](../Proposals/README.md)
[RelLink](../Proposals/README.md)
66 changes: 53 additions & 13 deletions pkg/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Dir(ctx context.Context, logger log.Logger, configFile string) error {
}

var (
linkTransformer = &relLinkTransformer{outputDir: c.OutputDir, absRelNewPathByFile: map[string]string{}}
linkTransformer = &relLinkTransformer{outputDir: c.OutputDir, newAbsRelPathByOldAbsRelPath: map[string]string{}}
files []string
)

Expand All @@ -58,13 +58,16 @@ func Dir(ctx context.Context, logger log.Logger, configFile string) error {
if info.IsDir() || path == c.InputDir {
return nil
}
files = append(files, path)

// Copy while preserving structure and tolerating custom mapping.
// absRelPath is an absolute path, but relatively to input dir (has `/` upfront).
absRelPath := strings.TrimPrefix(path, c.InputDir)

target := filepath.Join(c.OutputDir, absRelPath)
defer func() {
files = append(files, target)
}()

t, ok := firstMatch(absRelPath, c.Transformations)
if !ok {
level.Debug(logger).Log("msg", "copying without transformation", "in", path, "absRelPath", absRelPath, "target", target)
Expand All @@ -74,7 +77,7 @@ func Dir(ctx context.Context, logger log.Logger, configFile string) error {
var opts []mdformatter.Option
newAbsRelPath := newTargetAbsRelPath(absRelPath, t)
if newAbsRelPath != absRelPath {
linkTransformer.absRelNewPathByFile[path] = newAbsRelPath
linkTransformer.newAbsRelPathByOldAbsRelPath[absRelPath] = newAbsRelPath
}

target = filepath.Join(c.OutputDir, newAbsRelPath)
Expand All @@ -100,25 +103,62 @@ func Dir(ctx context.Context, logger log.Logger, configFile string) error {
}

type relLinkTransformer struct {
outputDir string
absRelNewPathByFile map[string]string
outputDir string
newAbsRelPathByOldAbsRelPath map[string]string
}

func (r *relLinkTransformer) TransformDestination(ctx mdformatter.SourceContext, destination []byte) ([]byte, error) {
d := string(destination)
if strings.Contains(d, "://") || filepath.IsAbs(d) {
split := strings.Split(string(destination), "#")
dest := split[0]
if strings.Contains(dest, "://") || filepath.IsAbs(dest) || strings.HasPrefix(string(destination), "#") {
return destination, nil
}
// TODO(bwplotka): Check if links are outside?
currentAbsRelPath := strings.TrimPrefix(ctx.Filepath, r.outputDir)
if filepath.Join(currentAbsRelPath, dest) == ctx.Filepath {
// Pointing to self.
_, file := filepath.Split(ctx.Filepath)
if len(split) > 1 {
return []byte(file + "#" + split[1]), nil
}
return []byte(file), nil
}

currentAbsRelDir := filepath.Dir(currentAbsRelPath)

// absTargetRelPath is an absolute path, but relatively to input dir (has `/` upfront).
absTargetRelPath := strings.TrimPrefix(ctx.Filepath, r.outputDir)
// Do we changed?
change := ""
for n, old := range r.newAbsRelPathByOldAbsRelPath {
if old != currentAbsRelPath {
continue
}
c, err := filepath.Rel(filepath.Dir(old), filepath.Dir(n))
if err != nil {
return nil, err
}
change = c
break
}

if absNewRelPath, ok := r.absRelNewPathByFile[filepath.Join(absTargetRelPath, d)]; ok {
str, err := filepath.Rel(ctx.Filepath, filepath.Join(r.outputDir, absNewRelPath))
return []byte(str), err
adjustedAbsRelDir := filepath.Join(currentAbsRelDir, change)
adjustedAbsRelDest := filepath.Join(adjustedAbsRelDir, dest)

// Does the link points to something that changed?
if absNewRelPath, ok := r.newAbsRelPathByOldAbsRelPath[adjustedAbsRelDest]; ok {
adjustedAbsRelDest = absNewRelPath
}

newDest, err := filepath.Rel(currentAbsRelDir, adjustedAbsRelDest)
if err != nil {
return nil, err
}
if newDest == "." {
newDest = ""
}
if len(split) > 1 {
return []byte(strings.TrimPrefix(newDest, "/") + "#" + split[1]), nil
}
return destination, nil
return []byte(strings.TrimPrefix(newDest, "/")), nil
}

func (r *relLinkTransformer) Close(mdformatter.SourceContext) error { return nil }
Expand Down
2 changes: 1 addition & 1 deletion pkg/transform/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestTransform(t *testing.T) {
tmpDir = "./tmp"
testData = "testdata"
)
//defer func() { _ = os.RemoveAll(tmpDir) }()
defer func() { _ = os.RemoveAll(tmpDir) }()

logger := log.NewLogfmtLogger(os.Stdout)

Expand Down

0 comments on commit d75d455

Please sign in to comment.