Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active June 11, 2020 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WebReflection/2727374b5b9831499134c883ec2b4554 to your computer and use it in GitHub Desktop.
Save WebReflection/2727374b5b9831499134c883ec2b4554 to your computer and use it in GitHub Desktop.
Markdown UX shenanigans

The following represents this typing:

1. this is parent
  1. this is child 1

* this is parent
  * this is child 1

which results into:

  1. this is parent
  2. this is child 1
  • this is parent
    • this is child 1

The markdown language was born to be meaningful on plain text, as well as emails, but it got this very specific ordered list case wrong at some point, when it requires an extra space compared to the regular unordered list one, defeating everyone intent into returning, and writing, a new ordered point in there!

That means that to have an ordered list, you need to brain-muscle an extra space:

1. this is parent
   1. this is child 1

* this is parent
  * this is child 1

Resulting into the desired layout:

  1. this is parent
    1. this is child 1
  • this is parent
    • this is child 1
@duanemoody
Copy link

Compare the list parsing code in the original CPAN Markdown module with its JS counterpart in markdown-it/list.js and you'll notice the following:

  • The Perl code is pure regex that intentionally avoids doing any direct string position/offset calculation on the MD text which likely makes it less prone to off-by-one errors. Unfortunately those regexes also use lookbehinds (19 of them, in fact) which weren't available to JS when markdown-it was created 6 years ago, only got implemented in Chrome when they entered ES2018, and still aren't implemented in FF/Safari as of this writing (Firefox has them in the upcoming 78 and Safari is supposedly working on it). So it's not surprising that markdown-it isn't a straight port of Markdown.pm.
  • The JS code primarily branches on list type; the Perl code primarily branches on list level, meaning the Perl code's level code is outside and therefore uniform between types. In theory one more elegant code block could have handled both level cases in the Perl code, except that the author noticed it inexplicably failed when running inside MovableType so he punted and split it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment