Skip to content

Commit e57c955

Browse files
[UXP-135][CST-14001] Add Markdown support to link rendering
1 parent 89091ba commit e57c955

2 files changed

Lines changed: 47 additions & 7 deletions

File tree

src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/link/link.component.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,41 @@ describe('LinkComponent', () => {
100100
});
101101
});
102102

103+
describe('with markdown link', () => {
104+
beforeEach(() => {
105+
component.metadataValue = { value: '[Example](http://example.com)' } as MetadataValue;
106+
fixture.detectChanges();
107+
});
108+
109+
it('should create', () => {
110+
expect(component).toBeTruthy();
111+
});
112+
113+
it('check metadata rendering', (done) => {
114+
const spanValueFound = fixture.debugElement.queryAll(By.css('span.link-value'));
115+
expect(spanValueFound.length).toBe(1);
116+
117+
const valueFound = fixture.debugElement.queryAll(By.css('a'));
118+
expect(valueFound.length).toBe(1);
119+
120+
expect(valueFound[0].nativeElement.textContent).toContain('Example');
121+
expect(valueFound[0].nativeElement.href).toBe('http://example.com/');
122+
done();
123+
});
124+
125+
it('check value style', (done) => {
126+
const valueFound = fixture.debugElement.queryAll(By.css('.test-style-value'));
127+
expect(valueFound.length).toBe(1);
128+
done();
129+
});
130+
131+
it('should return markdown link when metadataValue is in markdown format', () => {
132+
const result = component.getLinkFromValue();
133+
expect(result.href).toBe('http://example.com');
134+
expect(result.text).toBe('Example');
135+
});
136+
});
137+
103138
describe('with sub-type label', () => {
104139
beforeEach(() => {
105140
component.renderingSubType = 'LABEL';

src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/link/link.component.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,22 @@ export class LinkComponent extends RenderingTypeValueModelComponent implements O
6060
// If the component has label subtype get the text from translate service
6161
let linkText: string;
6262
let metadataValue: string;
63+
const isMarkdownURLRegex = /^\[(?<label>.*)\]\((?<url>(?:http|ftp)s?:.*)\)$/;
6364

6465
if (hasValue(this.renderingSubType) && this.renderingSubType.toUpperCase() === TYPES.EMAIL) {
65-
this.isEmail = true;
66-
metadataValue = 'mailto:' + this.metadataValue.value;
67-
linkText = (hasValue(this.renderingSubType) &&
66+
this.isEmail = true;
67+
metadataValue = 'mailto:' + this.metadataValue.value;
68+
linkText = (hasValue(this.renderingSubType) &&
6869
this.renderingSubType.toUpperCase() === TYPES.EMAIL) ? this.metadataValue.value : this.translateService.instant(this.field.label);
70+
} else if (isMarkdownURLRegex.test(this.metadataValue.value)) {
71+
const execRegex = isMarkdownURLRegex.exec(this.metadataValue.value).groups;
72+
linkText = execRegex.label;
73+
metadataValue = execRegex.url;
6974
} else {
70-
const startsWithProtocol = [/^https?:\/\//, /^ftp:\/\//];
71-
metadataValue = startsWithProtocol.some(rx => rx.test(this.metadataValue.value)) ? this.metadataValue.value : 'http://' + this.metadataValue.value;
72-
linkText = (hasValue(this.renderingSubType) &&
73-
this.renderingSubType.toUpperCase() === TYPES.LABEL) ? this.translateService.instant(this.field.label) : this.metadataValue.value;
75+
const startsWithProtocol = [/^https?:\/\//, /^ftp:\/\//];
76+
metadataValue = startsWithProtocol.some(rx => rx.test(this.metadataValue.value)) ? this.metadataValue.value : 'http://' + this.metadataValue.value;
77+
linkText = (hasValue(this.renderingSubType) &&
78+
this.renderingSubType?.toUpperCase() === TYPES.LABEL) ? this.translateService.instant(this.field.label) : this.metadataValue.value;
7479
}
7580

7681
return {

0 commit comments

Comments
 (0)