Struggling to create an Angular component that can display markdown files on a webpage using the ngx-markdown
library. The official demo of the library showcases a list of files it needs through require
, which are then rendered:
In 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 in the demo's app.component.html
:
<!-- HEADER -->
<section id="headers">
<h2 class="subtitle">Headers</h2>
<pre>{{ headers }}</pre>
<markdown>{{ headers }}</markdown>
</section>
Other sections follow this same pattern with different file requirements.
My goal is to create a method that dynamically changes the source file for the <markdown>
tag. Here's my attempt:
// Markdown variable.
markdown;
ngOnInit() {
this.setMarkdown('home.md');
}
setMarkdown(file: string) {
const path = 'raw-loader!./assets/markdown/' + file;
this.markdown = require(path);
}
However, I'm running into 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.
How can I fix this issue and successfully implement a method that changes the markdown content source?