mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-30 19:36:25 +08:00
Improve stripMarkdown formatting
At some point this will need to be done in another thread.
This commit is contained in:
@ -1,5 +1,8 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:gitjournal/core/note.dart';
|
import 'package:gitjournal/core/note.dart';
|
||||||
|
import 'package:gitjournal/utils/markdown.dart';
|
||||||
|
|
||||||
typedef void NoteSelectedFunction(Note note);
|
typedef void NoteSelectedFunction(Note note);
|
||||||
|
|
||||||
@ -11,11 +14,18 @@ class NoteTile extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
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('[ ]', '☐');
|
i += 1;
|
||||||
body = body.replaceAll('[x]', '☑');
|
if (i == 12) {
|
||||||
body = body.replaceAll('[X]', '☑');
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var body = buffer.toString();
|
||||||
|
|
||||||
var theme = Theme.of(context);
|
var theme = Theme.of(context);
|
||||||
var textTheme = theme.textTheme;
|
var textTheme = theme.textTheme;
|
||||||
|
@ -18,9 +18,7 @@ String stripMarkdownFormatting(String markdown) {
|
|||||||
if (line.isEmpty) {
|
if (line.isEmpty) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
line = line.replaceFirst('[ ]', '☐');
|
line = replaceMarkdownChars(line);
|
||||||
line = line.replaceFirst('[x]', '☑');
|
|
||||||
line = line.replaceFirst('[X]', '☑');
|
|
||||||
|
|
||||||
output.write(line.trim());
|
output.write(line.trim());
|
||||||
output.write(' ');
|
output.write(' ');
|
||||||
@ -28,3 +26,31 @@ String stripMarkdownFormatting(String markdown) {
|
|||||||
|
|
||||||
return output.toString();
|
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;
|
||||||
|
}
|
||||||
|
@ -27,7 +27,7 @@ look like:
|
|||||||
""";
|
""";
|
||||||
|
|
||||||
expect(stripMarkdownFormatting(input),
|
expect(stripMarkdownFormatting(input),
|
||||||
'Itemized lists look like: * this one * that one ');
|
'Itemized lists look like: • this one • that one ');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Checklist', () {
|
test('Checklist', () {
|
||||||
@ -41,5 +41,17 @@ look like:
|
|||||||
expect(stripMarkdownFormatting(input),
|
expect(stripMarkdownFormatting(input),
|
||||||
'Itemized lists ☐ this one ☑ that one ☑ last ');
|
'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 ');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user