marked 0.0.2 marked: ^0.0.2 copied to clipboard
A simple Markdown parser for Dart. Create your own custom Markdown syntax.
Markdown #
A simple Markdown parser for Dart.
Create your own custom Markdown syntax.
Features #
- Attach placeholder replacements to your Markdown syntax
- Simple to create a Markdown syntax
- Easily improvable and extendable, with better organization
- Generalized for most use cases
Getting started #
Install it using pub:
dart pub add marked
And import the package:
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.
import 'package:marked/marked.dart';
final markdown = Markdown.map({
'**': (text, match) => '<b>$text</b>',
'*': (text, match) => '<i>$text</i>',
'~~': (text, match) => '<strike>$text</strike>',
'__': (text, match) => '<u>$text</u>',
'`': (text, match) => '<code>$text</code>',
});
void main() {
print(
markdown.apply('''
Hello **World**!
__Looks *pretty* easy__
''')
);
// Output:
// Hello <b>World</b>!
// <u>Looks <i>pretty</i> easy</u>
}
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.
MarkdownPlaceholder(RegExp(r'\*\*(.*?)\*\*'), (text, match) => '<b>$text</b>');
To make it easier to create placeholders, there are some predefined methods:
MarkdownPlaceholder.enclosed('**', (text, match) => '<b>$text</b>');
// Hello **World**! -> Hello <b>World</b>!
MarkdownPlaceholder.tag('strong', (text, match) => '<b>$text</b>');
// Hello <strong>World</strong>! -> Hello <b>World</b>!
MarkdownPlaceholder.regexp(r'\*\*(.*?)\*\*', (text, match) => '<b>$text</b>');
// Hello **World**! -> Hello <b>World</b>!
When creating a placeholder, you can attribute a name to it. This allows you to specify which placeholder to apply.
... ({
MarkdownPlaceholder(name: 'bold', ... );
});
markdown.apply('Hello, **World**!', { 'bold' });
Example #
import 'package:marked/marked.dart';
final htmlMarkdown = Markdown({
MarkdownPlaceholder.enclosed('**', (text, match) => '<b>$text</b>'),
MarkdownPlaceholder.enclosed('*', (text, match) => '<i>$text</i>'),
MarkdownPlaceholder.enclosed('~~', (text, match) => '<strike>$text</strike>'),
MarkdownPlaceholder.enclosed('`', (text, match) => '<code>$text</code>'),
});
void main() {
print(htmlMarkdown.apply('HTML Markdown: **bold** *italic* ~~strike~~ `code`'));
// [Output]
// HTML Markdown: <b>bold</b> <i>italic</i> <strike>strike</strike> <code>code</code>
}
More Examples: