Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

fix: support short id

Sonui62b9104a6807
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)
+ })
}