Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix local anchor link issue #21

Merged
merged 1 commit into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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