Skip to content

Commit

Permalink
fix(content): make mermaid tree shakable (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanMeche committed Jul 24, 2023
1 parent e4387f2 commit b7cef8a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
8 changes: 6 additions & 2 deletions apps/blog-app/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { provideContent, withMarkdownRenderer } from '@analogjs/content';
import {
provideContent,
withMarkdownRenderer,
withMermaid,
} from '@analogjs/content';
import { provideFileRouter } from '@analogjs/router';
import { provideHttpClient } from '@angular/common/http';
import { ApplicationConfig } from '@angular/core';
Expand All @@ -12,7 +16,7 @@ export const appConfig: ApplicationConfig = {
providers: [
provideHttpClient(),
provideClientHydration(),
provideContent(withMarkdownRenderer({ enableMermaid: true })),
provideContent(withMarkdownRenderer(), withMermaid()),
provideFileRouter(
withInMemoryScrolling({ anchorScrolling: 'enabled' }),
withEnabledBlockingInitialNavigation()
Expand Down
1 change: 1 addition & 0 deletions packages/content/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
MarkdownContentRendererService,
provideContent,
withMarkdownRenderer,
withMermaid,
} from './lib/markdown-content-renderer.service';
export { default as MarkdownRouteComponent } from './lib/markdown-route.component';
export { default as MarkdownComponent } from './lib/markdown.component';
Expand Down
35 changes: 14 additions & 21 deletions packages/content/src/lib/markdown-content-renderer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,25 @@ export class MarkdownContentRendererService implements ContentRenderer {
enhance() {}
}

export interface MarkdownRendererOptions {
enableMermaid: boolean;
export function withMarkdownRenderer(): Provider {
return {
provide: ContentRenderer,
useFactory: () => new MarkdownContentRendererService(),
deps: [MarkedSetupService],
};
}

export function withMarkdownRenderer(
options?: MarkdownRendererOptions
): Provider[] {
return [
{
provide: ContentRenderer,
useFactory: () => new MarkdownContentRendererService(),
deps: [MarkedSetupService],
},
...(options?.enableMermaid
? [
{
provide: USE_MERMAID_TOKEN,
useValue: true,
},
]
: []),
];
export function withMermaid(): Provider {
return {
provide: MERMAID_IMPORT_TOKEN,
useValue: import('mermaid'),
};
}

export function provideContent(...features: Provider[]) {
return [...features, MarkedSetupService];
}

export const USE_MERMAID_TOKEN = new InjectionToken<boolean>('use_mermaid');
export const MERMAID_IMPORT_TOKEN = new InjectionToken<
Promise<typeof import('mermaid')>
>('mermaid_import');
15 changes: 8 additions & 7 deletions packages/content/src/lib/markdown.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { catchError, map, mergeMap } from 'rxjs/operators';

import { AnchorNavigationDirective } from './anchor-navigation.directive';
import { ContentRenderer } from './content-renderer';
import { USE_MERMAID_TOKEN } from './markdown-content-renderer.service';
import { MERMAID_IMPORT_TOKEN } from './markdown-content-renderer.service';

@Component({
selector: 'analog-markdown',
Expand All @@ -35,8 +35,9 @@ export default class AnalogMarkdownComponent
private route = inject(ActivatedRoute);
private zone = inject(NgZone);
private readonly platformId = inject(PLATFORM_ID);
private readonly useMermaid =
inject(USE_MERMAID_TOKEN, { optional: true }) ?? false;
private readonly mermaidImport = inject(MERMAID_IMPORT_TOKEN, {
optional: true,
});
private mermaid: typeof import('mermaid') | undefined;

public content$: Observable<SafeHtml> = of('');
Expand All @@ -47,14 +48,14 @@ export default class AnalogMarkdownComponent
contentRenderer = inject(ContentRenderer);

constructor() {
if (isPlatformBrowser(this.platformId) && this.useMermaid) {
if (isPlatformBrowser(this.platformId) && this.mermaidImport) {
// Mermaid can only be loaded on client side
this.loadMermaid();
this.loadMermaid(this.mermaidImport);
}
}

async loadMermaid() {
this.mermaid = await import('mermaid');
async loadMermaid(mermaidImport: Promise<typeof import('mermaid')>) {
this.mermaid = await mermaidImport;
this.mermaid.default.initialize({ startOnLoad: false });
// Explicitly running mermaid as ngAfterViewChecked
// has probably already been called
Expand Down

0 comments on commit b7cef8a

Please sign in to comment.