Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@atcute/bluesky-richtext-parser

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@atcute/bluesky-richtext-parser

parse Bluesky's (extended) rich text syntax

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
40
increased by48.15%
Maintainers
1
Weekly downloads
 
Created
Source

@atcute/bluesky-richtext-parser

parse Bluesky's rich text syntax, with added support for emotes, escapes, and Markdown-like links.

const result = tokenize(`hello @bsky.app! check out my [website](https://example.com)`);

expect(result).toEqual([
	{
		type: 'text',
		raw: 'hello ',
		text: 'hello ',
	},
	{
		type: 'mention',
		raw: '@bsky.app',
		handle: 'bsky.app',
	},
	{
		type: 'text',
		raw: '! check out my ',
		text: '! check out my ',
	},
	{
		type: 'link',
		raw: '[website](https://example.com)',
		text: 'website',
		url: 'https://example.com',
	},
]);

whitespace trimming can be done by using the following regular expression before passing to the tokenizer, and afterwards for text on Markdown-like links:

/^\s+|\s+$| +(?=\n)|\n(?=(?: *\n){2}) */g;

autolink trimming can be done like so:

const safeUrlParse = (href: string): URL | null => {
	const url = URL.parse(text);

	if (url !== null) {
		const protocol = url.protocol;

		if (protocol === 'https:' || protocol === 'http:') {
			return url;
		}
	}

	return null;
};

const TRIM_HOST_RE = /^www\./;
const PATH_MAX_LENGTH = 16;

const toShortUrl = (href: string): string => {
	const url = safeUrlParse(href);

	if (url !== null) {
		const host =
			(url.username ? url.username + (url.password ? ':' + url.password : '') + '@' : '') +
			url.host.replace(TRIM_HOST_RE, '');

		const path =
			(url.pathname === '/' ? '' : url.pathname) +
			(url.search.length > 1 ? url.search : '') +
			(url.hash.length > 1 ? url.hash : '');

		if (path.length > PATH_MAX_LENGTH) {
			return host + path.slice(0, PATH_MAX_LENGTH - 1) + '…';
		}

		return host + path;
	}

	return href;
};

FAQs

Package last updated on 06 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc