Skip to content
Merged
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
14 changes: 3 additions & 11 deletions packages/content/resources/src/content-files-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,15 @@ import {
InjectContentFilesFilterFunction,
} from '@analogjs/content';

import { toSignal } from '@angular/core/rxjs-interop';
import { from } from 'rxjs';

export function contentFilesResource<Attributes extends Record<string, any>>(
filterFn?: InjectContentFilesFilterFunction<Attributes> | undefined,
) {
const contentListLoader = injectContentListLoader<Attributes>();
const contentList = toSignal(
from(
contentListLoader().then((items) =>
filterFn ? items.filter(filterFn) : items,
),
),
const contentList = contentListLoader().then((items) =>
filterFn ? items.filter(filterFn) : items,
);

return resource({
params: contentList,
loader: async ({ params: list }) => list,
loader: () => contentList,
});
}
67 changes: 23 additions & 44 deletions packages/content/src/lib/markdown.component.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { AsyncPipe, isPlatformBrowser } from '@angular/common';
import { isPlatformBrowser } from '@angular/common';
import {
AfterViewChecked,
Component,
Input,
NgZone,
OnChanges,
OnInit,
PLATFORM_ID,
ViewChild,
ViewContainerRef,
Signal,
ViewEncapsulation,
computed,
inject,
input,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import { ActivatedRoute, Data } from '@angular/router';
import { Observable, from, of } from 'rxjs';
import { catchError, map, mergeMap, filter } from 'rxjs/operators';
import { from, of } from 'rxjs';
import { catchError, map, switchMap } from 'rxjs/operators';

import { AnchorNavigationDirective } from './anchor-navigation.directive';
import { ContentRenderer } from './content-renderer';
Expand All @@ -25,19 +23,12 @@ import { MERMAID_IMPORT_TOKEN } from './provide-content';
@Component({
selector: 'analog-markdown',
standalone: true,
imports: [AsyncPipe],
hostDirectives: [AnchorNavigationDirective],
preserveWhitespaces: true,
encapsulation: ViewEncapsulation.None,
template: `<div
#container
[innerHTML]="content$ | async"
[class]="classes"
></div>`,
template: ` <div [innerHTML]="htmlContent()" [class]="classes()"></div> `,
})
export default class AnalogMarkdownComponent
implements OnInit, OnChanges, AfterViewChecked
{
export default class AnalogMarkdownComponent implements AfterViewChecked {
private sanitizer = inject(DomSanitizer);
private route = inject(ActivatedRoute);
private zone = inject(NgZone);
Expand All @@ -47,13 +38,20 @@ export default class AnalogMarkdownComponent
});
private mermaid: typeof import('mermaid') | undefined;

public content$: Observable<SafeHtml> = this.getContentSource();
private contentSource: Signal<SafeHtml | string | undefined> = toSignal(
this.getContentSource(),
);
readonly htmlContent = computed(() => {
const inputContent = this.content();

@Input() content!: string | object | undefined | null;
@Input() classes = 'analog-markdown';
if (inputContent) {
return this.sanitizer.bypassSecurityTrustHtml(inputContent as string);
}

@ViewChild('container', { static: true, read: ViewContainerRef })
container!: ViewContainerRef;
return this.contentSource();
});
readonly content = input<string | object | null>();
readonly classes = input('analog-markdown');

contentRenderer = inject(ContentRenderer);

Expand All @@ -64,29 +62,10 @@ export default class AnalogMarkdownComponent
}
}

ngOnInit(): void {
this.updateContent();
}

ngOnChanges(): void {
this.updateContent();
}

updateContent() {
if (this.content && typeof this.content !== 'string') {
this.container.clear();
const componentRef = this.container.createComponent(this.content as any);
componentRef.changeDetectorRef.detectChanges();
} else {
this.content$ = this.getContentSource();
}
}

getContentSource() {
return this.route.data.pipe(
map<Data, string>((data) => this.content ?? data['_analogContent']),
filter((content) => typeof content === 'string'),
mergeMap((contentString) => this.renderContent(contentString)),
map<Data, string>((data) => data['_analogContent'] ?? ''),
switchMap((contentString) => this.renderContent(contentString)),
map((content) => this.sanitizer.bypassSecurityTrustHtml(content)),
catchError((e) => of(`There was an error ${e}`)),
);
Expand Down
Loading