Created
June 16, 2023 16:38
-
-
Save dev-sampsonorson/18c1af9364974336d6809ff4a19cdd17 to your computer and use it in GitHub Desktop.
Use dynamic import in pipe to parse markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Pipe({ name: 'markdownToHtml' }) | |
export class MarkdownToHtmlPipe implements PipeTransform { | |
transform(markdown: string): Observable<string> { | |
return markdown | |
// import(...) will only run once per instance | |
// The `marked` library will only be imported if the template | |
// has markdown; <div class="markdown-container" [innerHTML]="markdownContent | markdownToHtml | async | sanitize: 'html'"></div> | |
? from(import(/* webpackPrefetch: true */ 'marked')).pipe(map(({ parse }) => parse(markdown))) | |
: of('') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment