mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-11-17 08:07:30 -06:00
Markdown Statuses (#116)
* parse markdown statuses if desired * add some preliminary docs for writing posts
This commit is contained in:
parent
e2757ae676
commit
ad0e26dc04
19 changed files with 306 additions and 87 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/id"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/text"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/util"
|
||||
)
|
||||
|
||||
|
|
@ -29,7 +30,7 @@ func (p *processor) Create(account *gtsmodel.Account, application *gtsmodel.Appl
|
|||
Local: true,
|
||||
AccountID: account.ID,
|
||||
AccountURI: account.URI,
|
||||
ContentWarning: util.RemoveHTML(form.SpoilerText),
|
||||
ContentWarning: text.RemoveHTML(form.SpoilerText),
|
||||
ActivityStreamsType: gtsmodel.ActivityStreamsNote,
|
||||
Sensitive: form.Sensitive,
|
||||
Language: form.Language,
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/text"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/visibility"
|
||||
)
|
||||
|
|
@ -40,6 +41,7 @@ type processor struct {
|
|||
config *config.Config
|
||||
db db.DB
|
||||
filter visibility.Filter
|
||||
formatter text.Formatter
|
||||
fromClientAPI chan gtsmodel.FromClientAPI
|
||||
log *logrus.Logger
|
||||
}
|
||||
|
|
@ -51,6 +53,7 @@ func New(db db.DB, tc typeutils.TypeConverter, config *config.Config, fromClient
|
|||
config: config,
|
||||
db: db,
|
||||
filter: visibility.NewFilter(db, log),
|
||||
formatter: text.NewFormatter(config, db, log),
|
||||
fromClientAPI: fromClientAPI,
|
||||
log: log,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package status
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
|
|
@ -238,36 +237,28 @@ func (p *processor) processEmojis(form *apimodel.AdvancedStatusCreateForm, accou
|
|||
}
|
||||
|
||||
func (p *processor) processContent(form *apimodel.AdvancedStatusCreateForm, accountID string, status *gtsmodel.Status) error {
|
||||
// if there's nothing in the status at all we can just return early
|
||||
if form.Status == "" {
|
||||
status.Content = ""
|
||||
return nil
|
||||
}
|
||||
|
||||
// surround the whole status in '<p>'
|
||||
content := fmt.Sprintf(`<p>%s</p>`, form.Status)
|
||||
|
||||
// format mentions nicely
|
||||
for _, menchie := range status.GTSMentions {
|
||||
targetAccount := >smodel.Account{}
|
||||
if err := p.db.GetByID(menchie.TargetAccountID, targetAccount); err == nil {
|
||||
mentionContent := fmt.Sprintf(`<span class="h-card"><a href="%s" class="u-url mention">@<span>%s</span></a></span>`, targetAccount.URL, targetAccount.Username)
|
||||
content = strings.ReplaceAll(content, menchie.NameString, mentionContent)
|
||||
}
|
||||
// if format wasn't specified we should set the default
|
||||
if form.Format == "" {
|
||||
form.Format = apimodel.StatusFormatDefault
|
||||
}
|
||||
|
||||
// format tags nicely
|
||||
for _, tag := range status.GTSTags {
|
||||
tagContent := fmt.Sprintf(`<a href="%s" class="mention hashtag" rel="tag">#<span>%s</span></a>`, tag.URL, tag.Name)
|
||||
content = strings.ReplaceAll(content, fmt.Sprintf("#%s", tag.Name), tagContent)
|
||||
// parse content out of the status depending on what format has been submitted
|
||||
var content string
|
||||
switch form.Format {
|
||||
case apimodel.StatusFormatPlain:
|
||||
content = p.formatter.FromPlain(form.Status, status.GTSMentions, status.GTSTags)
|
||||
case apimodel.StatusFormatMarkdown:
|
||||
content = p.formatter.FromMarkdown(form.Status, status.GTSMentions, status.GTSTags)
|
||||
default:
|
||||
return fmt.Errorf("format %s not recognised as a valid status format", form.Format)
|
||||
}
|
||||
|
||||
// replace newlines with breaks
|
||||
content = strings.ReplaceAll(content, "\n", "<br />")
|
||||
|
||||
// sanitize html to remove any dodgy scripts or other disallowed elements
|
||||
clean := util.SanitizeHTML(content)
|
||||
|
||||
// set the content as the shiny clean parsed content
|
||||
status.Content = clean
|
||||
status.Content = content
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue