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

fix: markdown pipe should render in zoneless mode #473

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 6 additions & 3 deletions lib/src/markdown.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ export interface MarkdownModuleConfig {
sanitize?: SecurityContext;
}

const sharedImports = [
MarkdownPipe,
];

const sharedDeclarations = [
ClipboardButtonComponent,
LanguagePipe,
MarkdownComponent,
MarkdownPipe,
];

@NgModule({
imports: [CommonModule],
exports: sharedDeclarations,
imports: [CommonModule, sharedImports],
exports: [sharedDeclarations, sharedImports],
declarations: sharedDeclarations,
})
export class MarkdownModule {
Expand Down
10 changes: 4 additions & 6 deletions lib/src/markdown.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */

import { ElementRef, NgZone, ViewContainerRef } from '@angular/core';
import { fakeAsync, TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing';
import { DomSanitizer } from '@angular/platform-browser';

import { MarkdownModule } from './markdown.module';
Expand Down Expand Up @@ -63,21 +63,19 @@ describe('MarkdownPipe', () => {
});
});

it('should render element through MarkdownService when zone is stable', fakeAsync(() => {
it('should render element through MarkdownService when zone is stable', () => {

const markdown = '# Markdown';
const mockPipeOptions: MarkdownPipeOptions = { mermaid: true, mermaidOptions: { darkMode: true } };

spyOn(markdownService, 'render');

pipe.transform(markdown, mockPipeOptions);

expect(markdownService.render).not.toHaveBeenCalled();

zone.onStable.emit(null);
zone.run(() => pipe.transform(markdown, mockPipeOptions));

expect(markdownService.render).toHaveBeenCalledWith(elementRef.nativeElement, mockPipeOptions, viewContainerRef);
}));
});

it('should return parsed markdown', () => {

Expand Down
18 changes: 15 additions & 3 deletions lib/src/markdown.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type MarkdownPipeOptions = ParseOptions & RenderOptions;

@Pipe({
name: 'markdown',
standalone: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is part of a complete rewrite to the standalone see more #484

})
export class MarkdownPipe implements PipeTransform {

Expand All @@ -31,9 +32,20 @@ export class MarkdownPipe implements PipeTransform {

const markdown = this.markdownService.parse(value, options);

this.zone.onStable
.pipe(first())
.subscribe(() => this.markdownService.render(this.elementRef.nativeElement, options, this.viewContainerRef));
const render = () =>
this.markdownService.render(
this.elementRef.nativeElement,
options,
this.viewContainerRef
);

// This check is required for zoneless apps because `onStable` would never emit any value
// when the `NoopNgZone` is used over the `NgZone`.
if (NgZone.isInAngularZone()) {
this.zone.onStable.pipe(first()).subscribe(render);
} else {
Promise.resolve().then(render);
}

return this.domSanitizer.bypassSecurityTrustHtml(markdown);
}
Expand Down