mirror of
https://github.com/coaidev/coai.git
synced 2025-05-19 13:00:14 +09:00
70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package api
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/russross/blackfriday/v2"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
const maxColumnPerLine = 50
|
|
|
|
func ProcessMarkdownLine(source []byte) string {
|
|
segment := strings.Split(string(source), "\n")
|
|
var result []rune
|
|
for _, line := range segment {
|
|
data := []rune(line)
|
|
length := len([]rune(line))
|
|
if length < maxColumnPerLine {
|
|
result = append(result, data...)
|
|
result = append(result, '\n')
|
|
} else {
|
|
for i := 0; i < length; i += maxColumnPerLine {
|
|
if i+maxColumnPerLine < length {
|
|
result = append(result, data[i:i+maxColumnPerLine]...)
|
|
result = append(result, '\n')
|
|
} else {
|
|
result = append(result, data[i:]...)
|
|
result = append(result, '\n')
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return string(result)
|
|
}
|
|
|
|
func MarkdownConvert(text string) string {
|
|
if text == "" {
|
|
return ""
|
|
}
|
|
|
|
result := blackfriday.Run([]byte(text))
|
|
return string(result)
|
|
}
|
|
|
|
func CardAPI(c *gin.Context) {
|
|
var body AnonymousRequestBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "invalid request body",
|
|
})
|
|
}
|
|
message := strings.TrimSpace(body.Message)
|
|
if len(message) == 0 {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "message is empty",
|
|
})
|
|
return
|
|
}
|
|
|
|
key, res, err := GetAnonymousResponseWithCache(c, message, body.Web)
|
|
if err != nil {
|
|
res = "There was something wrong..."
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": MarkdownConvert(res),
|
|
"keyword": key,
|
|
})
|
|
}
|