Skip to content

Commit 7619739

Browse files
dirkgroenendimpu
authored andcommitted
Add support for Angular Universal
'document' is not available during Server Side Rendering. By using the 'he' library we're no longer depending on the DOM to exist, adding support for Angular Universal. Modified by Dan Church <[email protected]>
1 parent 9118436 commit 7619739

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

libs/ngx-md/src/lib/ngx-md.component.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { isPlatformBrowser } from '@angular/common';
1313
import * as Prism from 'prismjs';
1414
import { Subscribable } from 'rxjs';
1515
import { catchError } from 'rxjs/operators';
16+
import { decode } from 'he';
1617

1718
@Component({
1819
selector: 'markdown,[Markdown],ngx-md,[NgxMd]',
@@ -47,7 +48,7 @@ export class NgxMdComponent implements AfterViewInit {
4748
public _mdService: NgxMdService,
4849
public _el: ElementRef,
4950
@Inject(PLATFORM_ID) public platformId: string
50-
) {}
51+
) { }
5152

5253
@Input()
5354
set path(value: string) {
@@ -95,7 +96,7 @@ export class NgxMdComponent implements AfterViewInit {
9596
}
9697

9798
processRaw() {
98-
this._md = this.prepare(decodeHtml(this._el.nativeElement.innerHTML));
99+
this._md = this.prepare(decode(this._el.nativeElement.innerHTML));
99100
this._el.nativeElement.innerHTML = this._mdService.compile(
100101
this._md,
101102
this.sanitizeHtml
@@ -176,10 +177,3 @@ export class NgxMdComponent implements AfterViewInit {
176177
}
177178
}
178179
}
179-
180-
function decodeHtml(html: string) {
181-
// https://stackoverflow.com/a/7394787/588521
182-
const txt = document.createElement('textarea');
183-
txt.innerHTML = html;
184-
return txt.value;
185-
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"@angular/platform-browser-dynamic": "^8.2.14",
7575
"@angular/router": "^8.2.14",
7676
"core-js": "^3.6.0",
77+
"he": "^1.2.0",
7778
"json-align": "^0.1.0",
7879
"lodash": "^4.17.15",
7980
"marked": "^0.7.0",

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -5614,6 +5614,11 @@ [email protected]:
56145614
is-stream "^2.0.0"
56155615
type-fest "^0.8.0"
56165616

5617+
he@^1.2.0:
5618+
version "1.2.0"
5619+
resolved "https://registry.yarnpkg.com/he/-/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f"
5620+
integrity sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==
5621+
56175622
hmac-drbg@^1.0.0:
56185623
version "1.0.1"
56195624
resolved "https://registry.yarnpkg.com/hmac-drbg/-/hmac-drbg-1.0.1.tgz#d2745701025a6c775a6c545793ed502fc0c649a1"

0 commit comments

Comments
 (0)