This is an automated email from the ASF dual-hosted git repository.

linkinstar pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git

commit 62b9104a68077a9fd5c92c3a66a89ac2ed5e10fa
Author: Sonui <[email protected]>
AuthorDate: Fri Oct 11 00:49:15 2024 +0800

    fix: support short id
---
 pkg/checker/question_link.go      | 24 +++++++++++++++---------
 pkg/checker/question_link_test.go | 24 ++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/pkg/checker/question_link.go b/pkg/checker/question_link.go
index ca8327b6..36f850d6 100644
--- a/pkg/checker/question_link.go
+++ b/pkg/checker/question_link.go
@@ -22,6 +22,7 @@ package checker
 import (
        "github.com/apache/incubator-answer/internal/base/constant"
        "github.com/apache/incubator-answer/pkg/obj"
+       "github.com/apache/incubator-answer/pkg/uid"
 )
 
 const (
@@ -60,7 +61,7 @@ func GetQuestionLink(content string) []QuestionLink {
 }
 
 func processURL(content string, left, right *int, uniqueIDs 
map[string]struct{}, questionLinks *[]QuestionLink) {
-       for *right < len(content) && isDigit(content[*right]) {
+       for *right < len(content) && (isDigit(content[*right]) || 
isLetter(content[*right])) {
                *right++
        }
        questionID := content[*left+len("/questions/") : *right]
@@ -69,7 +70,7 @@ func processURL(content string, left, right *int, uniqueIDs 
map[string]struct{},
        if *right < len(content) && content[*right] == '/' {
                *left = *right + 1
                *right = *left
-               for *right < len(content) && isDigit(content[*right]) {
+               for *right < len(content) && (isDigit(content[*right]) || 
isLetter(content[*right])) {
                        *right++
                }
                answerID = content[*left:*right]
@@ -79,7 +80,7 @@ func processURL(content string, left, right *int, uniqueIDs 
map[string]struct{},
 }
 
 func processID(content string, left, right *int, uniqueIDs 
map[string]struct{}, questionLinks *[]QuestionLink) {
-       for *right < len(content) && isDigit(content[*right]) {
+       for *right < len(content) && (isDigit(content[*right]) || 
isLetter(content[*right])) {
                *right++
        }
        id := content[*left:*right]
@@ -90,14 +91,19 @@ func isDigit(c byte) bool {
        return c >= '0' && c <= '9'
 }
 
-func addUniqueID(questionID, answerID string, linkType int, uniqueIDs 
map[string]struct{}, questionLinks *[]QuestionLink) {
-       if questionID == "" && answerID == "" {
-               return
-       }
+func isLetter(c byte) bool {
+       return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
+}
 
+func addUniqueID(questionID, answerID string, linkType int, uniqueIDs 
map[string]struct{}, questionLinks *[]QuestionLink) {
        isAdd := false
        if answerID != "" {
-               if objectType, err := obj.GetObjectTypeStrByObjectID(answerID); 
err == nil && objectType == constant.AnswerObjectType {
+               objectType, err := 
obj.GetObjectTypeStrByObjectID(uid.DeShortID(answerID))
+               if err != nil {
+                       answerID = ""
+               }
+
+               if objectType == constant.AnswerObjectType {
                        if _, ok := uniqueIDs[answerID]; !ok {
                                uniqueIDs[answerID] = struct{}{}
                                isAdd = true
@@ -105,7 +111,7 @@ func addUniqueID(questionID, answerID string, linkType int, 
uniqueIDs map[string
                }
        }
 
-       if objectType, err := obj.GetObjectTypeStrByObjectID(questionID); err 
== nil {
+       if objectType, err := 
obj.GetObjectTypeStrByObjectID(uid.DeShortID(questionID)); err == nil {
                if _, ok := uniqueIDs[questionID]; !ok {
                        uniqueIDs[questionID] = struct{}{}
                        isAdd = true
diff --git a/pkg/checker/question_link_test.go 
b/pkg/checker/question_link_test.go
index 6b9f9483..bc4eb615 100644
--- a/pkg/checker/question_link_test.go
+++ b/pkg/checker/question_link_test.go
@@ -157,4 +157,28 @@ func TestGetQuestionLink(t *testing.T) {
                links := 
checker.GetQuestionLink("https://example.com/questions/10110000000000060";)
                assert.Empty(t, links)
        })
+
+       // step 15: SEO options
+       t.Run("SEO options", func(t *testing.T) {
+               content := `
+               URL1: http://localhost:3000/questions/D1I2
+               URL2: http://localhost:3000/questions/D1I2/hello
+               URL3: http://localhost:3000/questions/10010000000000068
+               URL4: http://localhost:3000/questions/10010000000000068/hello
+               ERROR URL: http://localhost:3000/questions/AAAA/BBBB
+               `
+               links := checker.GetQuestionLink(content)
+               assert.Equal(t, []checker.QuestionLink{
+                       {
+                               LinkType:   checker.QuestionLinkTypeURL,
+                               QuestionID: "D1I2",
+                               AnswerID:   "",
+                       },
+                       {
+                               LinkType:   checker.QuestionLinkTypeURL,
+                               QuestionID: "10010000000000068",
+                               AnswerID:   "",
+                       },
+               }, links)
+       })
 }

Reply via email to