Skip to content

Commit

Permalink
Add support for shortcode-like feature in posts
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusgomes28 committed Mar 8, 2024
1 parent 31d7cf6 commit 2ca5043
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
78 changes: 77 additions & 1 deletion admin-app/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package admin_app

import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"

"github.com/gin-gonic/gin"
"github.com/matheusgomes28/urchin/database"
Expand Down Expand Up @@ -65,10 +68,20 @@ func postPostHandler(database database.Database) func(*gin.Context) {
return
}

transformed_content, err := transformContent(add_post_request.Content)
if err != nil {
log.Warn().Msgf("could not transform post: %v", err)
c.JSON(http.StatusBadRequest, gin.H{
"error": "invalid request body",
"msg": err.Error(),
})
return
}

id, err := database.AddPost(
add_post_request.Title,
add_post_request.Excerpt,
add_post_request.Content,
transformed_content,
)
if err != nil {
log.Error().Msgf("failed to add post: %v", err)
Expand Down Expand Up @@ -153,3 +166,66 @@ func deletePostHandler(database database.Database) func(*gin.Context) {
})
}
}

// partitionString will partition the strings by
// removing the given ranges
func partitionString(text string, indexes [][]int) []string {

partitions := make([]string, 0)
start := 0
for _, window := range indexes {
partitions = append(partitions, text[start:window[0]])
start = window[1]
}

partitions = append(partitions, text[start:len(text)-1])
return partitions
}

func shortcodeToMarkdown(shortcode string) (string, error) {
key_value := strings.Split(shortcode, ":")

key := key_value[0]
values := key_value[1:]

if key == "img" {
if len(values) == 1 {
image_src := fmt.Sprintf("/media/%s", values[0])
return fmt.Sprintf("![image](%s)", image_src), nil
} else if len(values) == 2 {
image_src := fmt.Sprintf("/media/%s", values[0])
alt_text := values[1]
return fmt.Sprintf("![%s](%s)", alt_text, image_src), nil
} else {
return "", fmt.Errorf("invalid shortcode: %s", shortcode)
}
}

return "", fmt.Errorf("unsupported shortcode: %s", key)
}

func transformContent(content string) (string, error) {
// Find all the occurences of {{ and }}
regex, _ := regexp.Compile(`{{[\w.-]+(:[\w.-]+)+}}`)

shortcodes := regex.FindAllStringIndex(content, -1)
partitions := partitionString(content, shortcodes)

builder := strings.Builder{}
i := 0
for i, shortcode := range(shortcodes) {
builder.WriteString(partitions[i])

markdown, err := shortcodeToMarkdown(content[shortcode[0]+2:shortcode[1]-2])
if err != nil {
return "", err
}
builder.WriteString(markdown)
}

// Guaranteed to have +1 than the number of
// shortcodes by algorithm
builder.WriteString(partitions[i+1])

return builder.String(), nil
}
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func SetupRoutes(app_settings common.AppSettings, database database.Database) *g
r.POST("/contact-send", makeContactFormHandler())

r.Static("/static", "./static")
r.Static("/media", "./media")
return r
}

Expand Down

0 comments on commit 2ca5043

Please sign in to comment.