Expand description
Single-pass content parser for Nostr text-bearing events.
Nostr clients render kind:1 notes (and a long tail of similar
kinds) by walking the .content string and recognising four
syntactic affordances:
- NIP-21
nostr:URIs (NIP-21) — embedded references to profiles, events, addressable coordinates, …; - HTTP(S) and other scheme URLs — to be rendered as hyperlinks;
#hashtags(NIP-12) — folksonomy markers that thetfilter key indexes;\nline breaks — for layout.
NostrParser is a single-pass tokeniser that yields the
Token stream representing those affordances plus the
interspersed plain-text spans. Callers configure which affordances
to recognise via NostrParserOptions; a disabled affordance
falls through to plain text untouched.
§Why this exists
Earlier nula releases shipped two narrowly-scoped scanners:
crate::nips::nip27::references_in (NIP-21 only) and
crate::nips::nip27::tags_from_content (tag harvester).
Renderers had to layer their own URL / hashtag detection on top.
This module unifies those concerns behind a single iterator so a
UI layer can drive its rendering loop with one walk over the
source string.
§Token kinds
| Variant | Borrow scope | Spec |
|---|---|---|
Token::Text | &'a str | n/a |
Token::Nostr | owned Nip21 | NIP-21 |
Token::Url | owned Url | n/a (RFC-3986) |
Token::Hashtag | &'a str (without leading #) | NIP-12 |
Token::LineBreak | n/a | n/a |
§Example
use nula_core::parser::{NostrParser, NostrParserOptions, Token};
let parser = NostrParser::new();
let opts = NostrParserOptions::default();
let mut tokens = parser.parse("Visit https://example.com #rust", opts);
assert!(matches!(tokens.next(), Some(Token::Text(_))));
assert!(matches!(tokens.next(), Some(Token::Url(_))));Structs§
- Nostr
Parser - Stateless factory that turns a
&strinto aNostrParserIter. - Nostr
Parser Iter - Iterator yielded by
NostrParser::parse. - Nostr
Parser Options - Knobs that control which affordances
NostrParserrecognises.
Enums§
- Token
- One unit produced by
NostrParser::parse.