Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto
Something went wrong. Try again.
3.1 kB · 87 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788// What the composer is handed: which URL, and a title cut to fit around it.
import { describe, expect, it } from 'vitest'import { POST_LIMIT, composeUrl, graphemes, shareText, shareUrl, truncate } from './share'import type { DocInfo, PubInfo } from './types'
const pub: PubInfo = { uri: 'at://did:plc:sharetest000000000000000/site.standard.publication/self', did: 'did:plc:sharetest000000000000000', handle: 'pub.example', pds: 'https://pds.example.com', record: { $type: 'site.standard.publication', url: 'https://pub.example/', name: 'Wobbegong' }, verified: true,}
const doc: DocInfo = { uri: 'at://did:plc:sharetest000000000000000/site.standard.document/3kdoc', record: { $type: 'site.standard.document', site: pub.uri, title: 'On quarrying granite', publishedAt: '2026-08-12T00:00:00.000Z', path: '/posts/granite', },}
describe('shareUrl', () => { it('shares the page the reader is actually on', () => { expect(shareUrl(pub, doc, 'https://pub.example/posts/granite?utm=x')).toBe( 'https://pub.example/posts/granite?utm=x', ) })
it('rebuilds the article url from the record when there is no page url', () => { expect(shareUrl(pub, doc)).toBe('https://pub.example/posts/granite') })
it('falls back to the publication when the page is not an article', () => { expect(shareUrl(pub)).toBe('https://pub.example/') })
it('refuses a page url that is not http(s)', () => { expect(shareUrl(pub, undefined, 'chrome://extensions')).toBe('https://pub.example/') })})
describe('shareText', () => { it('leads with the article title and follows with the link', () => { expect(shareText(pub, doc, 'https://pub.example/posts/granite')).toBe( 'On quarrying granite\n\nhttps://pub.example/posts/granite', ) })
it('uses the publication name when the page is not an article', () => { expect(shareText(pub, undefined, 'https://pub.example/')).toBe( 'Wobbegong\n\nhttps://pub.example/', ) })
it('cuts the title, never the link, and stays inside Bluesky’s limit', () => { const long = { ...doc, record: { ...doc.record, title: 'granite '.repeat(60) } } const text = shareText(pub, long, 'https://pub.example/posts/granite') expect(graphemes(text!).length).toBeLessThanOrEqual(POST_LIMIT) expect(text).toContain('https://pub.example/posts/granite') expect(text).toContain('…') })
it('drops the title entirely when the link alone fills the post', () => { const url = `https://pub.example/${'x'.repeat(295)}` expect(shareText(pub, doc, url)).toBe(url) })})
describe('truncate', () => { it('counts what a person would count, not code units', () => { // Four code units, one grapheme. expect(truncate('👩🌾abc', 2)).toBe('👩🌾…') })})
describe('composeUrl', () => { it('percent-encodes the text into Bluesky’s documented intent link', () => { const u = new URL(composeUrl('a b\n\nhttps://pub.example/')) expect(u.origin + u.pathname).toBe('https://bsky.app/intent/compose') expect(u.searchParams.get('text')).toBe('a b\n\nhttps://pub.example/') })})