Action

Toggle numbered lists

Posted by @ComplexPoint, Last update over 5 years ago

Toggle Markdown numbered line (Ordered List) prefixes
(of selected lines)
on and off

Steps

  • script

    (() => {
        'use strict';
    
        // Toggle list item numbering of selected lines on ⇄ off
        const main = () => {
            const
                e = editor,
                rgxOL = /^(\s*)\d+\. (.*)/,
                rgxOther = /^(\s*)(\S.*)/,
                [pres, selns, posts] = selectionTriagedLines(),
                dcts = map(
                    x => {
                        const
                            mOL = rgxOL.exec(x),
                            mOther = mOL ? null : rgxOther.exec(x),
                            blnOther = Boolean(mOther);
                        return mOL ? {
                            ol: true,
                            indent: mOL[1],
                            txt: mOL[2]
                        } : {
                            ol: false,
                            indent: blnOther ? mOther[1] : '',
                            txt: blnOther ? mOther[2] : ''
                        };
                    },
                    selns
                ),
                blnToOL = !dcts.some(dct => dct.ol),
                strPre = unlines(pres),
                strToggled = unlines(map(
                    blnToOL ? (
                        (x, i) => `${x.indent}${i + 1}. ${x.txt}`
                    ) : x => x.indent + x.txt,
                    dcts
                ));
    
            return (
                e.setText(
                    unlines(
                        [strPre, strToggled, unlines(posts)]
                        .filter(x => 0 < x.length)
                    )
                ),
                selns.length > 1 ? (
                    e.setSelectedRange(
                        1 + (strPre.length || -1),
                        strToggled.length
                    )
                ) : e.setSelectedRange(
                    1 + (strPre.length || -1) +
                    strToggled.length,
                    0
                )
            );
        };
    
        // GENERIC FUNCTIONS -------------------------
    
        // map :: (a -> b) -> [a] -> [b]
        const map = (f, xs) => xs.map(f);
    
        // showLog :: a -> Console String
        const showLog = (...args) =>
            alert(
                args.map(JSON.stringify)
                .join(' -> ')
            );
    
        // unlines :: [String] -> String
        const unlines = xs => xs.join('\n');
    
        // DRAFTS ---
    
        // selectionTriagedLines :: () ->
        //                 ([String],[String],[String])
        const selectionTriagedLines = () => {
            const
                e = editor,
                [from, lng] = e.getSelectedLineRange(),
                last = from + lng,
                clng = draft.content.length,
                blnLast = clng === last;
            return map(
                rng => rng.length > 0 ? (
                    e.getTextInRange(...rng).split('\n')
                ) : [], [
                    from > 0 ? [0, from - 1] : [],
                    [from, blnLast ? lng : lng - 1],
                    blnLast ? [] : [last, clng - last]
                ]
            );
        };
    
        // MAIN ...
        main();
    })();
    

Options

  • After Success Default
    Notification Error
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.