I'm trying to write an Angular component that renders markdown files as part of the webpage, using the ngx-markdown
library. Looking at the library's official demo, it has a list of files that it require
s, which are then rendered from:
From the demo's app.component.ts
:
blockquotes = require('raw-loader!./markdown/blockquotes.md');
codeAndSynthaxHighlighting = require('raw-loader!./markdown/code-and-synthax-highlighting.md');
emphasis = require('raw-loader!./markdown/emphasis.md');
headers = require('raw-loader!./markdown/headers.md');
horizontalRule = require('raw-loader!./markdown/horizontal-rule.md');
images = require('raw-loader!./markdown/images.md');
links = require('raw-loader!./markdown/links.md');
lists = require('raw-loader!./markdown/lists.md');
listsDot = require('raw-loader!./markdown/lists-dot.md');
tables = require('raw-loader!./markdown/tables.md');
And from the demo's app.component.html
:
<!-- HEADER -->
<section id="headers">
<h2 class="subtitle">Headers</h2>
<pre>{{ headers }}</pre>
<markdown>{{ headers }}</markdown>
</section>
There are other sections that read from the other require
s, but the syntax is the same.
What I'm trying to do is to have a method that changes which file the <markdown>
tag reads from. Here's what I have so far:
// Markdown variable.
markdown;
ngOnInit() {
this.setMarkdown('home.md');
}
setMarkdown(file: string) {
const path = 'raw-loader!./assets/markdown/' + file;
this.markdown = require(path);
}
I'm obviously doing something wrong, since I get a compiler error:
ERROR in src/app/app.component.ts(24,21): error TS2591: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i @types/node` and then add `node` to the types field in your tsconfig.
What am I doing wrong, and how would I go about writing a method that changes the markdown source and actually works?
Rendering Markdown In Angular 1 Choosing The Library. In order to gain the full control of the rendered output we’ll be needing a library parsing the markdown text in some sort of abstract data format ... 2 Rendering The Data Tree. ... 3 Parsing The Source Text. ... 4 Try it Yourself. ...
The MarkdownBlock component mimics the very same behaviour of MarkdownRoot. As a matter of fact, both components share the same template since they are rendering their views the exact same way: The component body does very little since most of the magic is done by Angular rendering the view.
Selecting the DOM Element If you want to dynamically render a view, you can imagine there’s an element that serves as an anchor point to tell Angular where the view should be inserted. If you had experience with jQuery, it would be similar to:
To create a component, we simply include its selector in the template of another component, and Angular automatically does the rest. However, when we are dynamically creating views, there are two intermediary steps necessary. We need a ComponentFactory, which will be used to manually (i.e. dynamically) create a component.
Base on the documentation https://www.npmjs.com/package/ngx-markdown#directive you can load file via [src]
:
<!-- loaded from remote url -->
<div markdown [src]="'path/to/file.md'" (load)="onLoad($event)" (error)="onError($event)"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With