Skip to content

Commit

Permalink
Merge pull request #1616 from SnowCait/fix-parsing-hashtags
Browse files Browse the repository at this point in the history
Fix parsing hashtags
  • Loading branch information
SnowCait authored Feb 6, 2025
2 parents 0cf6d33 + 6f397ef commit 7bb0a3f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 8 additions & 0 deletions web/src/lib/Content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ describe('parse test', () => {
expect(Content.parse('#nostter', [['t', 'nostter']])).toStrictEqual([
new Token('hashtag', '#nostter')
]);
expect(Content.parse('#nostter', [['t', '']])).toStrictEqual([
new Token('text', '#nostter')
]);
});
it('emoji', () => {
expect(
Expand Down Expand Up @@ -88,6 +91,11 @@ describe('parse test', () => {
new Token('url', 'https://example.com/')
]);
});
it('url in JSON', () => {
expect(Content.parse('{"key":"https://example.com/"}')).toStrictEqual([
new Token('text', '{"key":"https://example.com/"}')
]);
});
// it('url', () => {
// expect(Content.parse('(https://example.com/path)')).toStrictEqual([
// new Token('text', '('),
Expand Down
8 changes: 6 additions & 2 deletions web/src/lib/Content.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { nip19 } from 'nostr-tools';
import type { EventPointer, ProfilePointer } from 'nostr-tools/lib/nip19';
import { unique } from './Array';
import escapeStringRegexp from 'escape-string-regexp';

export class Token {
constructor(
Expand All @@ -14,7 +15,7 @@ export class Token {
export class Content {
static parse(content: string, tags: string[][] = []): Token[] {
const hashtags = tags
.filter(([tagName, tagContent]) => tagName === 't' && tagContent !== undefined)
.filter(([tagName, tagContent]) => tagName === 't' && tagContent)
.map(([, tagContent]) => tagContent);
hashtags.sort((x, y) => y.length - x.length);
const emojis = new Map(
Expand All @@ -30,7 +31,10 @@ export class Content {
matches = [
...(hashtags.length > 0
? content.matchAll(
new RegExp(`(${hashtags.map((x) => `#${x}`).join('|')})`, 'gi')
new RegExp(
`(${hashtags.map((x) => `#${escapeStringRegexp(x)}`).join('|')})`,
'gi'
)
)
: []),
...(emojis.size > 0
Expand Down

0 comments on commit 7bb0a3f

Please sign in to comment.