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

Add new feature: URL clickable #25

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ To collapse all nodes at first:
```html
<ngx-json-viewer [json]="someObject" [expanded]="false"></ngx-json-viewer>
```

Makes URLs clickable
```html
<ngx-json-viewer [json]="someObject" [clickableLinks]="true"></ngx-json-viewer>
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-json-viewer",
"version": "2.4.0",
"version": "2.4.1",
"description": "JSON formatter / viewer for Angular 2/4/5/6/7+",
"keywords": [
"angular",
Expand Down
4 changes: 2 additions & 2 deletions src/ngx-json-viewer/ngx-json-viewer.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
<div *ngIf="isExpandable(segment)" class="toggler"></div>
<span class="segment-key">{{ segment.key }}</span>
<span class="segment-separator">: </span>
<span *ngIf="!segment.expanded || !isExpandable(segment)" class="segment-value">{{ segment.description }}</span>
<span *ngIf="!segment.expanded || !isExpandable(segment)" class="segment-value" [innerHTML]="segment.description"></span>
</section>
<section *ngIf="segment.expanded && isExpandable(segment)" class="children">
<ngx-json-viewer [json]="segment.value" [expanded]="expanded"></ngx-json-viewer>
<ngx-json-viewer [json]="segment.value" [expanded]="expanded" [clickableLinks]="clickableLinks"></ngx-json-viewer>
</section>
</section>
</section>
15 changes: 12 additions & 3 deletions src/ngx-json-viewer/ngx-json-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Segment {
type: undefined | string;
description: string;
expanded: boolean;
clickableLinks: boolean;
}

@Component({
Expand All @@ -17,6 +18,7 @@ export class NgxJsonViewerComponent implements OnChanges {

@Input() json: any;
@Input() expanded = true;
@Input() clickableLinks = false;
/**
* @deprecated It will be always true and deleted in version 3.0.0
*/
Expand Down Expand Up @@ -54,8 +56,10 @@ export class NgxJsonViewerComponent implements OnChanges {
value: value,
type: undefined,
description: '' + value,
expanded: this.expanded
expanded: this.expanded,
clickableLinks: this.clickableLinks
};
const htmlRegexp: any = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/;

switch (typeof segment.value) {
case 'number': {
Expand All @@ -71,8 +75,13 @@ export class NgxJsonViewerComponent implements OnChanges {
break;
}
case 'string': {
segment.type = 'string';
segment.description = '"' + segment.value + '"';
if (segment.clickableLinks && htmlRegexp.test(segment.value)) {
segment.type = 'url';
segment.description = '<a href="' + segment.value + '">' + segment.value + '</a>';
} else {
segment.type = 'string';
segment.description = '"' + segment.value + '"';
}
break;
}
case 'undefined': {
Expand Down