Skip to content

Commit

Permalink
Fix anchor link issue (#21)
Browse files Browse the repository at this point in the history
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
  • Loading branch information
saswatamcode committed Apr 17, 2021
1 parent 59e9130 commit 50f4844
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
10 changes: 9 additions & 1 deletion pkg/mdformatter/linktransformer/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,12 @@ type localLinksCache map[string]*[]string

// Lookup looks for given link in local anchorDir. It returns error if link can't be found.
func (l localLinksCache) Lookup(absLink string) error {
absLinkSplit := strings.Split(absLink, "#")
splitWith := "#"
if strings.Contains(absLink, "/#") {
splitWith = "/#"
}

absLinkSplit := strings.Split(absLink, splitWith)
ids, ok := l[absLinkSplit[0]]
if !ok {
if err := l.addRelLinks(absLinkSplit[0]); err != nil {
Expand Down Expand Up @@ -354,6 +359,9 @@ func absLocalLink(anchorDir string, docPath string, destination string) string {
newDest = filepath.Base(docPath)
case strings.HasPrefix(destination, "#"):
newDest = filepath.Base(docPath) + destination
case strings.Contains(destination, "/#"):
destination = strings.Replace(destination, "/#", "#", 1)
return filepath.Join(anchorDir, destination)
}
return filepath.Join(filepath.Dir(docPath), newDest)
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/mdformatter/linktransformer/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
[10](http://myproject.example.com/tip/a/does_not_exists_file.md) [11](https://myproject.example.com/tip/a/does_not_exists_file2) [12](http://myproject.example.com/tip/does_not_exists/does_not_exists_dir.md)
[11](/doc2.md) [12](/a/doc.md#yolo) [13](../doc2.md) [14](../a/../a/../a/../a/doc.md) [15](doc.md)
[11](/doc2.md) [12](/a/doc.md#yolo) [13](../doc2.md) [14](../a/../a/../a/../a/doc.md) [15](doc.md) [16](doc2.md/#yolo-2)
`
)

Expand Down Expand Up @@ -68,8 +68,8 @@ func TestLocalizer_TransformDestination(t *testing.T) {
[10](http://myproject.example.com/tip/a/does_not_exists_file.md) [11](https://myproject.example.com/tip/a/does_not_exists_file2) [12](http://myproject.example.com/tip/does_not_exists/does_not_exists_dir.md)
-[11](/doc2.md) [12](/a/doc.md#yolo) [13](../doc2.md) [14](../a/../a/../a/../a/doc.md) [15](doc.md)
+[11](../doc2.md) [12](#yolo) [13](../doc2.md) [14](.) [15](.)
-[11](/doc2.md) [12](/a/doc.md#yolo) [13](../doc2.md) [14](../a/../a/../a/../a/doc.md) [15](doc.md) [16](doc2.md/#yolo-2)
+[11](../doc2.md) [12](#yolo) [13](../doc2.md) [14](.) [15](.) [16](../doc2.md#yolo-2)
`, tmpDir, tmpDir), diff.String())
})
Expand All @@ -86,8 +86,8 @@ func TestLocalizer_TransformDestination(t *testing.T) {
[10](http://myproject.example.com/tip/a/does_not_exists_file.md) [11](https://myproject.example.com/tip/a/does_not_exists_file2) [12](http://myproject.example.com/tip/does_not_exists/does_not_exists_dir.md)
-[11](/doc2.md) [12](/a/doc.md#yolo) [13](../doc2.md) [14](../a/../a/../a/../a/doc.md) [15](doc.md)
+[11](../doc2.md) [12](#yolo) [13](../doc2.md) [14](.) [15](.)
-[11](/doc2.md) [12](/a/doc.md#yolo) [13](../doc2.md) [14](../a/../a/../a/../a/doc.md) [15](doc.md) [16](doc2.md/#yolo-2)
+[11](../doc2.md) [12](#yolo) [13](../doc2.md) [14](.) [15](.) [16](../doc2.md#yolo-2)
`, tmpDir, tmpDir), diff.String())
})
Expand All @@ -112,8 +112,8 @@ func TestLocalizer_TransformDestination(t *testing.T) {
[10](http://myproject.example.com/tip/a/does_not_exists_file.md) [11](https://myproject.example.com/tip/a/does_not_exists_file2) [12](http://myproject.example.com/tip/does_not_exists/does_not_exists_dir.md)
-[11](/doc2.md) [12](/a/doc.md#yolo) [13](../doc2.md) [14](../a/../a/../a/../a/doc.md) [15](doc.md)
+[11](../doc2.md) [12](#yolo) [13](../doc2.md) [14](.) [15](.)
-[11](/doc2.md) [12](/a/doc.md#yolo) [13](../doc2.md) [14](../a/../a/../a/../a/doc.md) [15](doc.md) [16](doc2.md/#yolo-2)
+[11](../doc2.md) [12](#yolo) [13](../doc2.md) [14](.) [15](.) [16](../doc2.md#yolo-2)
`, tmpDir, tmpDir), diff.String())
})
Expand Down

0 comments on commit 50f4844

Please sign in to comment.