From ad0e26dc04008feec8de0603c88fbd63f87c18ec Mon Sep 17 00:00:00 2001 From: Tobi Smethurst <31960611+tsmethurst@users.noreply.github.com> Date: Mon, 26 Jul 2021 20:25:54 +0200 Subject: Markdown Statuses (#116) * parse markdown statuses if desired * add some preliminary docs for writing posts --- internal/processing/status/util.go | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) (limited to 'internal/processing/status/util.go') diff --git a/internal/processing/status/util.go b/internal/processing/status/util.go index eb83babb0..b4d115f8d 100644 --- a/internal/processing/status/util.go +++ b/internal/processing/status/util.go @@ -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 '
' - content := fmt.Sprintf(`
%s
`, 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(`@%s`, 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(`#%s`, 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", "