Skip to content

Commit

Permalink
feat: add raw Markdown import (Mode.RAW)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanvanherwijnen authored and hmsk committed Feb 27, 2023
1 parent 4ff8531 commit 458da13
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ console.log(attributes) //=> { title: 'Awesome Title', description: 'Describe th
### Options

```ts
mode?: ('html' | 'toc' | 'react' | 'vue')[]
mode?: ('html' | 'toc' | 'react' | 'vue' | 'raw')[]
markdown?: (body: string) => string
markdownIt?: MarkdownIt | MarkdownIt.Options
```
Expand All @@ -68,6 +68,7 @@ console.log(Mode.HTML) //=> 'html'
console.log(Mode.TOC) //=> 'toc'
console.log(Mode.REACT) //=> 'react'
console.log(Mode.VUE) //=> 'vue'
console.log(Mode.RAW) //=> 'raw'
```

"Mode" enables you to import markdown file in various formats (HTML, ToC, React/Vue Component)
Expand All @@ -91,6 +92,18 @@ console.log(html) //=> "<h1>This is awesome</h1><p>ite is an opinionated web dev

</details>

#### `Mode.RAW`

<details>
<summary>Import the raw Markdown content</summary>

```js
import { raw } from './contents/the-doc.md'

console.log(raw) //=> "# This is awesome \n Vite is an opinionated web dev build tool that serves your code via native ES Module imports during dev and bundles it with Rollup for production."
```
</details>

#### `Mode.TOC`

<details>
Expand Down Expand Up @@ -216,6 +229,7 @@ export default {
</details>
</details>
### Type declarations
In TypeScript project, need to declare typedefs for `.md` file as you need.
Expand All @@ -231,6 +245,9 @@ declare module '*.md' {
// When "Mode.HTML" is requested
const html: string;

// When "Mode.RAW" is requested
const raw: string

// When "Mode.React" is requested. VFC could take a generic like React.VFC<{ MyComponent: TypeOfMyComponent }>
import React from 'react'
const ReactComponent: React.VFC;
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum Mode {
HTML = 'html',
REACT = 'react',
VUE = 'vue',
RAW = 'raw',
}

export interface PluginOptions {
Expand Down Expand Up @@ -62,6 +63,11 @@ const tf = (code: string, id: string, options: PluginOptions): TransformResult =
content.addExporting('html')
}

if (options.mode?.includes(Mode.RAW)) {
content.addContext(`const raw = ${JSON.stringify(fm.body)}`)
content.addExporting('raw')
}

if (options.mode?.includes(Mode.TOC)) {
const root = parseDOM(html)
const indicies = root.filter(
Expand Down

0 comments on commit 458da13

Please sign in to comment.