Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/liolle/mdparser

markdown parser
https://github.com/liolle/mdparser

markdown npm-package parser typescript

Last synced: 2 months ago
JSON representation

markdown parser

Awesome Lists containing this project

README

        

## mdParser

Generate an
[Abstract Syntax Tree ](https://en.wikipedia.org/wiki/Abstract_syntax_tree) from
a Markdown file, providing a structured representation that you can manipulate
or render in various formats such as HTML, plain text, or any custom output.

## Installation

```bash
npm install @edllx/md-parser
```

## Features

| Name | | | |
| -------------- | -------- | ------------ | -------- |
| Heading | ✅ | Tables | ❌ |
| Unordered list | ✅ | Ordered list | ❌ |
| Bold | ✅ | Quotes | ❌ |
| Italic | ✅ | Unicode | ❌ |
| Links | ✅ | | |
| Inline code | ✅ | | |
| Code block | ✅ | | |

## Usage

```ts
import { Factory, tokenize, Token } from '@edllx/md-parser';

const tokens: Token[] = tokenize(`## Header2`);

const root: Token = Factory.ROOT(tokens);
```

### Create and adapter

Here is an
[example](https://github.com/liolle/mdPreviewer/blob/main/src/components/preview/mdadapter.tsx)

```ts
import {
Token,
TokenCompiler,
Word,
NewLine,
Paragraph,
LinkToken,
Heading,
ListToken,
Decoration,
InlineCode,
CodeToken,
CheckBoxToken,
} from '@edllx/md-parser';

import { JSXElement } from 'solid-js';

/**
interface TokenCompiler {
compile: (token: Token) => T;
}
**/

export class TokenToTsxAdapter implements TokenCompiler {
compile(token: Token) {
return this.#recursiveCompile(token);
}

#recursiveCompile(token: Token): JSXElement {
switch (true) {
case token instanceof Word:
return TODO;

case token instanceof NewLine:
return TODO;

case token instanceof Paragraph:
return TODO;

case token instanceof LinkToken:
return TODO;

case token instanceof ListToken:
return TODO;

case token instanceof Heading:
return TODO;

case token instanceof Decoration:
return TODO;

case token instanceof InlineCode:
return TODO;

case token instanceof CodeToken:
return TODO;

default:
return TODO;
}
}
}
```

### use the adapter

```ts
import { Factory, tokenize, Token } from '@edllx/md-parser';
import { JSXElement } from 'solid-js';
import { TokenToTsxAdapter } from 'path-to-adapter';

const compiler = new TokenToTsxAdapter();
const tokens: Token[] = tokenize(`## Header2`);
const root: Token = Factory.ROOT(tokens);
const component: JSXElement = compiler.compile(root);
```

[Demo](https://md-viewer.kodevly.com/)