Skip to content

Commit 29c5b41

Browse files
authored
Fix plugins using Data input property (#136)
1 parent 7ecb4ea commit 29c5b41

File tree

7 files changed

+66
-54
lines changed

7 files changed

+66
-54
lines changed

demo/src/app/app.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ <h2 class="subtitle">Pipe Usage</h2>
193193
<br />
194194

195195
<markdown>
196-
In the example below, `pipeMarkdown` property does not contain any `back-ticks` to set the content language but will be chain with `language` pipe instead to specify synthax highlights language along with `markdown` pipe for conversion
196+
In the following example using the synthax above, `typescriptMarkdown` property does not contain any `back-ticks` to set the content language but will be chain with `language` pipe instead to specify synthax highlights language along with `markdown` pipe for conversion
197197
</markdown>
198198

199199
<textarea class="pipe-textarea" [(ngModel)]="typescriptMarkdown"></textarea>

demo/src/app/app.component.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,6 @@ public markdown = "# Markdown";
4848
//#endregion
4949

5050
//#region pipe
51-
pipeMarkdown =
52-
`### Markdown example
53-
---
54-
This is an **example** where we use a variable with the \`markdown\` pipe that is also bind to a textarea. Using the pipe allows to chain pipe transformation.
55-
56-
#### example.component.ts
57-
\`\`\`typescript
58-
public pipeMarkdown = "# Markdown";
59-
\`\`\`
60-
61-
#### example.component.html
62-
\`\`\`html
63-
<textarea [(ngModel)]="pipeMarkdown"></textarea>
64-
<div [innerHTML]="pipeMarkdown | markdown"></div>
65-
\`\`\``;
6651
typescriptMarkdown =
6752
`import { Component } from '@angular/core';
6853
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div [innerHTML]="markdown | language : 'typescript' | markdown"></div>
1+
<div [innerHTML]="typescriptMarkdown | language : 'typescript' | markdown"></div>

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-markdown",
3-
"version": "7.1.3",
3+
"version": "7.1.4-beta.0",
44
"description": "Angular library that uses marked to parse markdown to html combined with Prism.js for synthax highlights",
55
"homepage": "https://github.com/jfcere/ngx-markdown",
66
"license": "MIT",

lib/src/markdown.component.spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HttpClientModule } from '@angular/common/http';
22
import { ElementRef } from '@angular/core';
33
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
4-
import { Observable, of, throwError } from 'rxjs';
4+
import { of, throwError } from 'rxjs';
55

66
import { MarkdownComponent } from './markdown.component';
77
import { MarkdownService } from './markdown.service';
@@ -40,6 +40,8 @@ describe('MarkdownComponent', () => {
4040

4141
component.data = mockData;
4242

43+
component.ngOnChanges();
44+
4345
expect(component.render).toHaveBeenCalledWith(mockData);
4446
});
4547

@@ -65,6 +67,8 @@ describe('MarkdownComponent', () => {
6567

6668
component.src = mockSrc;
6769

70+
component.ngOnChanges();
71+
6872
expect(markdownService.getSource).toHaveBeenCalledWith(mockSrc);
6973
expect(component.render).toHaveBeenCalledWith(mockContent);
7074
}));
@@ -80,6 +84,21 @@ describe('MarkdownComponent', () => {
8084
expect(component.src).toBe(mockSrc);
8185
});
8286

87+
it('should emit load when get', () => {
88+
89+
const mockSrc = './src-example/file.md';
90+
const mockSrcReturn = 'src-return-value';
91+
92+
spyOn(markdownService, 'getSource').and.returnValue(of(mockSrcReturn));
93+
spyOn(component.load, 'emit');
94+
95+
component.src = mockSrc;
96+
97+
component.ngOnChanges();
98+
99+
expect(component.load.emit).toHaveBeenCalledWith(mockSrcReturn);
100+
});
101+
83102
it('should emit error when and error occurs', () => {
84103

85104
const mockSrc = './src-example/file.md';
@@ -90,6 +109,8 @@ describe('MarkdownComponent', () => {
90109

91110
component.src = mockSrc;
92111

112+
component.ngOnChanges();
113+
93114
expect(component.error.emit).toHaveBeenCalledWith(mockError);
94115
});
95116
});

lib/src/markdown.component.ts

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AfterViewInit, Component, ElementRef, EventEmitter, Input, Output } from '@angular/core';
1+
import { AfterViewInit, Component, ElementRef, EventEmitter, Input, OnChanges, Output } from '@angular/core';
22

33
import { MarkdownService } from './markdown.service';
44
import { PrismPlugin } from './prism-plugin';
@@ -8,37 +8,9 @@ import { PrismPlugin } from './prism-plugin';
88
selector: 'markdown, [markdown]',
99
template: '<ng-content></ng-content>',
1010
})
11-
export class MarkdownComponent implements AfterViewInit {
12-
private _data: string;
13-
private _lineHighlight = false;
14-
private _lineNumbers = false;
15-
private _src: string;
16-
17-
private get _isTranscluded() {
18-
return !this._data && !this._src;
19-
}
20-
21-
@Input()
22-
get data(): string { return this._data; }
23-
set data(value: string) {
24-
this._data = value;
25-
this.render(value);
26-
}
27-
28-
@Input()
29-
get src(): string { return this._src; }
30-
set src(value: string) {
31-
this._src = value;
32-
this.markdownService
33-
.getSource(value)
34-
.subscribe(
35-
markdown => {
36-
this.render(markdown);
37-
this.load.emit(markdown);
38-
},
39-
error => this.error.emit(error),
40-
);
41-
}
11+
export class MarkdownComponent implements OnChanges, AfterViewInit {
12+
@Input() data: string;
13+
@Input() src: string;
4214

4315
// Plugin - lineNumbers
4416
@Input()
@@ -56,14 +28,28 @@ export class MarkdownComponent implements AfterViewInit {
5628
@Output() error = new EventEmitter<string>();
5729
@Output() load = new EventEmitter<string>();
5830

31+
private _lineHighlight = false;
32+
private _lineNumbers = false;
33+
5934
constructor(
6035
public element: ElementRef<HTMLElement>,
6136
public markdownService: MarkdownService,
6237
) { }
6338

39+
ngOnChanges() {
40+
if (this.data) {
41+
this.handleData();
42+
return;
43+
}
44+
if (this.src) {
45+
this.handleSrc();
46+
return;
47+
}
48+
}
49+
6450
ngAfterViewInit() {
65-
if (this._isTranscluded) {
66-
this.render(this.element.nativeElement.innerHTML, true);
51+
if (!this.data && !this.src) {
52+
this.handleTransclusion();
6753
}
6854
}
6955

@@ -77,6 +63,26 @@ export class MarkdownComponent implements AfterViewInit {
7763
return value != null && `${value}` !== 'false';
7864
}
7965

66+
private handleData() {
67+
this.render(this.data);
68+
}
69+
70+
private handleSrc() {
71+
this.markdownService
72+
.getSource(this.src)
73+
.subscribe(
74+
markdown => {
75+
this.render(markdown);
76+
this.load.emit(markdown);
77+
},
78+
error => this.error.emit(error),
79+
);
80+
}
81+
82+
private handleTransclusion() {
83+
this.render(this.element.nativeElement.innerHTML, true);
84+
}
85+
8086
private handlePlugins() {
8187
if (this.lineHighlight) {
8288
this.setPluginClass(this.element.nativeElement, PrismPlugin.LineHighlight);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-markdown",
3-
"version": "7.1.3",
3+
"version": "7.1.4-beta.0",
44
"description": "Angular library that uses marked to parse markdown to html combined with Prism.js for synthax highlights",
55
"homepage": "https://github.com/jfcere/ngx-markdown",
66
"license": "MIT",

0 commit comments

Comments
 (0)