Skip to content

Commit

Permalink
refactor(bluesky-richtext-parser): follow discord's simpler url backp…
Browse files Browse the repository at this point in the history
…adal

less chaotic than whatever it was earlier
  • Loading branch information
mary-ext committed Dec 9, 2024
1 parent 0c7a477 commit e8e3a22
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
16 changes: 8 additions & 8 deletions packages/bluesky/richtext-parser/lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,26 +462,26 @@ it('autolinks', () => {
expect(tokenize('https://example.com/.)')).toEqual([
{
type: 'autolink',
raw: 'https://example.com/',
url: 'https://example.com/',
raw: 'https://example.com/.',
url: 'https://example.com/.',
},
{
type: 'text',
raw: '.)',
text: '.)',
raw: ')',
text: ')',
},
]);

expect(tokenize('https://example.com/.))')).toEqual([
{
type: 'autolink',
raw: 'https://example.com/',
url: 'https://example.com/',
raw: 'https://example.com/.)',
url: 'https://example.com/.)',
},
{
type: 'text',
raw: '.))',
text: '.))',
raw: ')',
text: ')',
},
]);

Expand Down
12 changes: 3 additions & 9 deletions packages/bluesky/richtext-parser/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const TOPIC_RE =

const EMOTE_RE = /^:([\w-]+):/;

const AUTOLINK_RE = /^https?:\/\/(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*/;
const AUTOLINK_BACKPEDAL_RE = /(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/;
const AUTOLINK_RE = /^https?:\/\/[\S]+/;
const AUTOLINK_BACKPEDAL_RE = /(?:(?<!\(.*)\))?[.,;]*$/;

const LINK_RE = /^\[((?:\[(?:\\.|[^\[\]\\])*\]|\\.|[^\[\]\\])*?)\]\((.*?)\)/;

Expand Down Expand Up @@ -106,13 +106,7 @@ const tokenizeEmote = (src: string): EmoteToken | undefined => {
const tokenizeAutolink = (src: string): AutolinkToken | undefined => {
const match = AUTOLINK_RE.exec(src);
if (match) {
let url = match[0];
let prevUrl: string;

do {
prevUrl = url;
url = AUTOLINK_BACKPEDAL_RE.exec(url)?.[0] ?? '';
} while (prevUrl !== url);
const url = match[0].replace(AUTOLINK_BACKPEDAL_RE, '');

return {
type: 'autolink',
Expand Down

0 comments on commit e8e3a22

Please sign in to comment.