From 384b97c96e970e22410d69f4fca333d4327bcc4a Mon Sep 17 00:00:00 2001 From: Saswata Mukherjee Date: Thu, 24 Jun 2021 12:18:11 +0530 Subject: [PATCH 1/2] Fix i18n section links 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..8682b46 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 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" From 564e77a6f5b6cdc58bf6e2e9da77197cfe6dc1e9 Mon Sep 17 00:00:00 2001 From: Saswata Mukherjee Date: Fri, 25 Jun 2021 08:21:47 +0530 Subject: [PATCH 2/2] Implement suggestions Signed-off-by: Saswata Mukherjee --- pkg/mdformatter/linktransformer/link.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/mdformatter/linktransformer/link.go b/pkg/mdformatter/linktransformer/link.go index 8682b46..5fa029a 100644 --- a/pkg/mdformatter/linktransformer/link.go +++ b/pkg/mdformatter/linktransformer/link.go @@ -392,7 +392,7 @@ func toHeaderID(header []byte) string { 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 cannot be linked. + // If header is just punctuation it comes up empty, so it cannot be linked. if len(headerText) <= 1 { return "" }