fix hashtag parsing
This commit is contained in:
parent
6530ad7bde
commit
801658bfbc
|
@ -36,7 +36,7 @@ describe('DatabindedTextComponent', () => {
|
||||||
const url = 'https://test.social/tags/programmers';
|
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>`;
|
const sample = `<p>bla1 <a href="${url}" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>${hashtag}</span></a> bla2</p>`;
|
||||||
component.text = sample;
|
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('bla1');
|
||||||
expect(component.processedText).toContain('bla2');
|
expect(component.processedText).toContain('bla2');
|
||||||
});
|
});
|
||||||
|
@ -75,7 +75,7 @@ describe('DatabindedTextComponent', () => {
|
||||||
const linkUrl = 'mydomain.co/test';
|
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>`;
|
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;
|
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="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('<a href class="link-httpsmydomaincotest" title="open link">mydomain.co/test</a>');
|
||||||
expect(component.processedText).toContain('bla1');
|
expect(component.processedText).toContain('bla1');
|
||||||
|
@ -98,5 +98,4 @@ describe('DatabindedTextComponent', () => {
|
||||||
component.text = sample;
|
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>');
|
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>');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -64,7 +64,8 @@ export class DatabindedTextComponent implements OnInit {
|
||||||
let extractedLinkAndNext = section.split('</a>');
|
let extractedLinkAndNext = section.split('</a>');
|
||||||
let extractedHashtag = extractedLinkAndNext[0].split('#')[1].replace('<span>', '').replace('</span>', '');
|
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];
|
if (extractedLinkAndNext[1]) this.processedText += extractedLinkAndNext[1];
|
||||||
this.hashtags.push(extractedHashtag);
|
this.hashtags.push(extractedHashtag);
|
||||||
}
|
}
|
||||||
|
@ -124,7 +125,8 @@ export class DatabindedTextComponent implements OnInit {
|
||||||
|
|
||||||
ngAfterViewInit() {
|
ngAfterViewInit() {
|
||||||
for (const hashtag of this.hashtags) {
|
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) => {
|
this.renderer.listen(el, 'click', (event) => {
|
||||||
event.preventDefault();
|
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 {
|
private getClassNameForAccount(value: string): string {
|
||||||
let res = value;
|
let res = value;
|
||||||
while (res.includes('.')) res = res.replace('.', '-');
|
while (res.includes('.')) res = res.replace('.', '-');
|
||||||
|
|
Loading…
Reference in New Issue