Add markdown-aware unfurl policy for Phase 667 embed depth.

Introduces unfurl-policy.js with code/spoiler/md-link suppression, first-line
wins extraction, and layout gates; wires format-message and index exports plus
unit tests for regression coverage.

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
Raven Scott
2026-06-02 20:22:26 -04:00
co-authored by Cursor
parent ac0c056ace
commit 552d72bdec
5 changed files with 258 additions and 27 deletions
+57
View File
@@ -0,0 +1,57 @@
'use strict'
const test = require('brittle')
const {
extractUnfurlUrls,
extractFirstUnfurlUrl,
stripUrlTrailingPunctuation,
urlInsideSpoiler,
urlPartiallySpoiled,
shouldUnfurlMessageLayout
} = require('../unfurl-policy')
test('suppress inline code URL', (t) => {
t.is(extractFirstUnfurlUrl('see `https://example.com/a`'), null)
})
test('suppress fenced code URL', (t) => {
const c = '```\nhttps://example.com/b\n```'
t.is(extractFirstUnfurlUrl(c), null)
})
test('suppress angle bracket URL', (t) => {
t.is(extractFirstUnfurlUrl('link <https://example.com/c>'), null)
})
test('suppress markdown link target', (t) => {
t.is(extractFirstUnfurlUrl('[docs](https://example.com/d)'), null)
})
test('plain URL unfurls', (t) => {
t.is(extractFirstUnfurlUrl('hello https://example.com/e world'), 'https://example.com/e')
})
test('spoiler wraps URL', (t) => {
t.is(extractFirstUnfurlUrl('||https://example.com/f||'), null)
t.ok(urlInsideSpoiler('||https://example.com/f||', 'https://example.com/f'))
})
test('partial spoiler overlap blocks unfurl', (t) => {
const text = '||https://example.com/g||extra'
t.ok(urlPartiallySpoiled(text, 'https://example.com/g', 2, 26))
})
test('first line wins multi-line', (t) => {
const urls = extractUnfurlUrls('line1 https://a.test/x\nline2 https://b.test/y', 3)
t.is(urls.length, 1)
t.is(urls[0], 'https://a.test/x')
})
test('strip trailing punctuation', (t) => {
t.is(stripUrlTrailingPunctuation('https://example.com/h).'), 'https://example.com/h')
})
test('emoji-only layout suppresses unfurl policy', (t) => {
t.is(shouldUnfurlMessageLayout('emoji-only'), false)
t.is(shouldUnfurlMessageLayout('text'), true)
})