From 9cdc35e705002b98009ea9f4fbb05cba8663c3f9 Mon Sep 17 00:00:00 2001 From: Estevao Soares dos Santos Date: Tue, 24 Oct 2017 15:15:56 +0100 Subject: [PATCH] feat(ol start num): add support for defining the first num of ol Implement support for starting ordered lists at an arbitrary number Closes #377 BREAKING CHANGE: Since showdown now supports starting ordered lists at an arbitrary number, list output may differ. --- dist/showdown.js | Bin 165902 -> 166352 bytes dist/showdown.js.map | Bin 411179 -> 412547 bytes dist/showdown.min.js | Bin 95486 -> 95633 bytes dist/showdown.min.js.map | Bin 72597 -> 72857 bytes src/subParsers/lists.js | 24 +++++++++++++++---- test/cases/ordered-list-starting-number.html | 12 ++++++++++ test/cases/ordered-list-starting-number.md | 10 ++++++++ test/cases/ordered-list-wrong-numbers.md | 4 ++-- 8 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 test/cases/ordered-list-starting-number.html create mode 100644 test/cases/ordered-list-starting-number.md diff --git a/dist/showdown.js b/dist/showdown.js index 9998a7493e5ca06593f4abf850974ae19c4bc758..b715bc6f5a5339c382a48d8140b7a56798ccd5c0 100644 GIT binary patch delta 473 zcmaixF-yZh7>3Cyg5AWeD7?1VOVZldK`4ZPt6&?kLI+zRO%oeTlbT$G;-G_b5jx!9 z-w-nU58MTJM|THvsa5FW4F|`4&-2~)-t;l|I?LTZNY7-wp0w-aY2(fv2R8G<05O~R zPK#L)YmSFKCxYyIF{?oAXOoem;5LVJX&UT-V_(5@5&E!#eu}14jNSwmf;rsxT-S-5 zfPoFH0JRKFiA*YVFbo4UG<~`d12ZFjon`FQK4LCrQ>;M1& delta 82 zcmcaGnX7LCS3?Wq7A8)M=@ZzQ)TUPiGHFa#c*w}Io!5eCy5ROBUQ9;J({=oq!lwWB jXOh{z%#X>J5yUK;zBGVI2E>w_{wRPcc>Cf&CS3~vf(9Hz diff --git a/dist/showdown.js.map b/dist/showdown.js.map index fc89a378de4bdd30bf5240d933fc41b9f36fc6db..69b66767a0227ff7c0131f27b26c09761b79816c 100644 G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 304 zcmbREn)TmH)`l&NU!u62bv!kz%Myzeil*<0X4IKJJ(^L4u>`~ps86(2D$P-{wY7D& zFS07p$+J~bvMx$3DJ{y=HB_{xhuI z25ZQ-*T~bhRf@^8QPOtSR(8<@-QB?HQ)K;?52Pt3&tI$cD zK0Ag{ULE8Dpy5!(`I?%VR@e-it{BTGtfP?*l*O3Ox&4{9?iOplFaT+Ldf Z;i%(0{a_rBuN%kM&sem5Zye(lF#vr8S_l9D diff --git a/dist/showdown.min.js.map b/dist/showdown.min.js.map index 81b657db826f23e4a413c903fb3f59d7e410392c..a4022f1bb7ac57b21bc3dc4fbbb746cf6855cdb4 100644 GIT binary patch delta 2898 zcma)8&2L*p5ZAUzX^NUAO=!}DQku4^NFGh%uhX>6?^yo#hLIu=H7qxFQ;wWnloI^-K1f1zA7XRjx;Q8jtcnl ztT5u7FL0C>gdw@C3d82y)|yi&k2M<7aKO%3q-8L(eISByRp7!+GPgk>7jPaoQkt5!r(1dAwl$D0oxKHK5*ld|i_nJ=JcJ{|iE z>Q#h=Lju*LUsh@60-CviU`=PQuXkh%f#nhslGJ#O=H}joVO)UNbHXHyIs9f1xB`

~Fp*k}}P2%6;gba!@=OvE8W$8clShzc1u%W(&g+-5( zZa~bzWK-~!^=9R}!REss`wv46)FsB|J4#glnwVB^GnVZr>`!<9X6zGow1CB(y4p~? zhgp`5sB>+sr+;*esC7c#*a%8+U|v_Z+u4vRwy_f(rE#q?uHI~8LmegPC1B4js*l^) z>qbqlxqh>A(yjHaoI= z|0tVnAK23KMI+$w*b=QR(zd9ovz=_{sOERRtn5zqL1zfFw_>!R(`EIblYQ9jxk=Mm zl9PGz(@lFhJ_hK(kYA9rPyKO%UAYn_3Q1p<>DaK14Ck2~S6r$aI2ssa;gxbg@^S4h5u<1*o#jtB!8AcDb-Akb?xW zu zg~4{SfFX!5=Jz_M#hqu8%Bwee*uZdDH*cy4NYJJcvA>Q2Jp2#l>f0W6Vf+8{*A}X-qc*@ll8Sf^H$)AL?!|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{U6Ag-o``|3p#p8Y8 zIj3+k+l!$LmMBoOU14pNd~(AjVQts9A5Ow{U1~8qpiV1+SoCXjn`3OLil@W$3SV{A zC^?I6sR!Ll3r&nB54#a0D0?l24aQQGFJiR1?j3YzMrHpe>oLvR{dNFm`X(AmZAl9z zn7u4bmSksFlpq_~)H^@?IMg~5hcYElM3E#h`ce02yez39gZ z7tv)+FNlMOVg177CMP0Oq?DMF4je7b%rF&*IYrr{S$Us`i@S$mG`cyh)bL2-lAwa- zB5DIA!m5LuAT}vx*W~02$Ax^A#S2FuwB1I#Br|SG&ypll5^YVR08ymFcw)zfcn}{< zge9js<&$p~_MOFMSyx5`8L^NwL549E*PCBmCCH3+nGA=ErjO|*Jt)370+$a2q)TQ- zrbYizxVT-wtjR-Bo$i1lLs&g(V6!R_+MB9-o($)jWXMG~oIb_nwAE+jUI?m2&XiCE zhat+lHgth`+5>wR;+vx|vHe_wJ$>cC%*xrIm^qASwQvxy^bN&?$|ggR%(j~ntt(vF zq$N?;u#AL6j-eHzssd5*ETbqqW&l#EsJT(ugfKzk$V&^+Dm)Cz9z&*TlwgI33DMd#R7x#aUIb(p5Z@ zOfRUNGbu?j7l;cRizvqnTk@iy1<&^?l7F% zt}E9L0qVkM^p!PSY?x*&wt-S4&Av{q?0UJuwin}IK9(WnNqV9o-oY@;r`8SndB*>N>hfI#C|=%l!Yx(IP5g zX`+7d?g{wm0Fj7<+~yVW<|vFF+QLzwEgPfm3iFV{JoCsSz8i%D$2Vl{l2bScD9>oC s__er^7k`ez`Jrlrn$f}ry-l}u5gLOthnsk7FssDN@`2eEQ5XZ?Kk7Z}ivR!s diff --git a/src/subParsers/lists.js b/src/subParsers/lists.js index 1b2c07d..96d08f9 100644 --- a/src/subParsers/lists.js +++ b/src/subParsers/lists.js @@ -3,7 +3,6 @@ */ showdown.subParser('lists', function (text, options, globals) { 'use strict'; - text = globals.converter._dispatch('lists.before', text, options, globals); /** * Process the contents of a single ordered or unordered list, splitting it @@ -123,6 +122,17 @@ showdown.subParser('lists', function (text, options, globals) { return listStr; } + function styleStartNumber (list, listType) { + // check if ol and starts by a number different than 1 + if (listType === 'ol') { + var res = list.match(/^ *(\d+)\./); + if (res && res[1] !== '1') { + return ' start="' + res[1] + '"'; + } + } + return ''; + } + /** * Check and parse consecutive lists (better fix for issue #142) * @param {string} list @@ -140,10 +150,11 @@ showdown.subParser('lists', function (text, options, globals) { if (list.search(counterRxg) !== -1) { (function parseCL (txt) { - var pos = txt.search(counterRxg); + var pos = txt.search(counterRxg), + style = styleStartNumber(list, listType); if (pos !== -1) { // slice - result += '\n<' + listType + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '\n'; + result += '\n<' + listType + style + '>\n' + processListItems(txt.slice(0, pos), !!trimTrailing) + '\n'; // invert counterType and listType listType = (listType === 'ul') ? 'ol' : 'ul'; @@ -152,16 +163,19 @@ showdown.subParser('lists', function (text, options, globals) { //recurse parseCL(txt.slice(pos)); } else { - result += '\n<' + listType + '>\n' + processListItems(txt, !!trimTrailing) + '\n'; + result += '\n<' + listType + style + '>\n' + processListItems(txt, !!trimTrailing) + '\n'; } })(list); } else { - result = '\n<' + listType + '>\n' + processListItems(list, !!trimTrailing) + '\n'; + var style = styleStartNumber(list, listType); + result = '\n<' + listType + style + '>\n' + processListItems(list, !!trimTrailing) + '\n'; } return result; } + /** Start of list parsing **/ + text = globals.converter._dispatch('lists.before', text, options, globals); // add sentinel to hack around khtml/safari bug: // http://bugs.webkit.org/show_bug.cgi?id=11231 text += 'ยจ0'; diff --git a/test/cases/ordered-list-starting-number.html b/test/cases/ordered-list-starting-number.html new file mode 100644 index 0000000..45666bb --- /dev/null +++ b/test/cases/ordered-list-starting-number.html @@ -0,0 +1,12 @@ +

    +
  1. foo
  2. +
  3. bar
  4. +
  5. baz
  6. +
+
+
    +
  1. a
  2. +
  3. b
  4. +
  5. c
  6. +
  7. d
  8. +
diff --git a/test/cases/ordered-list-starting-number.md b/test/cases/ordered-list-starting-number.md new file mode 100644 index 0000000..b5e9fde --- /dev/null +++ b/test/cases/ordered-list-starting-number.md @@ -0,0 +1,10 @@ +5. foo +6. bar +7. baz + +--- + +3. a +2. b +7. c +23. d diff --git a/test/cases/ordered-list-wrong-numbers.md b/test/cases/ordered-list-wrong-numbers.md index 643b57d..2fd21c6 100644 --- a/test/cases/ordered-list-wrong-numbers.md +++ b/test/cases/ordered-list-wrong-numbers.md @@ -1,4 +1,4 @@ - 8. Red + 1. Red 1. Green - 3. Blue + 1. Blue