text

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2023 License: AGPL-3.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Emojify added in v0.6.0

func Emojify(emojis []apimodel.Emoji, inputText string) string

Emojify replaces shortcodes in `inputText` with the emoji in `emojis`.

Callers should ensure that inputText and resulting text are escaped appropriately depending on what they're used for.

func MinifyHTML

func MinifyHTML(in string) string

MinifyHTML minifies the given string under the assumption that it's HTML.

If input is not HTML encoded, this function will try to do minimization anyway, but this may produce unexpected results.

If an error occurs during minimization, it will be logged and the original string returned unmodified.

func NormalizeHashtag added in v0.11.0

func NormalizeHashtag(text string) (string, bool)

NormalizeHashtag normalizes the given hashtag text by removing the initial '#' symbol, and then decomposing and canonically recomposing chars + combining diacritics in the text to single unicode characters, following Normalization Form C (https://unicode.org/reports/tr15/).

Finally, it will do a check on the normalized string to ensure that it's below maximumHashtagLength chars, and contains only unicode letters and numbers. If this passes, returned bool will be true.

func SanitizeToHTML added in v0.11.0

func SanitizeToHTML(in string) string

SanitizeToHTML sanitizes only risky html elements from the given string, allowing safe ones through.

func SanitizeToPlaintext added in v0.11.0

func SanitizeToPlaintext(in string) string

SanitizeToPlaintext runs text through basic sanitization. This removes any html elements that were in the string, and returns clean plaintext.

Types

type FormatFunc added in v0.7.0

type FormatFunc func(ctx context.Context, pmf gtsmodel.ParseMentionFunc, authorID string, statusID string, text string) *FormatResult

type FormatResult added in v0.7.0

type FormatResult struct {
	HTML     string
	Mentions []*gtsmodel.Mention
	Tags     []*gtsmodel.Tag
	Emojis   []*gtsmodel.Emoji
}

type Formatter

type Formatter interface {
	// FromPlain parses an HTML text from a plaintext.
	FromPlain(ctx context.Context, pmf gtsmodel.ParseMentionFunc, authorID string, statusID string, plain string) *FormatResult
	// FromPlainNoParagraph parses an HTML text from a plaintext, without wrapping the resulting text in <p> tags.
	FromPlainNoParagraph(ctx context.Context, pmf gtsmodel.ParseMentionFunc, authorID string, statusID string, plain string) *FormatResult
	// FromMarkdown parses an HTML text from a markdown-formatted text.
	FromMarkdown(ctx context.Context, pmf gtsmodel.ParseMentionFunc, authorID string, statusID string, md string) *FormatResult
	// FromPlainEmojiOnly parses an HTML text from a plaintext, only parsing emojis and not mentions etc.
	FromPlainEmojiOnly(ctx context.Context, pmf gtsmodel.ParseMentionFunc, authorID string, statusID string, plain string) *FormatResult
}

Formatter wraps some logic and functions for parsing statuses and other text input into nice html. Each of the member functions returns a struct containing the formatted HTML and any tags, mentions, and emoji that were found in the text.

func NewFormatter

func NewFormatter(db db.DB) Formatter

NewFormatter returns a new Formatter interface for parsing statuses and other text input into nice html.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL