-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for task list items (#8)
Fixes #7
- Loading branch information
Showing
17 changed files
with
195 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
markdown/src/main/kotlin/com/appmattus/markdown/rules/ConsistentTaskListMarkerStyleRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.appmattus.markdown.rules | ||
|
||
import com.appmattus.markdown.dsl.RuleSetup | ||
import com.appmattus.markdown.errors.ErrorReporter | ||
import com.appmattus.markdown.processing.MarkdownDocument | ||
import com.appmattus.markdown.rules.config.TaskListItemMarkerStyle | ||
import com.appmattus.markdown.rules.extentions.style | ||
|
||
/** | ||
* # Task list item marker style | ||
* | ||
* This rule is triggered when the symbols used in the document for task list item markers do not match the configured | ||
* task list item marker style: | ||
* | ||
* - [x] | ||
* - [X] | ||
* | ||
* To fix this issue, use the configured style for task list item markers throughout the document: | ||
* | ||
* - [x] | ||
* - [x] | ||
* | ||
* Note: the configured task list item marker style can be a specific case ([TaskListItemMarkerStyle.Lowercase], | ||
* [TaskListItemMarkerStyle.Uppercase]), or simply require that the usage be | ||
* [TaskListItemMarkerStyle.Consistent] within the document. | ||
*/ | ||
class ConsistentTaskListMarkerStyleRule( | ||
private val style: TaskListItemMarkerStyle = TaskListItemMarkerStyle.Lowercase, | ||
override val config: RuleSetup.Builder.() -> Unit = {} | ||
) : Rule() { | ||
|
||
override val description = "Task list item marker style" | ||
override val tags = listOf("task-list", "ul") | ||
|
||
override fun visitDocument(document: MarkdownDocument, errorReporter: ErrorReporter) { | ||
|
||
val doneMarkers = document.taskListItems.filter { it.isItemDoneMarker } | ||
|
||
if (doneMarkers.isEmpty()) { | ||
return | ||
} | ||
|
||
val docStyle = if (style == TaskListItemMarkerStyle.Consistent) doneMarkers.first().style() else style | ||
|
||
doneMarkers.forEach { | ||
if (it.style() != docStyle) { | ||
errorReporter.reportError(it.startOffset, it.endOffset, description) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
markdown/src/main/kotlin/com/appmattus/markdown/rules/TaskListMarkerSpaceRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.appmattus.markdown.rules | ||
|
||
import com.appmattus.markdown.dsl.RuleSetup | ||
import com.appmattus.markdown.errors.ErrorReporter | ||
import com.appmattus.markdown.processing.MarkdownDocument | ||
|
||
/** | ||
* # Spaces after list markers | ||
* | ||
* This rule checks for the number of spaces between a task list marker (e.g. '[ ]', '[x]' or '[X]') and the text of | ||
* the list item. | ||
* | ||
* The number of spaces checked for depends on the document style in use, but the default is 1 space after any task list | ||
* marker: | ||
* | ||
* - [ ] Foo | ||
* - [x] Bar | ||
* - [ ] Baz | ||
* | ||
* To fix this, ensure the correct number of spaces are used after task list markers for your selected document style. | ||
*/ | ||
class TaskListMarkerSpaceRule( | ||
private val indent: Int = 1, | ||
override val config: RuleSetup.Builder.() -> Unit = {} | ||
) : Rule() { | ||
|
||
override val description = "Spaces after task list markers" | ||
override val tags = listOf("task-list", "ul", "whitespace") | ||
|
||
override fun visitDocument(document: MarkdownDocument, errorReporter: ErrorReporter) { | ||
|
||
document.taskListItems.forEach { item -> | ||
|
||
if (item.firstChild.startOffset - item.markerSuffix.endOffset != indent) { | ||
errorReporter.reportError(item.startOffset, item.endOffset, description) | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
markdown/src/main/kotlin/com/appmattus/markdown/rules/config/TaskListItemMarkerStyle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.appmattus.markdown.rules.config | ||
|
||
enum class TaskListItemMarkerStyle { | ||
Consistent, Uppercase, Lowercase | ||
} |
12 changes: 12 additions & 0 deletions
12
markdown/src/main/kotlin/com/appmattus/markdown/rules/extentions/TaskListItemExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.appmattus.markdown.rules.extentions | ||
|
||
import com.appmattus.markdown.rules.config.TaskListItemMarkerStyle | ||
import com.vladsch.flexmark.ext.gfm.tasklist.TaskListItem | ||
|
||
fun TaskListItem.style(): TaskListItemMarkerStyle? { | ||
return when (this.markerSuffix.toString()) { | ||
"[x]" -> TaskListItemMarkerStyle.Lowercase | ||
"[X]" -> TaskListItemMarkerStyle.Uppercase | ||
else -> null | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...own/src/test/kotlin/com/appmattus/markdown/rules/ConsistentTaskListMarkerStyleRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.appmattus.markdown.rules | ||
|
||
import com.appmattus.markdown.rules.config.TaskListItemMarkerStyle | ||
import org.spekframework.spek2.Spek | ||
import org.spekframework.spek2.style.gherkin.Feature | ||
|
||
object ConsistentTaskListMarkerStyleRuleTest : Spek({ | ||
Feature("ConsistentTaskListMarkerStyleRule") { | ||
|
||
FileRuleScenario(listOf("task-list-marker.md")) { | ||
ConsistentTaskListMarkerStyleRule(TaskListItemMarkerStyle.Consistent) | ||
} | ||
|
||
FileRuleScenario(listOf("task-list-marker.md")) { | ||
ConsistentTaskListMarkerStyleRule(TaskListItemMarkerStyle.Lowercase) | ||
} | ||
|
||
FileRuleScenario(listOf("task-list-marker-uppercase.md")) { | ||
ConsistentTaskListMarkerStyleRule(TaskListItemMarkerStyle.Uppercase) | ||
} | ||
|
||
FileRuleScenario(listOf("task-list-marker-no-complete.md")) { | ||
ConsistentTaskListMarkerStyleRule(TaskListItemMarkerStyle.Consistent) | ||
} | ||
|
||
FileRuleScenario { ConsistentTaskListMarkerStyleRule() } | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
markdown/src/test/kotlin/com/appmattus/markdown/rules/TaskListMarkerSpaceRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.appmattus.markdown.rules | ||
|
||
import org.spekframework.spek2.Spek | ||
import org.spekframework.spek2.style.gherkin.Feature | ||
|
||
object TaskListMarkerSpaceRuleTest : Spek({ | ||
Feature("TaskListMarkerSpaceRule") { | ||
FileRuleScenario(listOf("task-list-marker.md")) { TaskListMarkerSpaceRule() } | ||
|
||
FileRuleScenario(listOf("task-list-marker-large-indent.md")) { TaskListMarkerSpaceRule(indent = 2) } | ||
|
||
FileRuleScenario { TaskListMarkerSpaceRule() } | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Ensure task list is okay | ||
|
||
- [ ] First task item | ||
- [x]Second item {TaskListMarkerSpaceRule} | ||
- [x] Third item {TaskListMarkerSpaceRule} | ||
- [ ] Fourth item {TaskListMarkerSpaceRule} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Ensure task list is okay | ||
|
||
- [ ] First task item | ||
- [ ] Second item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Ensure task list is okay | ||
|
||
- [ ] First task item | ||
- [x] Second item should be uppercase {ConsistentTaskListMarkerStyleRule} | ||
- [X] Third item is okay |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Ensure task list is okay | ||
|
||
- [ ] First task item {TaskListMarkerSpaceRule} | ||
- [x]Second item {TaskListMarkerSpaceRule} | ||
- [X] Third inconsistent item {ConsistentTaskListMarkerStyleRule} |