Skip to content

Commit

Permalink
fix: support short id
Browse files Browse the repository at this point in the history
  • Loading branch information
sosyz authored and LinkinStars committed Oct 12, 2024
1 parent b328a2b commit 62b9104
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
24 changes: 15 additions & 9 deletions pkg/checker/question_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -90,22 +91,27 @@ 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
}
}
}

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
Expand Down
24 changes: 24 additions & 0 deletions pkg/checker/question_link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit 62b9104

Please sign in to comment.