From d75d455206cc4ddff5f98393d9a319cc0836127f Mon Sep 17 00:00:00 2001 From: Bartlomiej Plotka Date: Tue, 18 May 2021 00:33:31 +0200 Subject: [PATCH] Fixed links. Signed-off-by: Bartlomiej Plotka --- .../testdata/testproj/Proposals/README.md | 4 +- pkg/transform/testdata/testproj/Team/doc.md | 2 +- pkg/transform/transform.go | 66 +++++++++++++++---- pkg/transform/transform_test.go | 2 +- 4 files changed, 57 insertions(+), 17 deletions(-) diff --git a/pkg/transform/testdata/testproj/Proposals/README.md b/pkg/transform/testdata/testproj/Proposals/README.md index fd44845..c68c4da 100644 --- a/pkg/transform/testdata/testproj/Proposals/README.md +++ b/pkg/transform/testdata/testproj/Proposals/README.md @@ -1,5 +1,5 @@ # Proposals -[RelLink](../../../tmp/2/test1/_index.md) +[RelLink](../README.md) -[RelLink](../Team/doc.md) +[RelLink](../Team/doc.md) \ No newline at end of file diff --git a/pkg/transform/testdata/testproj/Team/doc.md b/pkg/transform/testdata/testproj/Team/doc.md index 67c3ae3..1e7ece5 100644 --- a/pkg/transform/testdata/testproj/Team/doc.md +++ b/pkg/transform/testdata/testproj/Team/doc.md @@ -4,4 +4,4 @@ [RelLink](../README.md#group-handbook) -[RelLink](../Proposals/README.md) +[RelLink](../Proposals/README.md) \ No newline at end of file diff --git a/pkg/transform/transform.go b/pkg/transform/transform.go index c178fb9..154d0b4 100644 --- a/pkg/transform/transform.go +++ b/pkg/transform/transform.go @@ -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 ) @@ -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) @@ -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) @@ -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 } diff --git a/pkg/transform/transform_test.go b/pkg/transform/transform_test.go index 7f9daa9..b89a0ac 100644 --- a/pkg/transform/transform_test.go +++ b/pkg/transform/transform_test.go @@ -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)