richtext

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package richtext provides utilities for converting between Markdown and HTML. It uses glamour for terminal-friendly Markdown rendering.

Index

Constants

This section is empty.

Variables

View Source
var ErrMentionSkip = errors.New("mention skip")

ErrMentionSkip is a sentinel error that lookup functions can return to indicate that a fuzzy @Name mention should be left as plain text instead of failing the entire operation. Use this for recoverable errors like not-found or ambiguous.

Functions

func AttachmentToHTML

func AttachmentToHTML(sgid, filename, contentType string) string

AttachmentToHTML builds a <bc-attachment> tag for embedding in Trix-compatible HTML.

func DetectMIME

func DetectMIME(path string) string

DetectMIME returns the MIME type for a file path. It uses the extension map first, then falls back to reading file header bytes.

func EmbedAttachments

func EmbedAttachments(html string, attachments []AttachmentRef) string

EmbedAttachments appends <bc-attachment> tags to HTML content. Each attachment is added as a separate block after the main content.

func HTMLToMarkdown

func HTMLToMarkdown(html string) string

HTMLToMarkdown converts HTML content to Markdown. This is useful for displaying Basecamp's rich text content in the terminal.

func Hyperlink(text, url string) string

Hyperlink wraps text in an OSC 8 terminal hyperlink sequence. Returns text unchanged when url is empty. The URL is sanitized to strip control characters that could break out of the OSC 8 sequence or inject terminal commands.

func IsHTML

func IsHTML(s string) bool

IsHTML attempts to detect if the input string contains HTML. Only returns true for well-formed HTML with common content tags. Does not detect arbitrary tags like <script> to prevent XSS passthrough. Tags inside Markdown code spans (`...`) and fenced code blocks (```) are ignored.

func IsMarkdown

func IsMarkdown(s string) bool

IsMarkdown attempts to detect if the input string is Markdown rather than plain text or HTML. This is a heuristic and may not be 100% accurate.

func LinkifyMarkdownLinks(text string) string

LinkifyMarkdownLinks converts markdown-style [text](url) links to OSC 8 terminal hyperlinks where the link text is clickable. Use this for rendering paths that bypass glamour (e.g., chat).

func LinkifyURLs added in v0.3.0

func LinkifyURLs(text string) string

LinkifyURLs wraps bare URLs in OSC 8 hyperlink sequences. URLs already inside an OSC 8 sequence are not double-wrapped.

func MarkdownToHTML

func MarkdownToHTML(md string) string

MarkdownToHTML converts Markdown text to HTML suitable for Basecamp's rich text fields. It handles common Markdown syntax: headings, bold, italic, links, lists, code blocks, and blockquotes. If the input already appears to be HTML, it is returned unchanged to preserve existing formatting.

func MentionToHTML added in v0.7.0

func MentionToHTML(sgid, name string) string

MentionToHTML builds a <bc-attachment> mention tag.

func NormalizeDragPath added in v0.3.0

func NormalizeDragPath(raw string) string

NormalizeDragPath normalizes a pasted/dragged path into a filesystem path. It handles quoted paths, file:// URLs, shell-escaped characters, and tilde expansion. Only inputs that look like filesystem paths (absolute, ~/, file://, or quoted versions of these) are transformed; other inputs are returned unchanged. Returns empty for empty input.

This targets macOS and Linux terminals only. It compiles on Windows to avoid breaking cross-compilation, but Windows drag-and-drop (e.g. file:// URLs with drive letters) is not supported or tested.

func RenderMarkdown

func RenderMarkdown(md string) (string, error)

RenderMarkdown renders Markdown for terminal display using glamour. It returns styled output suitable for CLI display.

func RenderMarkdownWithWidth

func RenderMarkdownWithWidth(md string, width int) (string, error)

RenderMarkdownWithWidth renders Markdown for terminal display with a custom width.

func ValidateFile

func ValidateFile(path string) error

ValidateFile checks that a path refers to an existing, regular, readable file within the size limit. Returns nil on success.

Types

type AttachmentRef

type AttachmentRef struct {
	SGID        string
	Filename    string
	ContentType string
}

AttachmentRef holds the metadata needed to embed a <bc-attachment> in HTML.

type MentionLookupFunc added in v0.7.0

type MentionLookupFunc func(name string) (sgid, displayName string, err error)

MentionLookupFunc resolves a name to an attachable SGID and display name.

type MentionResult added in v0.7.0

type MentionResult struct {
	HTML       string
	Unresolved []string
}

MentionResult holds the resolved HTML and any mentions that could not be resolved.

func ResolveMentions added in v0.7.0

func ResolveMentions(html string, lookup MentionLookupFunc, lookupByID PersonByIDFunc) (MentionResult, error)

ResolveMentions processes mention syntax in HTML in three passes:

  1. Markdown mention anchors: <a href="mention:SGID">@Name</a> and <a href="person:ID">@Name</a>
  2. Inline @sgid:VALUE syntax
  3. Fuzzy @Name and @First.Last patterns

Each pass replaces matches with <bc-attachment> tags. Subsequent passes skip regions already converted by earlier passes via isInsideBcAttachment.

lookupByID may be nil if person:ID syntax is not needed; encountering a person:ID anchor with a nil lookupByID returns an error.

type ParsedAttachment added in v0.7.0

type ParsedAttachment struct {
	SGID        string `json:"sgid,omitempty"`
	Filename    string `json:"filename,omitempty"`
	ContentType string `json:"content_type,omitempty"`
	Filesize    string `json:"filesize,omitempty"`
	URL         string `json:"url,omitempty"`
	Href        string `json:"href,omitempty"`
	Width       string `json:"width,omitempty"`
	Height      string `json:"height,omitempty"`
	Caption     string `json:"caption,omitempty"`
}

ParsedAttachment holds metadata extracted from a <bc-attachment> tag in HTML content.

func ParseAttachments added in v0.7.0

func ParseAttachments(content string) []ParsedAttachment

ParseAttachments extracts file attachment metadata from HTML content. It finds all <bc-attachment> tags and returns their metadata, excluding mention attachments (content-type="application/vnd.basecamp.mention").

func (*ParsedAttachment) DisplayName added in v0.7.0

func (a *ParsedAttachment) DisplayName() string

DisplayName returns the best display name: caption, then filename, then fallback.

func (*ParsedAttachment) DisplayURL added in v0.7.0

func (a *ParsedAttachment) DisplayURL() string

DisplayURL returns the best available URL for the attachment.

func (*ParsedAttachment) IsImage added in v0.7.0

func (a *ParsedAttachment) IsImage() bool

IsImage returns true if the attachment has an image content type.

type PersonByIDFunc added in v0.7.0

type PersonByIDFunc func(id string) (sgid, canonicalName string, err error)

PersonByIDFunc resolves a person ID to an attachable SGID and canonical name. Used by the person:ID mention syntax.

Jump to

Keyboard shortcuts

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