Sublime-Markdown Preview-添加首行缩进

为Sublime的插件Markdown Preview生成的html文件添加首行缩进

// ==UserScript==
// @name         Sublime-Markdown Preview-添加首行缩进
// @name:zh      Sublime-Markdown Preview-添加首行缩进
// @name:en      Sublime-Markdown Preview-Add First Line Indentation
// @name:ja      Sublime-Markdown Preview-最初の行のインデントを追加します
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @description  为Sublime的插件Markdown Preview生成的html文件添加首行缩进
// @description:zh  为Sublime的插件Markdown Preview生成的html文件添加首行缩进
// @description:en  Adds first line indentation to the html files generated by the Markdown Preview plugin for Sublime.
// @description:ja  SublimeのMarkdown Previewプラグインで生成されたhtmlファイルに先頭行のインデントを追加します。
// @author       aotmd
// @match        file:///C:/Users/*/appdata/local/temp/*.html
// @noframes
// @license MIT
// @run-at document-body
// @grant        none
// ==/UserScript==

( function() {
    function add(){
        var brElements = document.querySelectorAll( "body .markdown-body br" );
        brElements.forEach( function( br ) {
            deleteRedundantEmptyTextNodes( br.parentNode);
            var nextElement = br.nextSibling;
            if ( nextElement ) {
                if (nextElement.className && nextElement.className.includes("highlight")) {return;}
                if (nextElement.nodeName && nextElement.nodeName==='P'){return;}
                if( nextElement.innerHTML === "  "){return;}
                var span = document.createElement( "span" );
                span.innerHTML = "  "; /* 缩进2字符*/
                span.style.userSelect = "none"; /* 被复制时不被选中*/
                nextElement.parentNode.insertBefore( span, nextElement );
            }
        }, false );
        brElements = document.querySelectorAll( "body .markdown-body .highlight" );
        brElements.forEach( function( br ) {
            deleteRedundantEmptyTextNodes( br.parentNode);
            var nextElement = br.nextSibling;
            if ( nextElement ) {
                if (nextElement.nodeName && nextElement.nodeName==='P'){return;}
                if( nextElement.innerHTML === "  "){return;}
                var span = document.createElement( "span" );
                span.innerHTML = "  "; /* 缩进2字符*/
                span.style.userSelect = "none"; /* 被复制时不被选中*/
                nextElement.parentNode.insertBefore( span, nextElement );
            }
        }, false );
    }
    add();
    document.addEventListener("DOMContentLoaded", function () {add();});


    addStyle( `
        body .markdown-body p {
            text-indent: 2em;
        }
        ` );
    /**
     * 删除多余的空文本节点,为nextSibling,等节点操作一致性做准备
     * @param elem 要优化的父节点
     */
    function deleteRedundantEmptyTextNodes( elem ) {
        let elemList = elem.childNodes;
        for ( let i = 0; i < elemList.length; i++ ) {
            /*当为文本节点并且为不可见字符时删除节点*/
            if ( elemList[ i ].nodeName === "#text" && /^\s+$/.test( elemList[ i ].nodeValue ) ) {
                elem.removeChild( elemList[ i ] )
            }else if( elemList[ i ].nodeName === "P" &&(elemList[ i ].textContent==='' || /^\s+$/.test( elemList[ i ].textContent ))){
            /*当为P节点并且为不可见字符时删除节点*/
                elem.removeChild( elemList[ i ] )
            }
        }
    }
    /**
     * 添加浏览器执行事件
     * @param func 无参匿名函数
     */
    function addLoadEvent( func ) {
        let oldOnload = window.onload;
        if ( typeof window.onload != 'function' ) {
            window.onload = func;
        } else {
            window.onload = function() {
                try {
                    oldOnload();
                } catch ( e ) {
                    console.log( e );
                } finally {
                    func();
                }
            }
        }
    }
    //添加css样式
    function addStyle( rules ) {
        let styleElement = document.createElement( 'style' );
        styleElement[ "type" ] = 'text/css';
        document.getElementsByTagName( 'head' )[ 0 ].appendChild( styleElement );
        styleElement.appendChild( document.createTextNode( rules ) );
    }
} )();