在 Angular 库中导入 Markdown 文件

发布于 2025-01-11 00:09:05 字数 703 浏览 1 评论 0原文

我目前正在尝试在我的 Angular 库中导入 .md(markdown)文件,以便我可以创建一个方法来将自述文件作为字符串传递回任何实现我的库的应用程序。

我已经通过在 src 文件夹中创建一个 global.d.ts 文件并为其提供以下内容,在标准应用程序中实现了此功能:

declare module '*.md';

然后,我可以使用以下命令将 markdown 导入到我的 app.component.ts 文件中:

import * as pageMarkdown from 'raw-loader!../../projects/ui/login/assets/1.md';

这按预期工作。但是,然后我尝试在使用以下方法生成的 Angular 库中执行相同的导入:

ng generate library example-library

如果我随后浏览到该库中的组件打字稿并添加相同的导入,则它不起作用。每当我构建库时,我都会收到以下错误:

找不到模块“raw-loader!../../projects/ui/login/assets/1.md”或其相应的类型声明。

当我将导入添加到库时,它几乎无法获取我的 global.d.ts 中声明的内容。我错过了什么吗?

任何帮助将不胜感激!提前致谢。

I am currently trying to import a .md (markdown) file within my Angular library so I can create a method to pass a readme file as a string back to whatever application implements my library.

I have already got this working in a standard application by creating a global.d.ts file in the src folder and giving it the following content:

declare module '*.md';

I am then able to import the markdown in my app.component.ts file using the following:

import * as pageMarkdown from 'raw-loader!../../projects/ui/login/assets/1.md';

This works as expected. However, I then try to do the same import in an Angular library I have generated using:

ng generate library example-library

If I then browse to the component typescript within that library and add the same import it doesn't work. Whenever I build the library I get the following error:

Cannot find module 'raw-loader!../../projects/ui/login/assets/1.md' or its corresponding type declarations.

Its almost as if it can't pick up whats declared within my global.d.ts when i'm adding the import to a library. Am I missing something?

Any help would be much appreciated! Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咿呀咿呀哟 2025-01-18 00:09:05

我建议您将 markdown 文件放在资产文件夹或文档中,并像资产文件夹一样公开它。这是我创建的一个服务,它使用marked和highlight.js呈现markdown

import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {firstValueFrom, from, Observable, of} from "rxjs";

@Injectable({
  providedIn: 'root'
})
export class MarkdownConverterService {

  constructor(
    private httpClient: HttpClient,
  ) { }

  convert(resourceRelUrl: string): Observable<string> {
    const parseMarkdown: Promise<string> = this.getMarkdownByPath(resourceRelUrl)
      .then(this.parseMarkdown)
    return from(parseMarkdown);
  }

  private async parseMarkdown(markdownString: string) {
    const marked = require('marked');
    const hljs = require('highlight.js');
    marked.setOptions({
      renderer: new marked.Renderer(),
      highlight: function (code: any) {
        return hljs.highlightAuto(code).value;
      },
      langPrefix: 'hljs language-', // highlight.js css expects a top-level 'hljs' class.
      pedantic: false,
      gfm: true,
      breaks: false,
      sanitize: false,
      smartLists: true,
      smartypants: false,
      xhtml: true
    });
    let html$ = of(marked.parse(markdownString));
    return await firstValueFrom(html$)
      .then(value => value.toString());
  }

  private async getMarkdownByPath(path: string): Promise<string> {
    // @ts-ignore
    let markdown$ = this.httpClient.get<string>(path, {responseType: "text"});
    return await firstValueFrom(markdown$)
      .then(value => value.toString());
  }

}

I recommend you place markdown files in the assets folder or in a docs and expose it like an assets folder. Here is a service i created that renders markdown using marked and highlight.js

import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {firstValueFrom, from, Observable, of} from "rxjs";

@Injectable({
  providedIn: 'root'
})
export class MarkdownConverterService {

  constructor(
    private httpClient: HttpClient,
  ) { }

  convert(resourceRelUrl: string): Observable<string> {
    const parseMarkdown: Promise<string> = this.getMarkdownByPath(resourceRelUrl)
      .then(this.parseMarkdown)
    return from(parseMarkdown);
  }

  private async parseMarkdown(markdownString: string) {
    const marked = require('marked');
    const hljs = require('highlight.js');
    marked.setOptions({
      renderer: new marked.Renderer(),
      highlight: function (code: any) {
        return hljs.highlightAuto(code).value;
      },
      langPrefix: 'hljs language-', // highlight.js css expects a top-level 'hljs' class.
      pedantic: false,
      gfm: true,
      breaks: false,
      sanitize: false,
      smartLists: true,
      smartypants: false,
      xhtml: true
    });
    let html$ = of(marked.parse(markdownString));
    return await firstValueFrom(html$)
      .then(value => value.toString());
  }

  private async getMarkdownByPath(path: string): Promise<string> {
    // @ts-ignore
    let markdown$ = this.httpClient.get<string>(path, {responseType: "text"});
    return await firstValueFrom(markdown$)
      .then(value => value.toString());
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文