简体   繁体   English

如何在打字稿中导入 Markdown(.md) 文件

[英]How to import markdown(.md) file in typescript

I am trying to import readme files in typescript but getting "error module not found"我正在尝试在打字稿中导入自述文件,但收到“找不到错误模块”

My TS code我的 TS 代码

import * as readme from "./README.md"; // here i am getting error module not found

I also tried: typings.d.ts我也试过:typings.d.ts

declare module "*.md" {
    const value: any;
    export default value;
}

I found that in typescript 2.0 https://github.com/Microsoft/TypeScript/wiki/What 's-new-in-TypeScript#typescript-20 they have introduced "Wildcard character in module names" using that we can include any extension file.我发现在 typescript 2.0 https://github.com/Microsoft/TypeScript/wiki/What 's-new-in-TypeScript#typescript-20 中,他们引入了“模块名称中的通配符”,使用我们可以包含任何扩展名文件。

I just followed examplehttps://hackernoon.com/import-json-into-typescript-8d465beded79 which is for json files I followed same for markdown files but no success.我只是按照示例https://hackernoon.com/import-json-into-typescript-8d465beded79用于 json 文件,我对 Markdown 文件也遵循相同的示例,但没有成功。

I am not using webpack and any loader, so I just wanted it only from typescript我没有使用 webpack 和任何加载器,所以我只想要从打字稿

For those using React with Typescript: 对于那些使用React with Typescript的人:

Create a globals.d.ts file in your root directory with the following code: 使用以下代码在根目录中创建globals.d.ts文件:

declare module "*.md";

then import it like this: 然后像这样导入:

import readme from "../README.md" // substitute this path with your README.md file path

In your linked example, they are importing JSON, not Markdown. 在您的链接示例中,他们正在导入JSON,而不是Markdown。 They are able to import the JSON because valid JSON is also valid JavaScript/TypeScript. 他们能够导入JSON,因为有效的JSON也是有效的JavaScript / TypeScript。 Markdown is not valid TypeScript and so importing it like that just isn't going to work out of the box like that. Markdown是无效的TypeScript,因此像这样导入它不会像这样开箱即用。

If you want to access the Markdown file at runtime, then make an AJAX request to retrieve its contents. 如果要在运行时访问Markdown文件,请发出AJAX请求以检索其内容。 If you really want it built within your JavaScript itself, then you will need to have some sort of build script. 如果你真的想在JavaScript本身内构建它,那么你需要有一些构建脚本。 You mentioned you aren't using Webpack, but it will be able to achieve what you're looking for by adding a module rule tying /\\.md$/ to raw-loader . 您提到您没有使用Webpack,但它可以通过添加将/\\.md$/raw-loader的模块规则来实现您所需的功能。 You'll need to use some sort of equivalent. 你需要使用某种等价物。

Edit: 编辑:

It seems you learn something new every day. 看来你每天都在学习新东西。 As OP pointed out in comments, TypeScript 2.0 has support for importing non-code resources. 正如OP在评论中指出的那样,TypeScript 2.0支持导入非代码资源。

Try the following: 请尝试以下方法:

declare module "*!txt" {
    const content: string;
    export default content;
}

import readme from "./README.md!txt";

The readme value should then be a string containing the contents of README.md. 然后, readme值应该是包含README.md内容的字符串。

@Mitch's answer didn't work for me. @Mitch的回答对我不起作用。 Using angular v7, I found I could just use this syntax: import * as documentation from 'raw-loader!./documentation.md'; 使用angular v7,我发现我可以使用这种语法: import * as documentation from 'raw-loader!./documentation.md';

Angular 8, TypeScript 3.5.2 Angular 8,TypeScript 3.5.2

Create a file globals.d.ts in the src folder (has to be in src to work), add: src文件夹中创建一个文件globals.d.ts (必须在src中工作),添加:

declare module '*.md';

In your component or service import with: 在您的组件或服务导入中:

import * as pageMarkdown from 'raw-loader!./page.md';

In this case page.md was at the same level as the component I was importing it into. 在这种情况下, page.md与我导入它的组件处于同一级别。 This worked with serve and build --prod also. 这适用于servebuild --prod也是。 Make sure to restart your serve if testing it in live reload mode for the first time. 确保重新启动serve ,如果在现场装载方式,测试它的第一次。

There's a cleaner process for importing json - see the TypeScript 2.9 Release Documentation 有一个更简洁的导入json过程 - 请参阅TypeScript 2.9发行文档

Note: you don't have to name the file globals.d.ts , it could be called anything-at-all.d.ts , but that's the convention 注意:您不必将文件命名为globals.d.ts ,它可以被称为anything-at-all.d.ts ,但这是惯例

For those using React with Typescript:对于那些使用 React 和 Typescript 的人:

find file in src/react-app-env.d.ts file with the following code:使用以下代码在src/react-app-env.d.ts文件中找到文件:

declare module "*.md";

then import it like this:然后像这样导入:

import readme from "../README.md" // substitute this path with your README.md 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM