fix hashtag parsing

This commit is contained in:
Nicolas Constant 2018-11-01 18:38:59 -04:00
parent 6530ad7bde
commit 801658bfbc
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
2 changed files with 12 additions and 6 deletions

View File

@ -36,7 +36,7 @@ describe('DatabindedTextComponent', () => {
const url = 'https://test.social/tags/programmers';
const sample = `<p>bla1 <a href="${url}" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>${hashtag}</span></a> bla2</p>`;
component.text = sample;
expect(component.processedText).toContain('<a href class="programmers">#programmers</a>');
expect(component.processedText).toContain('<a href class="hashtag-programmers">#programmers</a>');
expect(component.processedText).toContain('bla1');
expect(component.processedText).toContain('bla2');
});
@ -75,7 +75,7 @@ describe('DatabindedTextComponent', () => {
const linkUrl = 'mydomain.co/test';
const sample = `<p>bla1 <a href="${hashtagUrl}" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>${hashtag}</span></a> bla2 <span class="h-card"><a href="${mentionUrl}" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>${mention}</span></a></span> bla3 <a href="https://${linkUrl}" rel="nofollow noopener" target="_blank"><span class="invisible">https://</span><span class="">${linkUrl}</span><span class="invisible"></span></a> bla4</p>`;
component.text = sample;
expect(component.processedText).toContain('<a href class="programmers">#programmers</a>');
expect(component.processedText).toContain('<a href class="hashtag-programmers">#programmers</a>');
expect(component.processedText).toContain('<a href class="account--sengi_app-mastodon-social" title="@sengi_app@mastodon.social">@sengi_app</a>');
expect(component.processedText).toContain('<a href class="link-httpsmydomaincotest" title="open link">mydomain.co/test</a>');
expect(component.processedText).toContain('bla1');
@ -97,6 +97,5 @@ describe('DatabindedTextComponent', () => {
component.text = sample;
expect(component.processedText).toContain('<div><span> <a href class="account--kaniini-pleroma-site" title="@kaniini@pleroma.site">@kaniini</a> <span> <a href class="account--Gargron-mastodon-social" title="@Gargron@mastodon.social">@Gargron</a> bla1?</div>');
});
});
});

View File

@ -64,7 +64,8 @@ export class DatabindedTextComponent implements OnInit {
let extractedLinkAndNext = section.split('</a>');
let extractedHashtag = extractedLinkAndNext[0].split('#')[1].replace('<span>', '').replace('</span>', '');
this.processedText += ` <a href class="${extractedHashtag}">#${extractedHashtag}</a>`;
let classname = this.getClassNameForHastag(extractedHashtag);
this.processedText += ` <a href class="${classname}">#${extractedHashtag}</a>`;
if (extractedLinkAndNext[1]) this.processedText += extractedLinkAndNext[1];
this.hashtags.push(extractedHashtag);
}
@ -124,7 +125,8 @@ export class DatabindedTextComponent implements OnInit {
ngAfterViewInit() {
for (const hashtag of this.hashtags) {
let el = this.contentElement.nativeElement.querySelector(`.${hashtag}`);
let classname = this.getClassNameForHastag(hashtag);
let el = this.contentElement.nativeElement.querySelector(`.${classname}`);
this.renderer.listen(el, 'click', (event) => {
event.preventDefault();
@ -161,6 +163,11 @@ export class DatabindedTextComponent implements OnInit {
}
}
private getClassNameForHastag(value: string): string {
let res = value.replace(/[.,\/#?!@$%+\^&\*;:{}=\-_`~()]/g, "");
return `hashtag-${res}`;
}
private getClassNameForAccount(value: string): string {
let res = value;
while (res.includes('.')) res = res.replace('.', '-');