feat(shared): extractUrls helper for multi-embed message parsing (P666)
Export extractUrls up to N unique http(s) URLs; extractFirstUrl delegates to first entry for backward-compatible unfurl policy. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
+20
-2
@@ -63,8 +63,25 @@ function extractMentionNames (content) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function extractFirstUrl (content) {
|
function extractFirstUrl (content) {
|
||||||
const m = String(content ?? '').match(URL_RE)
|
const urls = extractUrls(content, 1)
|
||||||
return m ? m[0] : null
|
return urls[0] || null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Up to `maxCount` unique http(s) URLs in message order (Phase 666). */
|
||||||
|
function extractUrls (content, maxCount = 3) {
|
||||||
|
const cap = Math.max(1, Math.min(Number(maxCount) || 3, 10))
|
||||||
|
const seen = new Set()
|
||||||
|
const out = []
|
||||||
|
const re = new RegExp(URL_RE.source, URL_RE.flags)
|
||||||
|
let m
|
||||||
|
while ((m = re.exec(String(content ?? ''))) && out.length < cap) {
|
||||||
|
const url = m[0]
|
||||||
|
if (!seen.has(url)) {
|
||||||
|
seen.add(url)
|
||||||
|
out.push(url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Local link card metadata (no network fetch — P2P-safe). */
|
/** Local link card metadata (no network fetch — P2P-safe). */
|
||||||
@@ -120,6 +137,7 @@ module.exports = {
|
|||||||
formatMessageContent,
|
formatMessageContent,
|
||||||
extractMentionNames,
|
extractMentionNames,
|
||||||
extractFirstUrl,
|
extractFirstUrl,
|
||||||
|
extractUrls,
|
||||||
linkPreviewMeta,
|
linkPreviewMeta,
|
||||||
buildLinkPreviewHtml,
|
buildLinkPreviewHtml,
|
||||||
buildRichEmbedHtml,
|
buildRichEmbedHtml,
|
||||||
|
|||||||
@@ -642,6 +642,7 @@ const {
|
|||||||
formatMessageContent,
|
formatMessageContent,
|
||||||
extractMentionNames,
|
extractMentionNames,
|
||||||
extractFirstUrl,
|
extractFirstUrl,
|
||||||
|
extractUrls,
|
||||||
linkPreviewMeta,
|
linkPreviewMeta,
|
||||||
buildLinkPreviewHtml,
|
buildLinkPreviewHtml,
|
||||||
buildRichEmbedHtml,
|
buildRichEmbedHtml,
|
||||||
@@ -734,6 +735,7 @@ module.exports = {
|
|||||||
formatMessageContent,
|
formatMessageContent,
|
||||||
extractMentionNames,
|
extractMentionNames,
|
||||||
extractFirstUrl,
|
extractFirstUrl,
|
||||||
|
extractUrls,
|
||||||
linkPreviewMeta,
|
linkPreviewMeta,
|
||||||
buildLinkPreviewHtml,
|
buildLinkPreviewHtml,
|
||||||
buildRichEmbedHtml,
|
buildRichEmbedHtml,
|
||||||
|
|||||||
Reference in New Issue
Block a user