diff options
| author | Christian Cleberg <[email protected]> | 2026-07-15 18:01:39 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-07-15 18:01:39 -0500 |
| commit | 9c6ad663cf5901669aaed59cb16482e0f716a313 (patch) | |
| tree | c492d849ae96fb12e19f0bc21be5b8ab14df9c3b /deviantion.go | |
| parent | 971bdb3e8073965ebbb1924864b0442cd3d8dc95 (diff) | |
| download | devianter-0.3.3.tar.gz devianter-0.3.3.tar.bz2 devianter-0.3.3.zip | |
fix: extract text from tiptap bodies, and read the right description field (#4)v0.3.3
Checking the library against the live API disproved what its comments
claimed. Comment and Description were handing back raw JSON blobs, and
the parser this package has always carried could not have prevented it.
DeviantArt no longer stores rich text as Draft.js. It uses tiptap:
Draft.js, what we parse: {"blocks":[{"text":"..."}]}
tiptap, what DA sends: {"version":1,"document":{"type":"doc",...}}
Across 34 live comments: 33 tiptap, 0 Draft.js, and 33 whose .Comment was
the raw JSON. flattenMarkup now walks the tiptap tree — text nodes joined
per block, hardBreak as a newline, entities decoded — and keeps the
Draft.js path for old bodies that still carry it. Nodes with no text of
their own (images, galleries, emotes) drop out, as they always did.
The description had a second, larger bug behind that one. It lives in
Extended.DescriptionText, not TextContent, which GetDeviation read: of 24
deviations sampled, 23 populated the former and 1 the latter. So
Post.Description was empty for nearly every deviation, and the txt[1]
guard removed in 3dad0ea was never what stood in the way. Prefer
Extended.DescriptionText and fall back, since either may be the one set.
Verified against the live API rather than by reading: comments still raw
JSON went 33/34 to 0/34, and descriptions that were "" now return prose.
Also corrects the doc comments that asserted the Draft.js story on
pkg.go.dev, and notes that flattening drops images, links, and emotes.
Diffstat (limited to 'deviantion.go')
| -rw-r--r-- | deviantion.go | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/deviantion.go b/deviantion.go index 5b6df39..f3f9103 100644 --- a/deviantion.go +++ b/deviantion.go @@ -79,9 +79,14 @@ type Media struct { } // Text is a block of user-written text — a description, a comment, a group's -// about page. Markup holds either HTML or a JSON-encoded Draft.js document, -// distinguished by Type; the functions that return a Text generally extract the -// plain text into a neighbouring field, which is easier to use. +// about page. +// +// Markup is a rich document rather than a string of prose, in whichever format +// DeviantArt stored it: tiptap JSON on anything recent (Type is "tiptap"), +// Draft.js JSON on older bodies, or plain HTML on the oldest. The functions +// returning a Text generally flatten it to plain text in a neighbouring field, +// which is what most callers want; read Markup itself for the formatting, +// images, and links that flattening discards. type Text struct { Excerpt string Html struct { @@ -91,8 +96,9 @@ type Text struct { // Post is a deviation together with its comment metadata, as returned by // [GetDeviation]. IMG and Description are conveniences that GetDeviation derives -// from the Deviation, so callers need not assemble a URL or decode Draft.js -// markup themselves. +// from the Deviation, so callers need not assemble a URL or flatten a rich-text +// document themselves. Description is empty for the many deviations that have +// none. // // Comments holds only a total and a cursor. To retrieve the comments, pass them // to [GetComments] with type 1. @@ -175,7 +181,14 @@ func GetDeviation(id string, user string) (st Post, err Error) { st.IMG, _ = UrlFromMedia(st.Deviation.Media) - st.Description = flattenComment(st.Deviation.TextContent.Html.Markup) + // The description lives in Extended.DescriptionText on the great majority of + // deviations; TextContent carries it on only a small minority. Prefer the + // former and fall back, since either may be the populated one. + desc := st.Deviation.Extended.DescriptionText.Html.Markup + if desc == "" { + desc = st.Deviation.TextContent.Html.Markup + } + st.Description = flattenMarkup(desc) return } |
