Merge pull request dart-lang/markdown#5 from filiph/master
Implement inlineOnly optional argument to markdownToHtml
diff --git a/pkgs/markdown/lib/markdown.dart b/pkgs/markdown/lib/markdown.dart
index 01de970..33d7c37 100644
--- a/pkgs/markdown/lib/markdown.dart
+++ b/pkgs/markdown/lib/markdown.dart
@@ -14,15 +14,20 @@
typedef Node Resolver(String name);
/// Converts the given string of markdown to HTML.
-String markdownToHtml(String markdown, {inlineSyntaxes, linkResolver}) {
+String markdownToHtml(String markdown, {inlineSyntaxes, linkResolver,
+ bool inlineOnly: false}) {
final document = new Document(inlineSyntaxes: inlineSyntaxes,
linkResolver: linkResolver);
- // Replace windows line endings with unix line endings, and split.
- final lines = markdown.replaceAll('\r\n','\n').split('\n');
- document.parseRefLinks(lines);
- final blocks = document.parseLines(lines);
- return renderToHtml(blocks);
+ if (inlineOnly) {
+ return renderToHtml(document.parseInline(markdown));
+ } else {
+ // Replace windows line endings with unix line endings, and split.
+ final lines = markdown.replaceAll('\r\n','\n').split('\n');
+ document.parseRefLinks(lines);
+ final blocks = document.parseLines(lines);
+ return renderToHtml(blocks);
+ }
}
/// Replaces `<`, `&`, and `>`, with their HTML entity equivalents.
diff --git a/pkgs/markdown/test/markdown_test.dart b/pkgs/markdown/test/markdown_test.dart
index fcf76d1..63ffb91 100644
--- a/pkgs/markdown/test/markdown_test.dart
+++ b/pkgs/markdown/test/markdown_test.dart
@@ -855,6 +855,46 @@
// things are not quite working properly. The regexps are sometime a little
// too greedy, I think.
});
+
+ group('Inline only', () {
+ validate('simple line', '''
+ This would normally create a paragraph.
+ ''', '''
+ This would normally create a paragraph.
+ ''', inlineOnly: true);
+ validate('strong and em', '''
+ This would _normally_ create a **paragraph**.
+ ''', '''
+ This would <em>normally</em> create a <strong>paragraph</strong>.
+ ''', inlineOnly: true);
+ validate('link', '''
+ This [link](http://www.example.com/) will work normally.
+ ''', '''
+ This <a href="http://www.example.com/">link</a> will work normally.
+ ''', inlineOnly: true);
+ validate('references do not work', '''
+ [This][] shouldn't work, though.
+ ''', '''
+ [This][] shouldn't work, though.
+ ''', inlineOnly: true);
+ validate('less than and ampersand are escaped', '''
+ < &
+ ''', '''
+ < &
+ ''', inlineOnly: true);
+ validate('keeps newlines', '''
+ This paragraph
+ continues after a newline.
+ ''', '''
+ This paragraph
+ continues after a newline.
+ ''', inlineOnly: true);
+ validate('ignores block-level markdown syntax', '''
+ 1. This will not be an <ol>.
+ ''', '''
+ 1. This will not be an <ol>.
+ ''', inlineOnly: true);
+ });
}
/**
@@ -886,13 +926,14 @@
}
validate(String description, String markdown, String html,
- {bool verbose: false, inlineSyntaxes, linkResolver}) {
+ {bool verbose: false, inlineSyntaxes, linkResolver,
+ bool inlineOnly: false}) {
test(description, () {
markdown = cleanUpLiteral(markdown);
html = cleanUpLiteral(html);
var result = markdownToHtml(markdown, inlineSyntaxes: inlineSyntaxes,
- linkResolver: linkResolver);
+ linkResolver: linkResolver, inlineOnly: inlineOnly);
var passed = compareOutput(html, result);
if (!passed) {