Skip to content

Commit

Permalink
Fix i18n section links (#52)
Browse files Browse the repository at this point in the history
* Fix i18n section links

Signed-off-by: Saswata Mukherjee <[email protected]>

* Implement suggestions

Signed-off-by: Saswata Mukherjee <[email protected]>
  • Loading branch information
saswatamcode authored Jun 28, 2021
1 parent 1b32e8f commit ce7adf8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pkg/mdformatter/linktransformer/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,17 +387,24 @@ func (l localLinksCache) addRelLinks(localLink string) error {

func toHeaderID(header []byte) string {
var id []byte
// Remove punctuation from header except '-' or '#'.
// '\p{L}\p{N}\p{M}' is the unicode equivalent of '\w', https://www.regular-expressions.info/unicode.html.
punctuation := regexp.MustCompile(`[^\p{L}\p{N}\p{M}-# ]`)
header = punctuation.ReplaceAll(header, []byte(""))
headerText := bytes.TrimLeft(bytes.ToLower(header), "#")
// If header is just punctuation it comes up empty, so it cannot be linked.
if len(headerText) <= 1 {
return ""
}

for _, h := range bytes.TrimLeft(bytes.ToLower(header), "#")[1:] {
if (h >= 97 && h <= 122) || (h >= 48 && h <= 57) {
id = append(id, h)
}
for _, h := range headerText[1:] {
switch h {
case '{':
return string(id)
case ' ', '-':
id = append(id, '-')
default:
id = append(id, h)
}
}
return string(id)
Expand Down
22 changes: 22 additions & 0 deletions pkg/mdformatter/linktransformer/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,28 @@ func TestValidator_TransformDestination(t *testing.T) {
testutil.Equals(t, 0, len(diff), diff.String())
})

t.Run("check valid local links in diff language", func(t *testing.T) {
testFile := filepath.Join(tmpDir, "repo", "docs", "test", "valid-local-links-diff-lang.md")
testutil.Ok(t, ioutil.WriteFile(testFile, []byte(`# Twój wkład w dokumentację
[1](#twój-wkład-w-dokumentację)
## Hugo का उपयोग करते हुए स्थानीय रूप से साइट चलाना
[2](#hugo-का-उपयोग-करते-हुए-स्थानीय-रूप-से-साइट-चलाना)
`), os.ModePerm))

diff, err := mdformatter.IsFormatted(context.TODO(), logger, []string{testFile})
testutil.Ok(t, err)
testutil.Equals(t, 0, len(diff), diff.String())

diff, err = mdformatter.IsFormatted(context.TODO(), logger, []string{testFile}, mdformatter.WithLinkTransformer(
MustNewValidator(logger, []byte(""), anchorDir),
))
testutil.Ok(t, err)
testutil.Equals(t, 0, len(diff), diff.String())
})

t.Run("check invalid local links", func(t *testing.T) {
testFile := filepath.Join(tmpDir, "repo", "docs", "test", "invalid-local-links.md")
filePath := "/repo/docs/test/invalid-local-links.md"
Expand Down

0 comments on commit ce7adf8

Please sign in to comment.