From c3b965145bc7fb6c798d96b6e992a66b0cb857bd Mon Sep 17 00:00:00 2001 From: Ika Date: Sat, 14 Oct 2017 06:53:58 -0500 Subject: [PATCH] feat(markdown): auto-detect which number style of ordered list to be used (#3029) --- src/printer-markdown.js | 15 ++++++++++++++- tests/markdown/__snapshots__/jsfmt.spec.js.snap | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 15 +++++++++++++-- tests/markdown_list/git-diff-friendly.md | 3 +++ .../__snapshots__/jsfmt.spec.js.snap | 6 +++--- 5 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 tests/markdown_list/git-diff-friendly.md diff --git a/src/printer-markdown.js b/src/printer-markdown.js index a11c7ac0e..3469a763a 100644 --- a/src/printer-markdown.js +++ b/src/printer-markdown.js @@ -175,10 +175,23 @@ function genericPrint(path, options, print) { node, path.getParentNode() ); + + const isGitDiffFriendlyOrderedList = + node.ordered && + node.children.length > 1 && + /^\s*1(\.|\))/.test( + options.originalText.slice( + node.children[1].position.start.offset, + node.children[1].position.end.offset + ) + ); + return printChildren(path, options, print, { processor: (childPath, index) => { const prefix = node.ordered - ? (index === 0 ? node.start : 1) + + ? (index === 0 + ? node.start + : isGitDiffFriendlyOrderedList ? 1 : node.start + index) + (nthSiblingIndex % 2 === 0 ? ". " : ") ") : nthSiblingIndex % 2 === 0 ? "* " : "- "; return concat([prefix, align(prefix.length, childPath.call(print))]); diff --git a/tests/markdown/__snapshots__/jsfmt.spec.js.snap b/tests/markdown/__snapshots__/jsfmt.spec.js.snap index 14ccb719a..c0d8e3f7c 100644 --- a/tests/markdown/__snapshots__/jsfmt.spec.js.snap +++ b/tests/markdown/__snapshots__/jsfmt.spec.js.snap @@ -2243,7 +2243,7 @@ This is a paragraph. > ## This is a header. > > 1. This is the first list item. -> 1. This is the second list item. +> 2. This is the second list item. > > Here's some example code: > diff --git a/tests/markdown_list/__snapshots__/jsfmt.spec.js.snap b/tests/markdown_list/__snapshots__/jsfmt.spec.js.snap index 269b242d1..d2a582b41 100644 --- a/tests/markdown_list/__snapshots__/jsfmt.spec.js.snap +++ b/tests/markdown_list/__snapshots__/jsfmt.spec.js.snap @@ -11,6 +11,17 @@ exports[`checkbox.md 1`] = ` `; +exports[`git-diff-friendly.md 1`] = ` +5. abc +1. def +999. ghi +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +5. abc +1. def +1. ghi + +`; + exports[`long-paragraph.md 1`] = ` - This is a long long long long long long long long long long long long long long paragraph. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -113,7 +124,7 @@ exports[`start.md 1`] = ` 7. ghi ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 5. abc -1. def -1. ghi +6. def +7. ghi `; diff --git a/tests/markdown_list/git-diff-friendly.md b/tests/markdown_list/git-diff-friendly.md new file mode 100644 index 000000000..d4ddbe9dc --- /dev/null +++ b/tests/markdown_list/git-diff-friendly.md @@ -0,0 +1,3 @@ +5. abc +1. def +999. ghi diff --git a/tests/markdown_spec/__snapshots__/jsfmt.spec.js.snap b/tests/markdown_spec/__snapshots__/jsfmt.spec.js.snap index 4e26d910b..ff0403ae7 100644 --- a/tests/markdown_spec/__snapshots__/jsfmt.spec.js.snap +++ b/tests/markdown_spec/__snapshots__/jsfmt.spec.js.snap @@ -2765,8 +2765,8 @@ exports[`example-243.md 1`] = ` 3. bar ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. foo -1. -1. bar +2. +3. bar `; @@ -2967,7 +2967,7 @@ exports[`example-261.md 1`] = ` 3) baz ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. foo -1. bar +2. bar 3) baz