mirror of https://github.com/Squidex/squidex.git
12 changed files with 125 additions and 40 deletions
@ -0,0 +1,30 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { MarkdownPipe } from './markdown.pipe'; |
|||
|
|||
describe('MarkdownPipe', () => { |
|||
it('should convert link to html', () => { |
|||
const actual = new MarkdownPipe().transform('[link-name](link-url)'); |
|||
|
|||
expect(actual).toBe('<p><a href="link-url" target="_blank", rel="noopener">link-name <i class="icon-external-link"></i></a></p>\n'); |
|||
}); |
|||
|
|||
it('should convert markdown to html', () => { |
|||
const actual = new MarkdownPipe().transform('*bold*'); |
|||
|
|||
expect(actual).toBe('<p><em>bold</em></p>\n'); |
|||
}); |
|||
|
|||
[null, undefined, ''].map(x => { |
|||
it('should return empty string for invalid value', () => { |
|||
const actual = new MarkdownPipe().transform(x); |
|||
|
|||
expect(actual).toBe(''); |
|||
}); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,29 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { Pipe, PipeTransform } from '@angular/core'; |
|||
import Marked from 'marked'; |
|||
|
|||
const renderer = new Marked.Renderer(); |
|||
|
|||
renderer.link = (href, _, text) => { |
|||
return `<a href="${href}" target="_blank", rel="noopener">${text} <i class="icon-external-link"></i></a>`; |
|||
}; |
|||
|
|||
@Pipe({ |
|||
name: 'sqxMarkdown', |
|||
pure: true |
|||
}) |
|||
export class MarkdownPipe implements PipeTransform { |
|||
public transform(text: string | null | undefined): string { |
|||
if (text) { |
|||
return Marked(text, { renderer }); |
|||
} else { |
|||
return ''; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { HelpMarkdownPipe } from './help-markdown.pipe'; |
|||
|
|||
describe('MarkdownPipe', () => { |
|||
it('should convert absolute link to html', () => { |
|||
const actual = new HelpMarkdownPipe().transform('[link-name](https://squidex.io)'); |
|||
|
|||
expect(actual).toBe('<p><a href="https://squidex.io" target="_blank", rel="noopener">link-name <i class="icon-external-link"></i></a></p>\n'); |
|||
}); |
|||
|
|||
it('should convert relative link to html', () => { |
|||
const actual = new HelpMarkdownPipe().transform('[link-name](link-url)'); |
|||
|
|||
expect(actual).toBe('<p><a href="https://docs.squidex.io/link-url" target="_blank", rel="noopener">link-name <i class="icon-external-link"></i></a></p>\n'); |
|||
}); |
|||
|
|||
it('should convert markdown to html', () => { |
|||
const actual = new HelpMarkdownPipe().transform('*bold*'); |
|||
|
|||
expect(actual).toBe('<p><em>bold</em></p>\n'); |
|||
}); |
|||
|
|||
[null, undefined, ''].map(x => { |
|||
it('should return empty string for invalid value', () => { |
|||
const actual = new HelpMarkdownPipe().transform(x); |
|||
|
|||
expect(actual).toBe(''); |
|||
}); |
|||
}); |
|||
}); |
|||
Loading…
Reference in new issue