Improve code to determine unordered list style for MD004/ul-style (fixes #104).

This commit is contained in:
David Anson 2018-01-31 21:32:50 -08:00
parent c0ea22e55e
commit 056608a680
4 changed files with 61 additions and 38 deletions

View file

@ -4,6 +4,19 @@
var shared = require("./shared"); var shared = require("./shared");
// Returns the unordered list style for a list item token
function unorderedListStyleFor(token) {
switch (token.markup) {
case "-":
return "dash";
case "+":
return "plus";
// case "*":
default:
return "asterisk";
}
}
module.exports = { module.exports = {
"names": [ "MD004", "ul-style" ], "names": [ "MD004", "ul-style" ],
"description": "Unordered list style", "description": "Unordered list style",
@ -15,10 +28,10 @@ module.exports = {
shared.flattenLists(params).forEach(function forList(list) { shared.flattenLists(params).forEach(function forList(list) {
if (list.unordered) { if (list.unordered) {
if (expectedStyle === "consistent") { if (expectedStyle === "consistent") {
expectedStyle = shared.unorderedListStyleFor(list.items[0]); expectedStyle = unorderedListStyleFor(list.items[0]);
} }
list.items.forEach(function forItem(item) { list.items.forEach(function forItem(item) {
var itemStyle = shared.unorderedListStyleFor(item); var itemStyle = unorderedListStyleFor(item);
if (style === "sublist") { if (style === "sublist") {
var nesting = list.nesting; var nesting = list.nesting;
if (!nestingStyles[nesting] && if (!nestingStyles[nesting] &&

View file

@ -100,19 +100,6 @@ module.exports.headingStyleFor = function headingStyleFor(token) {
return "setext"; return "setext";
}; };
// Returns the unordered list style for a list item token
module.exports.unorderedListStyleFor = function unorderedListStyleFor(token) {
switch (trimLeft(token.line).substr(0, 1)) {
case "-":
return "dash";
case "+":
return "plus";
// case "*":
default:
return "asterisk";
}
};
// Calls the provided function for each matching token // Calls the provided function for each matching token
function filterTokens(params, type, callback) { function filterTokens(params, type, callback) {
(params.tokenLists[type] || []).forEach(callback); (params.tokenLists[type] || []).forEach(callback);

View file

@ -29,8 +29,8 @@ Text
Text Text
> - Item > - Item {MD004}
> - Item > - Item {MD004}
Text Text
@ -40,35 +40,35 @@ Text
Text Text
> * Item > * Item {MD004}
> * Item > * Item {MD004}
> * Item {MD030} > * Item {MD004} {MD030}
> * Item {MD005} > * Item {MD004} {MD005}
> * Item {MD005} > * Item {MD004} {MD005}
Text Text
> Text > Text
> > - Item > > - Item {MD004}
> > - Item {MD027} > > - Item {MD004} {MD027}
> > - Item > > - Item {MD004}
> > > - Item > > > - Item {MD004}
> > > - Item {MD027} > > > - Item {MD004} {MD027}
> > > - Item {MD027} > > > - Item {MD004} {MD027}
Text Text
>* Item >+ Item
> * Item > + Item
>>* Item >>+ Item
>> * Item >> + Item
Text Text
> Quoted text > Quoted text
> >
> * A list > * A list {MD004}
> * of items > * of items {MD004}
> >
> More quoted text > More quoted text
@ -76,8 +76,8 @@ Text
> Quoted text > Quoted text
> >
> > * A list > > + A list
> > * of items > > + of items
> >
> More quoted text > More quoted text
@ -85,7 +85,7 @@ Text
> Quoted text > Quoted text
> >
> > * A list > > - A list {MD004}
> > * of items > > - of items {MD004}
> > > >
> > More quoted text > > More quoted text

View file

@ -0,0 +1,23 @@
# Header
Text
- Item
- Item
Text
>- Item
>- Item
Text
> - Item
> - Item
Text
> > - Item
> > - Item
Text