diff --git a/lib/folder_views/note_tile.dart b/lib/folder_views/note_tile.dart index e279678d..ddae961e 100644 --- a/lib/folder_views/note_tile.dart +++ b/lib/folder_views/note_tile.dart @@ -1,5 +1,8 @@ +import 'dart:convert'; + import 'package:flutter/material.dart'; import 'package:gitjournal/core/note.dart'; +import 'package:gitjournal/utils/markdown.dart'; typedef void NoteSelectedFunction(Note note); @@ -11,11 +14,18 @@ class NoteTile extends StatelessWidget { @override Widget build(BuildContext context) { - var body = note.body.trimRight(); + var buffer = StringBuffer(); + var i = 0; + for (var line in LineSplitter.split(note.body)) { + line = replaceMarkdownChars(line); + buffer.writeln(line); - body = body.replaceAll('[ ]', '☐'); - body = body.replaceAll('[x]', '☑'); - body = body.replaceAll('[X]', '☑'); + i += 1; + if (i == 12) { + break; + } + } + var body = buffer.toString(); var theme = Theme.of(context); var textTheme = theme.textTheme; diff --git a/lib/utils/markdown.dart b/lib/utils/markdown.dart index 5bff85c4..ce3fb151 100644 --- a/lib/utils/markdown.dart +++ b/lib/utils/markdown.dart @@ -18,9 +18,7 @@ String stripMarkdownFormatting(String markdown) { if (line.isEmpty) { continue; } - line = line.replaceFirst('[ ]', '☐'); - line = line.replaceFirst('[x]', '☑'); - line = line.replaceFirst('[X]', '☑'); + line = replaceMarkdownChars(line); output.write(line.trim()); output.write(' '); @@ -28,3 +26,31 @@ String stripMarkdownFormatting(String markdown) { return output.toString(); } + +String replaceMarkdownChars(String line) { + line = line.replaceFirst('[ ]', '☐'); + line = line.replaceFirst('[x]', '☑'); + line = line.replaceFirst('[X]', '☑'); + + line = replaceListChar(line, '*'); + line = replaceListChar(line, '-'); + line = replaceListChar(line, '+'); + + return line; +} + +String replaceListChar(String line, String char) { + const String bullet = '•'; + + var starPos = line.indexOf(char); + if (starPos == 0) { + line = line.replaceFirst(char, bullet); + } else if (starPos != -1) { + var beforeStar = line.substring(0, starPos); + if (beforeStar.trim().isEmpty) { + line = line.replaceFirst(char, bullet, starPos); + } + } + + return line; +} diff --git a/test/strip_markdown_formatting_test.dart b/test/strip_markdown_formatting_test.dart index 2074da59..3b2fd8b9 100644 --- a/test/strip_markdown_formatting_test.dart +++ b/test/strip_markdown_formatting_test.dart @@ -27,7 +27,7 @@ look like: """; expect(stripMarkdownFormatting(input), - 'Itemized lists look like: * this one * that one '); + 'Itemized lists look like: • this one • that one '); }); test('Checklist', () { @@ -41,5 +41,17 @@ look like: expect(stripMarkdownFormatting(input), 'Itemized lists ☐ this one ☑ that one ☑ last '); }); + + test('List', () { + var input = """Itemized lists + +* this one + * that one +* four +"""; + + expect(stripMarkdownFormatting(input), + 'Itemized lists • this one • that one • four '); + }); }); }