From ce7adf847d0ae8e03bc5586ad0a54f6c24dc7c9e Mon Sep 17 00:00:00 2001 From: Saswata Mukherjee Date: Mon, 28 Jun 2021 16:16:59 +0530 Subject: [PATCH] Fix i18n section links (#52) * Fix i18n section links Signed-off-by: Saswata Mukherjee * Implement suggestions Signed-off-by: Saswata Mukherjee --- pkg/mdformatter/linktransformer/link.go | 15 +++++++++---- pkg/mdformatter/linktransformer/link_test.go | 22 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/pkg/mdformatter/linktransformer/link.go b/pkg/mdformatter/linktransformer/link.go index b25ad21..5fa029a 100644 --- a/pkg/mdformatter/linktransformer/link.go +++ b/pkg/mdformatter/linktransformer/link.go @@ -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) diff --git a/pkg/mdformatter/linktransformer/link_test.go b/pkg/mdformatter/linktransformer/link_test.go index 4e33a1f..d1d9a49 100644 --- a/pkg/mdformatter/linktransformer/link_test.go +++ b/pkg/mdformatter/linktransformer/link_test.go @@ -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"