Skip to content

Commit

Permalink
fix(content): render markdown content in resolver for markdown route (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed May 23, 2023
1 parent 70262c6 commit 096b45e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/content/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { injectContentFiles } from './lib/inject-content-files';
export { ContentFile } from './lib/content-file';
export { ContentRenderer } from './lib/content-renderer';
export { default as MarkdownComponent } from './lib/markdown.component';
export { default as MarkdownRouteComponent } from './lib/markdown-route.component';
export { MarkdownContentRendererService } from './lib/markdown-content-renderer.service';
export {
provideContent,
Expand Down
38 changes: 38 additions & 0 deletions packages/content/src/lib/markdown-route.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { AsyncPipe } from '@angular/common';
import {
AfterViewChecked,
Component,
inject,
Input,
ViewEncapsulation,
} from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import { ActivatedRoute } from '@angular/router';

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

@Component({
selector: 'analog-markdown-route',
standalone: true,
imports: [AsyncPipe],
hostDirectives: [AnchorNavigationDirective],
preserveWhitespaces: true,
encapsulation: ViewEncapsulation.None,
template: `<div [innerHTML]="content" [class]="classes"></div>`,
})
export default class AnalogMarkdownRouteComponent implements AfterViewChecked {
private sanitizer = inject(DomSanitizer);
private route = inject(ActivatedRoute);
contentRenderer = inject(ContentRenderer);

protected content: SafeHtml = this.sanitizer.bypassSecurityTrustHtml(
this.route.snapshot.data['renderedAnalogContent']
);

@Input() classes = 'analog-markdown-route';

ngAfterViewChecked() {
this.contentRenderer.enhance();
}
}
20 changes: 17 additions & 3 deletions packages/router/src/lib/markdown-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import { inject } from '@angular/core';
import { RouteExport } from './models';

export function toMarkdownModule(
markdownFileFactory: () => Promise<string>
): () => Promise<RouteExport> {
return () =>
Promise.all([import('@analogjs/content'), markdownFileFactory()]).then(
([{ parseRawContentFile, MarkdownComponent }, markdownFile]) => {
([
{ parseRawContentFile, MarkdownRouteComponent, ContentRenderer },
markdownFile,
]) => {
const { content, attributes } = parseRawContentFile(markdownFile);
const { title, meta } = attributes;

return {
default: MarkdownComponent,
routeMeta: { data: { _analogContent: content }, title, meta },
default: MarkdownRouteComponent,
routeMeta: {
data: { _analogContent: content },
title,
meta,
resolve: {
renderedAnalogContent: async () => {
const contentRenderer = inject(ContentRenderer);
return contentRenderer.render(content);
},
},
},
};
}
);
Expand Down

0 comments on commit 096b45e

Please sign in to comment.