ref: bdbf7776066e1347915f87d60cdd2dead4121bd8
parent: e0019f69bdca6d7a8c81aa111966a8fe8e6ef083
author: penny <penny@limitedideas.org>
date: Thu Oct 2 13:14:18 EDT 2025
/edit replaces profile urls with their user@domain format
--- /dev/null
+++ b/edit.go
@@ -1,0 +1,57 @@
+package main
+
+import (
+ "errors"
+ "strings"
+ "bytes"
+
+ mastodon "codeberg.org/penny64/hellclient-go-mastodon"
+ "golang.org/x/net/html"
+)
+
+// Replace username links with their user@domain format
+func prepareForEdit(status *mastodon.Status) (string, error) {+ doc, err := html.Parse(strings.NewReader(status.Content))
+ if err != nil {+ return "", errors.New("failed to parse post HTML")+ }
+ mentions := 0
+ for node := range doc.Descendants() {+ if node.Data == "a" && node.Type == html.ElementNode {+ ismention := false
+ for attr := range node.Attr {+ if node.Attr[attr].Key == "class" && strings.Contains(node.Attr[attr].Val, "mention") {+ ismention = true
+ continue
+ }
+ }
+ if ismention {+ for attr := range node.Attr {+ if node.Attr[attr].Key == "href" {+ node.Attr[attr].Val = status.Mentions[mentions].Acct
+ mentions++
+ }
+ }
+ }
+ }
+ }
+
+ //Rip off the HTML body the parser made for us
+ for node := range doc.Descendants() {+ if node.Data == "body" {+ node.Type = html.DocumentNode
+ doc = node
+ }
+ }
+
+ var rendered bytes.Buffer
+ err = html.Render(&rendered, doc)
+
+ if err != nil {+ return "", err
+ }
+
+ renderedPlainText := rendered.String()
+
+ return renderedPlainText, nil
+}
--- a/main.go
+++ b/main.go
@@ -320,7 +320,12 @@
return
case "edit":
if content == "" || content == " " {- rl.SetDefault(fmt.Sprintf("/edit %v %v", index, html2text.HTML2Text(postItem.Content)))+ fixedHTML, err := prepareForEdit(postItem)
+ if err != nil {+ fmt.Printf("Error loading post HTML: %s\n", err)+ return
+ }
+ rl.SetDefault(fmt.Sprintf("/edit %v %v", index, html2text.HTML2Text(fixedHTML)))return
}
var MediaIDs []mastodon.ID
--- a/mastodon.go
+++ b/mastodon.go
@@ -79,7 +79,8 @@
func (hc *Hellclient) renderStatus(content string, index string) (string, map[string]string) {doc, err := html.Parse(strings.NewReader(content))
if err != nil {- log.Fatal(err)
+ fmt.Printf("Failed to parse status\n")+ return "", nil
}
//clear out the url map
--
⑨