Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I also tried: 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.

I just followed example https://hackernoon.com/import-json-into-typescript-8d465beded79 which is for json files I followed same for markdown files but no success.

I am not using webpack and any loader, so I just wanted it only from typescript

like image 844
Lokesh Daiya Avatar asked Jun 21 '17 14:06

Lokesh Daiya


People also ask

How do I import MD files?

Import a Markdown file Select the desired folder in which you want to import the Markdown (. md) file. Then, select File > Import > Markdown File.

What is a .MD Markdown file?

MARKDOWN file extension. MD files are saved in plain text format that uses Markdown language which also includes inline text symbols, defining how a text can be formatted such as indentations, table formatting, fonts, and headers.

How do I access Markdown files?

Open the Markdown file in a Markdown application. Use the Markdown application to convert the Markdown file to an HTML document. View the HTML file in a web browser or use the Markdown application to convert it to another file format, like PDF.


2 Answers

For those using React with Typescript:

Create a globals.d.ts file in your root directory with the following code:

declare module "*.md";

then import it like this:

import readme from "../README.md" // substitute this path with your README.md file path
like image 70
Stephani Bishop Avatar answered Oct 01 '22 02:10

Stephani Bishop


Angular 8, TypeScript 3.5.2

Create a file globals.d.ts in the src folder (has to be in src to work), add:

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. This worked with serve and build --prod also. Make sure to restart your serve if testing it in live reload mode for the first time.

There's a cleaner process for importing json - see the TypeScript 2.9 Release Documentation

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

like image 16
Drenai Avatar answered Oct 01 '22 01:10

Drenai