Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/drafakiller/markdown-dart

A simple Markdown parser for Dart. Create your own custom Markdown syntax.
https://github.com/drafakiller/markdown-dart

dart markdown package

Last synced: 25 days ago
JSON representation

A simple Markdown parser for Dart. Create your own custom Markdown syntax.

Awesome Lists containing this project

README

        

[![Pub.dev package](https://img.shields.io/badge/pub.dev-marked-blue)](https://pub.dev/packages/marked)
[![GitHub repository](https://img.shields.io/badge/GitHub-Markdown--dart-blue?logo=github)](https://github.com/DrafaKiller/Markdown-dart)

# Markdown

A simple-setup Markdown syntax parser for Dart.

Create your own custom Markdown syntax.

## Features

* Simple Markdown syntax setup
* Generic Markdown-base for any use-case
* Apply the Markdown to any text
* Attach placeholders to modify the input

## Getting started

Install it using pub:
```
dart pub add marked
```

And import the package:
```dart
import 'package:marked/marked.dart';
```

## Usage

Create a Markdown instance with all the placeholders you want to use.

Then, use the `apply` method to parse the Markdown syntax.

```dart
import 'package:marked/marked.dart';

final markdown = Markdown.map({
'**': (text, match) => '$text',
'*': (text, match) => '$text',
'__': (text, match) => '$text',
});

void main() {
print(
markdown.apply('''
Hello **World**!
__Looks *pretty* easy__
''')
);

// Output:
// Hello World!
// Looks pretty easy
}
```

## Placeholders

Placeholders are modular elements that can be used to create a Markdown syntax.
They are used to replace a specific part of the text that matches a pattern.

```dart
MarkdownPlaceholder(RegExp(r'\*\*(.*?)\*\*'), (text, match) => '$text');
```

To make it easier to create placeholders, there are some predefined methods:

```dart
MarkdownPlaceholder.enclosed('**', (text, match) => '$text');
// Hello **World**! -> Hello World!

MarkdownPlaceholder.tag('strong', (text, match) => '$text');
// Hello World! -> Hello World!

MarkdownPlaceholder.regexp(r'\*\*(.*?)\*\*', (text, match) => '$text');
// Hello **World**! -> Hello World!
```

## Placeholder Mapping

To Simplify the markdown definition, you may transform a set of entries into placeholders, given the related string key. Using the `Markdown.map()` method.

Usage example, with the following placeholder equivalent:
```dart
'*': (text, match) => '$text'
MarkdownPlaceholder.enclosed('*', (text, match) => '$text'),

'': (text, match) => '[$text]'
MarkdownPlaceholder.tag('custom', (text, match) => '[$text]'),

'/\*([^*]+)\*/': (text, match) => '$text'
MarkdownPlaceholder.regexp('/\*([^*]+)\*/', (text, match) => '$text'),
```

A prefix can be used to ensure the right placeholder is used.
```dart
'enclosed: *': (text, match) => '$text'
'tag: ': (text, match) => '[$text]'
'regexp: \*([^*]+)\*': (text, match) => '$text'
```

### Types of placeholders:

| Type | Description | Prefix | Symbol |
| ---- | ----------- | ------ | ------ |
| Normal | Applies the default placeholder, which is **enclosed**. | `normal: ` | None |
| Enclosed | Starts and ends with the same token, like **\*** for `*text*`. | `enclosed: ` | None |
| Basic | Single token placeholder, for a basic replacement. | `basic: ` | None |
| Sticky | Same as **enclosed**, but tokens must be next to a character. | `sticky: ` | `[...]` |
| Split | Splits the start token at ` \| ` to set the end token, like `/* \| */` matching with `/*text*/`. | `split: ` | `... \| ...` |
| RegExp | Matches a regular expression, the **text** is the first capture group. | `regexp: ` | `/.../` |
| Tag | Starts with a tag of type **\** and ending with **\**, HTML-like.
Tags may have properties, `key[="value"]`, defined as ``, and can be fetched using `match.tagProperties`. | `tag: ` | `<...>` |

**Note:**
When using a unique character token, the created placeholders will be symmetrical with no nesting, this is so it's more intuitive.

## Escaping

To escape a placeholder, you can use the `\` character.
You may also escape the escape character, instances of **\\\\** will be replaced with **\\**, since they are escaped.

An input can be manually escaped and unescaped using the methods `markdown.escape(input)` and `markdown.unescape(input)`.

## Example

```dart
import 'package:marked/marked.dart';

final htmlMarkdown = Markdown({
MarkdownPlaceholder.enclosed('**', (text, match) => '$text'),
MarkdownPlaceholder.enclosed('*', (text, match) => '$text'),
MarkdownPlaceholder.enclosed('~~', (text, match) => '$text'),
MarkdownPlaceholder.enclosed('`', (text, match) => '$text'),
});

void main() {
print(htmlMarkdown.apply('HTML Markdown: **bold** *italic* ~~strike~~ `code`'));

// [Output]
// HTML Markdown: bold italic strike code
}
```

More Examples:
* [HTML Markdown](https://pub.dev/packages/marked/example)
* [Markdown Map](https://github.com/DrafaKiller/Markdown-dart/blob/main/example/mapped.dart)