Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: markdown does not support nested lists #611

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 70 additions & 5 deletions lib/src/plugins/markdown/decoder/document_markdown_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
[convertLineToNode(lines[i], customInlineSyntaxes)],
);
i++;
} else if (lines[i].startsWith("```")) {
String codeBlock = "";
codeBlock += "${lines[i]}\n";
} else if (lines[i].startsWith('```')) {
String codeBlock = '';
codeBlock += '${lines[i]}\n';
int tempLinePointer = i;
i++;
while (i < lines.length && !lines[i].endsWith("```")) {
codeBlock += "${lines[i]}\n";
while (i < lines.length && !lines[i].endsWith('```')) {
codeBlock += '${lines[i]}\n';
i++;
}

Expand All @@ -73,6 +73,53 @@

document.insert([i], [node]);
i++;
} else if (i + 1 < lines.length && isNestedList(lines[i], lines[i + 1])) {
String text = lines[i];
int tempLinePointer = i;
int currentIndent = getIndentLevel(lines[i]);
i++;

while (i < lines.length &&
(lines[i].trimLeft().startsWith('- ') ||
lines[i].trimLeft().startsWith('* ')) &&

Check warning on line 84 in lib/src/plugins/markdown/decoder/document_markdown_decoder.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/plugins/markdown/decoder/document_markdown_decoder.dart#L84

Added line #L84 was not covered by tests
getIndentLevel(lines[i]) > currentIndent) {
text += '\n${lines[i]}';
i++;
}
List<String> listLines = text.split('\n');
Node? rootList;
Node? currentParent;
int indentLevel = 0;

for (String line in listLines) {
currentIndent = getIndentLevel(line);
Node newNode =
convertLineToNode(line.trimLeft(), customInlineSyntaxes);

if (rootList == null) {
rootList = newNode;
currentParent = newNode;
} else if (currentIndent > indentLevel) {
currentParent!.insert(newNode);
currentParent = newNode;
} else {
while (
currentIndent <= indentLevel && currentParent!.parent != null) {
currentParent = currentParent.parent;
indentLevel--;
}
if (currentParent != rootList) {
currentParent!.parent!.insert(newNode);

Check warning on line 112 in lib/src/plugins/markdown/decoder/document_markdown_decoder.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/plugins/markdown/decoder/document_markdown_decoder.dart#L112

Added line #L112 was not covered by tests
} else {
rootList.insert(newNode);
}
currentParent = newNode;
}
indentLevel = currentIndent;
}
if (rootList != null) {
document.insert([tempLinePointer], [rootList]);
}
} else {
document.insert(
[i],
Expand All @@ -85,6 +132,24 @@
return document;
}

bool isNestedList(String line1, String line2) {
if ((line1.trimLeft().startsWith('- ') ||
line1.trimLeft().startsWith('* ')) &&
(line2.trimLeft().startsWith('- ') ||
line2.trimLeft().startsWith('* '))) {
return getIndentLevel(line2) > getIndentLevel(line1);
}
return false;
}

int getIndentLevel(String line) {
int indent = 0;
while (indent < line.length && line[indent] == ' ') {
indent++;
}
return (indent / 2).floor();
}

Node convertLineToNode(
String line,
List<md.InlineSyntax> customInlineSyntaxes,
Expand Down
132 changes: 132 additions & 0 deletions test/plugins/markdown/decoder/document_markdown_decoder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,120 @@ void main() async {
}
}
''';
const example4 = '''
{
"document": {
"type": "page",
"children": [
{
"type": "heading",
"data": {
"delta": [
{
"insert": "Welcome to AppFlowy"
}
],
"level": 1
}
},
{
"type": "paragraph",
"data": {
"delta": []
}
},
{
"type": "heading",
"data": {
"delta": [
{
"insert": "Tasks"
}
],
"level": 2
}
},
{
"type": "paragraph",
"data": {
"delta": []
}
},
{
"type": "bulleted_list",
"children": [
{
"type": "bulleted_list",
"children": [
{
"type": "bulleted_list",
"data": {
"delta": [
{
"insert": "Task Two"
}
]
}
}
],
"data": {
"delta": [
{
"insert": "Task One + Parent"
}
]
}
},
{
"type": "bulleted_list",
"data": {
"delta": [
{
"insert": "Task Three"
}
]
}
}
],
"data": {
"delta": [
{
"insert": "Task Parent One"
}
]
}
},
{
"type": "bulleted_list",
"data": {
"delta": [
{
"insert": "Task Four"
}
]
}
},
{
"type": "bulleted_list",
"data": {
"delta": [
{
"insert": "Task Five"
}
]
}
},
{
"type": "paragraph",
"data": {
"delta": []
}
}
]
}
}
''';

setUpAll(() {
TestWidgetsFlutterBinding.ensureInitialized();
});
Expand Down Expand Up @@ -698,6 +812,24 @@ void main(){
expect(result.toJson(), data);
});

test('test nested list', () async {
const markdown = '''
# Welcome to AppFlowy

## Tasks

- Task Parent One
- Task One + Parent
- Task Two
- Task Three
- Task Four
- Task Five
''';
final result = DocumentMarkdownDecoder().convert(markdown);
final data = jsonDecode(example4);
expect(result.toJson(), data);
});

test('decode uncommon markdown table', () async {
const markdown = r'''
| ## \|a|_c_|
Expand Down
Loading