better link parsing from GNU social

This commit is contained in:
Nicolas Constant 2018-10-29 23:56:00 -04:00
parent c7e9084307
commit 2b123e386d
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
2 changed files with 36 additions and 18 deletions

View File

@ -75,4 +75,13 @@ describe('DatabindedTextComponent', () => {
expect(component.processedText).toContain('bla3'); expect(component.processedText).toContain('bla3');
expect(component.processedText).toContain('bla4'); expect(component.processedText).toContain('bla4');
}); });
it('should parse link - GNU social', () => {
const sample = `bla1 <a href="https://www.lemonde.fr/planete.html?xtor=RSS-3208" rel="nofollow noopener" class="" target="_blank">https://social.bitcast.info/url/819438</a>`;
component.text = sample;
expect(component.processedText).toContain('<a href class="link-httpswwwlemondefrplanetehtmlxtorRSS3208" title="open link">https://social.bitcast.info/url/819438</a>');
expect(component.processedText).toContain('bla1');
});
}); });

View File

@ -21,28 +21,40 @@ export class DatabindedTextComponent implements OnInit {
@Input('text') @Input('text')
set text(value: string) { set text(value: string) {
this.processedText = ''; this.processedText = '';
let linksSections = value.split('<a '); let linksSections = value.split('<a ');
for (let section of linksSections) { for (let section of linksSections) {
console.log(section);
if (!section.includes('href')) { if (!section.includes('href')) {
this.processedText += section; this.processedText += section;
continue; continue;
} }
if (section.includes('class="mention hashtag"') || section.includes('target="_blank">#')) { if (section.includes('class="mention hashtag"') || section.includes('target="_blank">#')) {
console.log('process hashtag'); try {
this.processHashtag(section); this.processHashtag(section);
}
catch (err) {
console.warn('process hashtag');
console.warn(value);
}
} else if (section.includes('class="u-url mention"') || section.includes('class="mention"')) { } else if (section.includes('class="u-url mention"') || section.includes('class="mention"')) {
console.log('process mention'); try {
this.processUser(section); this.processUser(section);
}
catch (err) {
console.warn('process mention');
console.warn(value);
}
} else { } else {
console.log('process link'); try {
console.log(section);
this.processLink(section); this.processLink(section);
} }
catch (err) {
console.warn('process link');
console.warn(value);
}
}
} }
} }
@ -78,18 +90,15 @@ export class DatabindedTextComponent implements OnInit {
let extractedLinkAndNext = section.split('</a>') let extractedLinkAndNext = section.split('</a>')
let extractedUrl = extractedLinkAndNext[0].split('"')[1]; let extractedUrl = extractedLinkAndNext[0].split('"')[1];
console.warn(extractedLinkAndNext[0]);
console.warn(extractedLinkAndNext[0].split('<span class="ellipsis">'));
let extractedName = ''; let extractedName = '';
try { try {
extractedName = extractedLinkAndNext[0].split('<span class="ellipsis">')[1].split('</span>')[0]; extractedName = extractedLinkAndNext[0].split('<span class="ellipsis">')[1].split('</span>')[0];
} catch (err){ } catch (err) {
try { try {
extractedName = extractedLinkAndNext[0].split('<span class="invisible">https://</span><span class="">')[1].split('</span>')[0]; extractedName = extractedLinkAndNext[0].split('<span class="invisible">https://</span><span class="">')[1].split('</span>')[0];
} }
catch(err){ catch (err) {
extractedName = extractedLinkAndNext[0].split('rel="nofollow noopener" target="_blank">')[1].split('</span>')[0]; extractedName = extractedLinkAndNext[0].split(' target="_blank">')[1].split('</span>')[0];
} }
} }