diff --git a/markdown/src/main/kotlin/com/appmattus/markdown/rules/UlIndentRule.kt b/markdown/src/main/kotlin/com/appmattus/markdown/rules/UlIndentRule.kt index 0bc814b..a6593cd 100644 --- a/markdown/src/main/kotlin/com/appmattus/markdown/rules/UlIndentRule.kt +++ b/markdown/src/main/kotlin/com/appmattus/markdown/rules/UlIndentRule.kt @@ -1,10 +1,12 @@ package com.appmattus.markdown.rules -import com.appmattus.markdown.processing.MarkdownDocument import com.appmattus.markdown.dsl.RuleSetup import com.appmattus.markdown.errors.ErrorReporter -import com.appmattus.markdown.rules.extentions.indent -import com.appmattus.markdown.rules.extentions.level +import com.appmattus.markdown.processing.MarkdownDocument +import com.vladsch.flexmark.ast.BulletListItem +import com.vladsch.flexmark.ast.ListItem +import com.vladsch.flexmark.ast.OrderedListItem +import com.vladsch.flexmark.util.ast.Document /** * # Unordered list indentation @@ -32,7 +34,8 @@ import com.appmattus.markdown.rules.extentions.level * [Sub-lists not indenting](http://support.markedapp.com/discussions/problems/21-sub-lists-not-indenting) for a * description of the problem. * - * Based on [MD007](https://github.com/markdownlint/markdownlint/blob/master/lib/mdl/rules.rb) + * Based on [MD007](https://github.com/markdownlint/markdownlint/blob/master/lib/mdl/rules.rb) and + * [MD007](https://github.com/DavidAnson/markdownlint/blob/master/lib/md007.js) */ class UlIndentRule( private val indent: Int = 2, @@ -44,9 +47,32 @@ class UlIndentRule( override fun visitDocument(document: MarkdownDocument, errorReporter: ErrorReporter) { document.unorderedListItems.forEach { - if (it.indent() != it.level() * indent) { + val parentListItem = it.parentListItemOrNull() + + var expectedMarkerPos = parentListItem?.let { + document.chars.getColumnAtIndex(parentListItem.openingMarker.startOffset) + indent + } ?: 0 + + if (parentListItem is OrderedListItem) { + // When contained in an ordered list, align with it's content + expectedMarkerPos = document.chars.getColumnAtIndex(parentListItem.childChars.startOffset) + } + + val actualMarkerPos = document.chars.getColumnAtIndex(it.openingMarker.startOffset) + + if (expectedMarkerPos != actualMarkerPos) { errorReporter.reportError(it.startOffset, it.endOffset, description) } } } + + fun BulletListItem.parentListItemOrNull(): ListItem? { + var cur = parent + while (cur !is Document) { + cur = cur.parent + if (cur is ListItem) + return cur + } + return null + } } diff --git a/markdown/src/test/kotlin/com/appmattus/markdown/rules/UlIndentRuleTest.kt b/markdown/src/test/kotlin/com/appmattus/markdown/rules/UlIndentRuleTest.kt index 8bab315..5aea7f1 100644 --- a/markdown/src/test/kotlin/com/appmattus/markdown/rules/UlIndentRuleTest.kt +++ b/markdown/src/test/kotlin/com/appmattus/markdown/rules/UlIndentRuleTest.kt @@ -5,6 +5,10 @@ import org.spekframework.spek2.style.gherkin.Feature object UlIndentRuleTest : Spek({ Feature("UlIndentRule") { + FileRuleScenario(listOf("ul_indent_bugfixes.md")) { UlIndentRule() } + + FileRuleScenario(listOf("ul_indent_bugfixes_4_spaces.md")) { UlIndentRule(indent = 4) } + FileRuleScenario(listOf("bulleted_list_2_space_indent.md")) { UlIndentRule(indent = 4) } FileRuleScenario(listOf("spaces_after_list_marker.md")) { UlIndentRule(indent = 4) } diff --git a/markdown/src/test/resources/spaces_after_list_marker.md b/markdown/src/test/resources/spaces_after_list_marker.md index 5198962..24f624a 100755 --- a/markdown/src/test/resources/spaces_after_list_marker.md +++ b/markdown/src/test/resources/spaces_after_list_marker.md @@ -61,7 +61,7 @@ And with incorrect spacing: Ordered lists with children: 1. Foo {ListMarkerSpaceRule} - * Hi + * Hi {UlIndentRule} complains as should align with content above 1. Bar {ListMarkerSpaceRule} 1. Baz {ListMarkerSpaceRule} diff --git a/markdown/src/test/resources/ul_indent_bugfixes.md b/markdown/src/test/resources/ul_indent_bugfixes.md new file mode 100755 index 0000000..1693e3c --- /dev/null +++ b/markdown/src/test/resources/ul_indent_bugfixes.md @@ -0,0 +1,13 @@ +1. List item + - List item {UlIndentRule} top level item as not indented enough + - List item + - List item + - List item {UlIndentRule} top level item as not indented enough +1. List item + +1. List item + - List item + - List item + - List item + - List item +1. List item diff --git a/markdown/src/test/resources/ul_indent_bugfixes_4_spaces.md b/markdown/src/test/resources/ul_indent_bugfixes_4_spaces.md new file mode 100755 index 0000000..958315d --- /dev/null +++ b/markdown/src/test/resources/ul_indent_bugfixes_4_spaces.md @@ -0,0 +1,13 @@ +1. List item + - List item + - List item + - List item + - List item +1. List item + +1. List item + - List item + - List item + - List item + - List item +1. List item