/**
 * @desc amWiki 工作端·提取H2-H3为页内目录模块
 * @author Tevin
 */

module.exports = {
    //创建
    make: function () {
        var editor = atom.workspace.getActiveTextEditor();
        if (!editor) {
            return;
        }
        var grammar = editor.getGrammar();
        if (!grammar) {
            return;
        }
        if (grammar.scopeName !== 'source.gfm' && grammar.scopeName !== 'text.md') {
            return;
        }
        editor.insertText(this.extract(editor.getText()) || '');
    },
    //抽取标题
    extract: function (arc) {
        var lines = arc.split('\n');
        if (lines.length == 1) {
            lines = lines[0].split('\r');
        }
        var contents = '';
        var lineStr = '';
        var hashStr = '';
        var h1 = '';
        for (var i = 0; i < lines.length; i++) {
            lines[i] = lines[i].replace(/^\s*/, '');
            switch (lines[i].split(/\s/)[0]) {
                case '#':
                    var text = lines[i].split('# ')[1];
                    if (text != undefined) {
                        lineStr = text.replace(/^\s+|\s+$/g, ''); //去除首位空格
                        h1 = h1 == '' ? lineStr.replace(/\[(.*?)\]\(.*?\)/g, '$1') : h1;
                    }
                    break;
                case '##':
                    var text = lines[i].split('## ')[1];
                    if (text != undefined) {
                        lineStr = text.replace(/^\s+|\s+$/g, '');
                        //去除链接
                        lineStr = lineStr.replace(/\[(.*?)\]\(.*?\)/g, '$1');
                        //去除所有空格
                        hashStr = lineStr.replace(/\s+/g, '');
                        //添加内容
                        contents += '1. [' + lineStr + '](#' + hashStr + ' "' + lineStr + '")\n';
                    }
                    break;
                case '###':
                    var text = lines[i].split('### ')[1];
                    if (text != undefined) {
                        lineStr = text.replace(/^\s+|\s+$/g, '');
                        lineStr = lineStr.replace(/\[(.*?)\]\(.*?\)/g, '$1');
                        hashStr = lineStr.replace(/\s+/g, '');
                        contents += '\t1. [' + lineStr + '](#' + hashStr + ' "' + lineStr + '")\n';
                    }
                    break;
            }
        }
        if (contents.substr(0, 2) != '1.') {
            contents = '1. ' + h1 + '\n' + contents;
        }
        contents = '>' + contents;
        return contents;
    }

};

@@ -1453,39 +1453,8 @@
 ');%0A
-                        //%E5%8E%BB%E9%99%A4%E9%93%BE%E6%8E%A5%0A
     
@@ -1530,135 +1530,15 @@
 1');
-%0A                        //%E5%8E%BB%E9%99%A4%E6%89%80%E6%9C%89%E7%A9%BA%E6%A0%BC%0A                        hashStr = lineStr.replace(/%5Cs+/g, '');%0A                        //%E6%B7%BB%E5%8A%A0%E5%86%85%E5%AE%B9
+ //%E5%8E%BB%E9%99%A4%E9%93%BE%E6%8E%A5
 %0A   
@@ -1589,36 +1589,36 @@
 neStr + '%5D(#' + 
-hash
+line
 Str + ' %22' + lin
@@ -1960,71 +1960,8 @@
 ');%0A
-                        hashStr = lineStr.replace(/%5Cs+/g, '');%0A
     
@@ -2021,20 +2021,20 @@
 '%5D(#' + 
-hash
+line
 Str + ' 

fix #15