From 5fbaf5b7bec6f59bb868dd8b76a90c1bbd2986b5 Mon Sep 17 00:00:00 2001 From: tobi Date: Mon, 30 Jun 2025 12:56:50 +0200 Subject: [feature] Allow anchor href to work for footnotes, use ID prefix to avoid clashes (#4298) Updates markdown parser + sanitizer to allow footnote anchors to work properly, with appropriate roles. Footnote anchor IDs and backrefs use the status ID as a prefix to avoid clashes, so that footnotes don't break when multiple footnoted statuses are rendered on the same page (eg., in a thread or on the account's home page). closes https://codeberg.org/superseriousbusiness/gotosocial/issues/4296 Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4298 Co-authored-by: tobi Co-committed-by: tobi --- internal/text/markdown.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'internal/text/markdown.go') diff --git a/internal/text/markdown.go b/internal/text/markdown.go index e4b633e75..8486d6935 100644 --- a/internal/text/markdown.go +++ b/internal/text/markdown.go @@ -24,6 +24,7 @@ import ( "strings" "code.superseriousbusiness.org/gotosocial/internal/gtsmodel" + "code.superseriousbusiness.org/gotosocial/internal/id" "code.superseriousbusiness.org/gotosocial/internal/log" "code.superseriousbusiness.org/gotosocial/internal/regexes" "codeberg.org/gruf/go-byteutil" @@ -118,6 +119,18 @@ func (f *Formatter) fromMarkdown( } } + // Inject a footnote ID prefix to avoid + // footnote ID clashes. StatusID isn't + // always set (eg., when parsing instance + // description markdown), so take a random + // ULID if it's not. + var footnoteIDPrefix string + if statusID != "" { + footnoteIDPrefix = statusID + "-" + } else { + footnoteIDPrefix = id.NewULID() + "-" + } + // Instantiate goldmark parser for // markdown, using custom renderer // to add hashtag/mention links. @@ -141,7 +154,9 @@ func (f *Formatter) fromMarkdown( extension.NewLinkify( extension.WithLinkifyURLRegexp(regexes.URLLike), ), - extension.Footnote, + extension.NewFootnote( + extension.WithFootnoteIDPrefix(footnoteIDPrefix), + ), extension.Strikethrough, ), ) -- cgit v1.2.3