mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-26 03:47:40 -07:00
Better hashtag parsing
This commit is contained in:
parent
4eb539c4e0
commit
20c4d59c85
2 changed files with 13 additions and 4 deletions
|
@ -9,9 +9,9 @@ export type TextElementHashtag = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function(text: string, i: number) {
|
export default function(text: string, i: number) {
|
||||||
if (!(/^\s#[^\s]+/.test(text) || (i == 0 && /^#[^\s]+/.test(text)))) return null;
|
if (!(/^\s#[^\s\.,]+/.test(text) || (i == 0 && /^#[^\s\.,]+/.test(text)))) return null;
|
||||||
const isHead = text.startsWith('#');
|
const isHead = text.startsWith('#');
|
||||||
const hashtag = text.match(/^\s?#[^\s]+/)[0];
|
const hashtag = text.match(/^\s?#[^\s\.,]+/)[0];
|
||||||
const res: any[] = !isHead ? [{
|
const res: any[] = !isHead ? [{
|
||||||
type: 'text',
|
type: 'text',
|
||||||
content: text[0]
|
content: text[0]
|
||||||
|
|
13
test/mfm.ts
13
test/mfm.ts
|
@ -71,11 +71,20 @@ describe('Text', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('hashtag', () => {
|
it('hashtag', () => {
|
||||||
const tokens = analyze('Strawberry Pasta #alice');
|
const tokens1 = analyze('Strawberry Pasta #alice');
|
||||||
assert.deepEqual([
|
assert.deepEqual([
|
||||||
{ type: 'text', content: 'Strawberry Pasta ' },
|
{ type: 'text', content: 'Strawberry Pasta ' },
|
||||||
{ type: 'hashtag', content: '#alice', hashtag: 'alice' }
|
{ type: 'hashtag', content: '#alice', hashtag: 'alice' }
|
||||||
], tokens);
|
], tokens1);
|
||||||
|
|
||||||
|
const tokens2 = analyze('Foo #bar, baz #piyo.');
|
||||||
|
assert.deepEqual([
|
||||||
|
{ type: 'text', content: 'Foo ' },
|
||||||
|
{ type: 'hashtag', content: '#bar', hashtag: 'bar' },
|
||||||
|
{ type: 'text', content: ', baz ' },
|
||||||
|
{ type: 'hashtag', content: '#piyo', hashtag: 'piyo' },
|
||||||
|
{ type: 'text', content: '.' }
|
||||||
|
], tokens2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('url', () => {
|
it('url', () => {
|
||||||
|
|
Loading…
Reference in a new issue