Created
November 5, 2025 15:07
-
-
Save postspectacular/1d7ebdc5a81894c16ab744cb8d25c320 to your computer and use it in GitHub Desktop.
Example code to convert the body of a single Mastodon post to Markdown (see https://mastodon.thi.ng/@toxi/115496400071927410 for details)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { parseHtml } from "@thi.ng/hiccup-html-parse"; | |
| import { serialize } from "@thi.ng/hiccup-markdown"; | |
| import { arrayZipper, type Location } from "@thi.ng/zipper"; | |
| const HOST = "mastodon.thi.ng"; | |
| const STATUS_ID = "115464108396925195"; | |
| // load a Mastodon status via API | |
| const res = await ( | |
| await fetch(`https://${HOST}/api/v1/statuses/${STATUS_ID}`) | |
| ).json(); | |
| // parse HTML content into thing/hiccup format (nested JS arrays) | |
| const parsed = parseHtml(res.content, { whitespace: true }).result!; | |
| // structure of parsed example: | |
| // [["p", {}, "text"], ["p", {}, ...], ...] | |
| // recursively traverse result document/array using thi.ng/zipper | |
| // and replace all <span> elements with their raw text body | |
| let loc: Location<any> | undefined = arrayZipper(parsed); | |
| do { | |
| // move to logically next item (depth-first) | |
| loc = loc.next; | |
| // check if current loc is a span element and if so transform it | |
| if (Array.isArray(loc?.node) && loc?.node[0] == "span") { | |
| loc = loc.replace(loc.node[2]); | |
| } | |
| // (insert other transforms...) | |
| // ... | |
| } while (loc?.next); | |
| // serialize hiccup to markdown | |
| console.log(serialize(loc?.root)); | |
| /* | |
| Example result (in markdown format): | |
| This week I've been rewriting (in Zig) the position-based dynamics & | |
| cellular-automata engines used for the cloth sim in my | |
| [#DANZA](https://mastodon.thi.ng/tags/DANZA) art project (check the hashtag for | |
| older WIP)... Still endless tweaking, but could already stare at this beauty for | |
| hours, literally! | |
| (And of course, I'm also again "dogfooding" | |
| [http://thi.ng/genart-api](http://thi.ng/genart-api) for defining/handling | |
| parameters and animation timing...) | |
| [#GenerativeArt](https://mastodon.thi.ng/tags/GenerativeArt) | |
| [#AlgorithmicArt](https://mastodon.thi.ng/tags/AlgorithmicArt) | |
| [#CellularAutomata](https://mastodon.thi.ng/tags/CellularAutomata) | |
| [#Physics](https://mastodon.thi.ng/tags/Physics) | |
| [#Simulation](https://mastodon.thi.ng/tags/Simulation) | |
| [#GenArtAPI](https://mastodon.thi.ng/tags/GenArtAPI) | |
| [#Zig](https://mastodon.thi.ng/tags/Zig) | |
| [#WASM](https://mastodon.thi.ng/tags/WASM) | |
| [#WebAssembly](https://mastodon.thi.ng/tags/WebAssembly) | |
| [#NoAI](https://mastodon.thi.ng/tags/NoAI) | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment