6

I just noticed that if you have some text and you add a dash in front of the first character, then when you hit Shift + Enter before any character in the text, then it gets rendered as a list.

This way, -hello bla ble becomes a list.

Steps to reproduce:

hello bla ble
-hello bla ble
 - hello bla ble
      ^
      cursor here, press Shift + Enter

Output:

 - hell

 - o bla ble

Same with bullets:

* hola adéu

Becomes

 * hola 
 * adéu
3
  • Ah, yes, it does, same for block quotes and code blocks. – Sebastian Simon Jul 2 '20 at 11:09
  • 2
    @user289905 hadn't seen it documented anywhere – fedorqui 'SO stop harming' Jul 2 '20 at 11:11
  • Also with numbered lists. – Glorfindel Jul 2 '20 at 12:28
6

Yup, it does. The source code of the editor is available here on Google Code Archive and the Markdown.Editor.js file contains the following lines:

// Auto-indent on shift-enter
util.addEvent(inputBox, "keyup", function (key) {
    if (key.shiftKey && !key.ctrlKey && !key.metaKey) {
        var keyCode = key.charCode || key.keyCode;
        // Character 13 is Enter
        if (keyCode === 13) {
            var fakeButton = {};
            fakeButton.textOp = bindCommand("doAutoindent");
            doClick(fakeButton);
        }
    }
});

What does "doAutoindent" do? Well, a couple of lines below, there's some regex magic, which you could analyze further to determine under which conditions the Shift+Enter works.

// When making a list, hitting shift-enter will put your cursor on the next line
// at the current indent level.
commandProto.doAutoindent = function (chunk, postProcessing) {

    var commandMgr = this,
        fakeSelection = false;

    chunk.before = chunk.before.replace(/(\n|^)[ ]{0,3}([*+-]|\d+[.])[ \t]*\n$/, "\n\n");
    chunk.before = chunk.before.replace(/(\n|^)[ ]{0,3}>[ \t]*\n$/, "\n\n");
    chunk.before = chunk.before.replace(/(\n|^)[ \t]+\n$/, "\n\n");
    
    // There's no selection, end the cursor wasn't at the end of the line:
    // The user wants to split the current list item / code line / blockquote line
    // (for the latter it doesn't really matter) in two. Temporarily select the
    // (rest of the) line to achieve this.
    if (!chunk.selection && !/^[ \t]*(?:\n|$)/.test(chunk.after)) {
        chunk.after = chunk.after.replace(/^[^\n]*/, function (wholeMatch) {
            chunk.selection = wholeMatch;
            return "";
        });
        fakeSelection = true;
    }

    if (/(\n|^)[ ]{0,3}([*+-]|\d+[.])[ \t]+.*\n$/.test(chunk.before)) {
        if (commandMgr.doList) {
            commandMgr.doList(chunk);
        }
    }
    if (/(\n|^)[ ]{0,3}>[ \t]+.*\n$/.test(chunk.before)) {
        if (commandMgr.doBlockquote) {
            commandMgr.doBlockquote(chunk);
        }
    }
    if (/(\n|^)(\t|[ ]{4,}).*\n$/.test(chunk.before)) {
        if (commandMgr.doCode) {
            commandMgr.doCode(chunk);
        }
    }
    
    if (fakeSelection) {
        chunk.after = chunk.selection + chunk.after;
        chunk.selection = "";
    }
};

Note that the /(\n|^)[ ]{0,3}([*+-]|\d+[.])[ \t]+.*\n$/ regex detects numbered lists with periods (1., 2.) etc. but not the new ones (1), 2)) we got with the CommonMark migration.

1
  • Cool! The fact that it does not work with 1), etc, suggests this is something older and undocumented to date. I thought for a sec that it could come with the CommonMark migration but it does not – fedorqui 'SO stop harming' Jul 2 '20 at 13:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .