parser: always convert damus.io links to bech32 mentions
Update our note parse to always interpret damus.io links as bech32 mentions (nostr:npub...) This means links will get converted to nostr: on post composition, and if we ever see a link it will also get converted to nostr: visually Changelog-Added: Always convert damus.io links to inline mentions Fixes: https://github.com/damus-io/damus/issues/690
This commit is contained in:
@@ -169,6 +169,10 @@ static int consume_url_host(struct cursor *cur)
|
|||||||
|
|
||||||
static int parse_url(struct cursor *cur, struct note_block *block) {
|
static int parse_url(struct cursor *cur, struct note_block *block) {
|
||||||
u8 *start = cur->p;
|
u8 *start = cur->p;
|
||||||
|
u8 *host;
|
||||||
|
int host_len;
|
||||||
|
struct cursor path_cur;
|
||||||
|
struct nostr_bech32 bech32;
|
||||||
|
|
||||||
if (!parse_str(cur, "http"))
|
if (!parse_str(cur, "http"))
|
||||||
return 0;
|
return 0;
|
||||||
@@ -185,14 +189,36 @@ static int parse_url(struct cursor *cur, struct note_block *block) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(consume_url_host(cur) &&
|
// make sure to save the hostname. We will use this to detect damus.io links
|
||||||
consume_url_path(cur) &&
|
host = cur->p;
|
||||||
consume_url_fragment(cur)))
|
|
||||||
{
|
if (!consume_url_host(cur)) {
|
||||||
cur->p = start;
|
cur->p = start;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the length of the host string
|
||||||
|
host_len = cur->p - host;
|
||||||
|
|
||||||
|
// save the current parse state so that we can continue from here when
|
||||||
|
// parsing the bech32 in the damus.io link if we have it
|
||||||
|
copy_cursor(cur, &path_cur);
|
||||||
|
|
||||||
|
// skip leading /
|
||||||
|
if (!cursor_skip(&path_cur, 1)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!consume_url_path(cur)) {
|
||||||
|
cur->p = start;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!consume_url_fragment(cur)) {
|
||||||
|
cur->p = start;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// smart parens
|
// smart parens
|
||||||
if (start - 1 >= 0 &&
|
if (start - 1 >= 0 &&
|
||||||
start < cur->end &&
|
start < cur->end &&
|
||||||
@@ -203,6 +229,19 @@ static int parse_url(struct cursor *cur, struct note_block *block) {
|
|||||||
cur->p--;
|
cur->p--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// save the bech32 string pos in case we hit a damus.io link
|
||||||
|
block->block.str.start = path_cur.p;
|
||||||
|
|
||||||
|
// if we have a damus link, make it a mention
|
||||||
|
if (host_len == 8
|
||||||
|
&& !strncmp(host, "damus.io", 8)
|
||||||
|
&& parse_nostr_bech32(&path_cur, &block->block.mention_bech32.bech32))
|
||||||
|
{
|
||||||
|
block->block.str.end = path_cur.p;
|
||||||
|
block->type = BLOCK_MENTION_BECH32;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
block->type = BLOCK_URL;
|
block->type = BLOCK_URL;
|
||||||
block->block.str.start = (const char *)start;
|
block->block.str.start = (const char *)start;
|
||||||
block->block.str.end = (const char *)cur->p;
|
block->block.str.end = (const char *)cur->p;
|
||||||
|
|||||||
Reference in New Issue
Block a user