From 2b123e386d33b05a42a6743e3a4476aad12ad006 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Mon, 29 Oct 2018 23:56:00 -0400 Subject: [PATCH] better link parsing from GNU social --- .../databinded-text.component.spec.ts | 9 ++++ .../databinded-text.component.ts | 45 +++++++++++-------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/app/components/stream/status/databinded-text/databinded-text.component.spec.ts b/src/app/components/stream/status/databinded-text/databinded-text.component.spec.ts index bc0c0122..e8444801 100644 --- a/src/app/components/stream/status/databinded-text/databinded-text.component.spec.ts +++ b/src/app/components/stream/status/databinded-text/databinded-text.component.spec.ts @@ -75,4 +75,13 @@ describe('DatabindedTextComponent', () => { expect(component.processedText).toContain('bla3'); expect(component.processedText).toContain('bla4'); }); + + it('should parse link - GNU social', () => { + const sample = `bla1 https://social.bitcast.info/url/819438`; + + component.text = sample; + expect(component.processedText).toContain('https://social.bitcast.info/url/819438'); + expect(component.processedText).toContain('bla1'); + }); + }); diff --git a/src/app/components/stream/status/databinded-text/databinded-text.component.ts b/src/app/components/stream/status/databinded-text/databinded-text.component.ts index e8a2e23d..7cba1234 100644 --- a/src/app/components/stream/status/databinded-text/databinded-text.component.ts +++ b/src/app/components/stream/status/databinded-text/databinded-text.component.ts @@ -21,27 +21,39 @@ export class DatabindedTextComponent implements OnInit { @Input('text') set text(value: string) { this.processedText = ''; - let linksSections = value.split('#')) { - console.log('process hashtag'); - this.processHashtag(section); + try { + this.processHashtag(section); + } + catch (err) { + console.warn('process hashtag'); + console.warn(value); + } + } else if (section.includes('class="u-url mention"') || section.includes('class="mention"')) { - console.log('process mention'); - this.processUser(section); + try { + this.processUser(section); + } + catch (err) { + console.warn('process mention'); + console.warn(value); + } } else { - console.log('process link'); - console.log(section); - this.processLink(section); + try { + this.processLink(section); + } + catch (err) { + console.warn('process link'); + console.warn(value); + } } } } @@ -78,20 +90,17 @@ export class DatabindedTextComponent implements OnInit { let extractedLinkAndNext = section.split('') let extractedUrl = extractedLinkAndNext[0].split('"')[1]; - console.warn(extractedLinkAndNext[0]); - console.warn(extractedLinkAndNext[0].split('')); - let extractedName = ''; try { extractedName = extractedLinkAndNext[0].split('')[1].split('')[0]; - } catch (err){ + } catch (err) { try { - extractedName = extractedLinkAndNext[0].split('')[1].split('')[0]; + extractedName = extractedLinkAndNext[0].split('')[1].split('')[0]; } - catch(err){ - extractedName = extractedLinkAndNext[0].split('rel="nofollow noopener" target="_blank">')[1].split('')[0]; + catch (err) { + extractedName = extractedLinkAndNext[0].split(' target="_blank">')[1].split('')[0]; } - } + } this.links.push(extractedUrl); let classname = this.getClassNameForLink(extractedUrl);