Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Datahub] Make abstract links as hyperlinks #588

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions libs/ui/elements/src/lib/metadata-info/linkify.directive.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
/* eslint-disable @angular-eslint/directive-selector */
import { Directive, ElementRef, Renderer2, OnInit } from '@angular/core'
import { Directive, ElementRef, Renderer2, AfterViewInit } from '@angular/core'

@Directive({
selector: '[gnUiLinkify]',
})
export class GnUiLinkifyDirective implements OnInit {
export class GnUiLinkifyDirective implements AfterViewInit {
constructor(private el: ElementRef, private renderer: Renderer2) {}

ngOnInit() {
setTimeout(() => {
this.processLinks()
}, 0)
ngAfterViewInit() {
this.processLinks()
}

private processLinks() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<gn-ui-content-ghost ghostClass="h-32" [showContent]="fieldReady('abstract')">
<p
class="whitespace-pre-line break-words"
[innerHTML]="metadata.abstract | safe: 'html'"
[innerHTML]="getAbstract() | safe: 'html'"
></p>
</gn-ui-content-ghost>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ describe('MetadataInfoComponent', () => {
fixture.detectChanges()
})
it('should not display a message for no usage or constraints', () => {
// Use waitForAsync to handle asynchronous changes in the DOM.
fixture.whenStable().then(() => {
const displayedElement =
fixture.nativeElement.getElementsByClassName('noUsage')
Expand All @@ -75,12 +74,80 @@ describe('MetadataInfoComponent', () => {
})

it('should display the keywords section', () => {
// Use waitForAsync to handle asynchronous changes in the DOM.
fixture.whenStable().then(() => {
const displayedElement =
fixture.nativeElement.querySelector('ng-container')
expect(displayedElement).toBeTruthy()
})
})
})
describe('When a section is not empty', () => {
beforeEach(() => {
component.metadata = {
id: '',
uuid: '',
title: '',
metadataUrl: '',
keywords: ['banana', 'pear'],
constraints: ['no usage'],
}
fixture.detectChanges()
})
it('should not display a message for no usage or constraints', () => {
fixture.whenStable().then(() => {
const displayedElement =
fixture.nativeElement.getElementsByClassName('noUsage')
expect(displayedElement).toBeFalsy()
})
})

it('should display the keywords section', () => {
fixture.whenStable().then(() => {
const displayedElement =
fixture.nativeElement.querySelector('ng-container')
expect(displayedElement).toBeTruthy()
})
})
})
describe('#abstract', () => {
describe('When abstract contains a link', () => {
beforeEach(() => {
component.metadata = {
id: '',
uuid: '',
title: '',
metadataUrl: '',
keywords: ['banana', 'pear'],
constraints: ['no usage'],
abstract: 'This is a text with a link http://www.google.com',
}
fixture.detectChanges()
})
it('should create a link in the abstract', () => {
const paragraph = component.getAbstract()
expect(paragraph).toContain('<a href="http://www.google.com"')
expect(paragraph).toContain(
'<mat-icon class="mat-icon !w-[12px] !h-[14px] !text-[14px] opacity-75 material-icons">open_in_new</mat-icon>'
)
})
})
describe('When abstract does not contain a link', () => {
beforeEach(() => {
component.metadata = {
id: '',
uuid: '',
title: '',
metadataUrl: '',
keywords: ['banana', 'pear'],
constraints: ['no usage'],
abstract: 'This is a text without a link',
}
fixture.detectChanges()
})
it('should not create a hyperlink or icon', () => {
const paragraph = component.getAbstract()
expect(paragraph).toEqual('This is a text without a link')
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ export class MetadataInfoComponent {
onKeywordClick(keyword: string) {
this.keyword.emit(keyword)
}

getAbstract() {
return this.metadata.abstract?.replace(/(\bhttps?:\/\/\S+\b)/g, (match) => {
return `<a href="${match}" target="_blank"
class="text-primary cursor-pointer hover:underline">${match} <mat-icon class="mat-icon !w-[12px] !h-[14px] !text-[14px] opacity-75 material-icons">open_in_new</mat-icon></a>`
})
}
}