File tree Expand file tree Collapse file tree 2 files changed +45
-3
lines changed Expand file tree Collapse file tree 2 files changed +45
-3
lines changed Original file line number Diff line number Diff line change @@ -192,11 +192,20 @@ function setBlockType(
192192/**
193193 * Returns any block formatting characters (plus trailing space) at the very start of the passed text
194194 * @param text The text to check for leading block characters
195+ * @internal
195196 */
196- function matchLeadingBlockCharacters ( text : string ) {
197+ export function matchLeadingBlockCharacters ( text : string ) {
197198 // TODO this might be too aggressive... remove based on a whitelist instead?
198- // TODO HACK assumes all block types are non-letter characters followed by a single space
199- return / ^ [ ^ a - z A - Z ] + \s { 1 } (? = [ a - z A - Z _ * [ ! ] | $ ) / . exec ( text ) ?. [ 0 ] || "" ;
199+ // Match ordered list markers; see https://spec.commonmark.org/0.30/#ordered-list-marker
200+ let match = / ^ ( \d + ) (?: \. | \) ) \s / . exec ( text ) ?. [ 0 ] ;
201+
202+ // If text is not an ordered list block, check for other block types
203+ if ( ! match ) {
204+ // TODO HACK assumes all non-ordered list block types are non-letter characters followed by a single space
205+ match = / ^ [ ^ a - z A - Z 0 - 9 ] + \s { 1 } (? = [ a - z A - Z 0 - 9 _ * [ ! ] | $ ) + / . exec ( text ) ?. [ 0 ] ;
206+ }
207+
208+ return match || "" ;
200209}
201210
202211/**
Original file line number Diff line number Diff line change @@ -521,4 +521,37 @@ some text`;
521521 } ) ;
522522 } ) ;
523523 } ) ;
524+
525+ describe ( "matchLeadingBlockCharacters" , ( ) => {
526+ it ( "return no leading characters" , ( ) => {
527+ const command = commands . matchLeadingBlockCharacters (
528+ "42 shouldn't be returned"
529+ ) ;
530+
531+ expect ( command ) . toBe ( "" ) ;
532+ } ) ;
533+
534+ it ( "return ordered list leading characters" , ( ) => {
535+ let command = commands . matchLeadingBlockCharacters ( "23. -ol item" ) ;
536+
537+ expect ( command ) . toBe ( "23. " ) ;
538+
539+ command = commands . matchLeadingBlockCharacters ( "23) -ol item" ) ;
540+
541+ expect ( command ) . toBe ( "23) " ) ;
542+ } ) ;
543+
544+ it ( "return unordered list leading characters" , ( ) => {
545+ const command = commands . matchLeadingBlockCharacters ( "- 1 ul item" ) ;
546+
547+ expect ( command ) . toBe ( "- " ) ;
548+ } ) ;
549+
550+ it ( "return heading leading characters" , ( ) => {
551+ const command =
552+ commands . matchLeadingBlockCharacters ( "## Heading level 2" ) ;
553+
554+ expect ( command ) . toBe ( "## " ) ;
555+ } ) ;
556+ } ) ;
524557} ) ;
You can’t perform that action at this time.
0 commit comments