Improve stripMarkdown formatting

At some point this will need to be done in another thread.
This commit is contained in:
Vishesh Handa
2020-04-02 18:13:20 +02:00
parent 26f641380d
commit 64e3d36130
3 changed files with 56 additions and 8 deletions

View File

@ -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;

View File

@ -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;
}

View File

@ -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 ');
});
});
}