Ldc
2024-04-07 0652600959e5e3b5796fb6e8da129704ca95347a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>markdown编辑器</title>
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.8.2/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/marked/2.1.3/marked.js"></script>
    <link href="http://cdn.bootcss.com/highlight.js/8.0/styles/monokai_sublime.min.css" rel="stylesheet">
    <script src="http://cdn.bootcss.com/highlight.js/8.0/highlight.min.js"></script>
 
    <style type="text/css">
        html, body {
            margin: 0;
            padding: 0;
            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
            color: #333;
            background-color: #fbfbfb;
            height: 100%;
        }
        textarea {
            font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
            font-size: 12px;
            resize: none;
        }
        header {
            padding-top: 10px;
            display: flex;
            height: 58px;
        }
        header h1 {
            margin: 0;
        }
        .containers {
            display: flex;
            height: calc(100vh - 10px);
        }
        .container {
            flex-basis: 50%;
            padding: 5px;
            display: flex;
            flex-direction: column;
            height: 100%;
            box-sizing: border-box;
        }
        .pane, .inputPane {
            margin-top: 5px;
            padding: 0.6em;
            border: 1px solid #ccc;
            overflow: auto;
            flex-grow: 1;
            flex-shrink: 1;
        }
    </style>
</head>
 
<body>
    <div class="containers">
        <div class="container">
            <input type="button" id="clear" value="清除" style="width: 100px;height: 25px"/>
            <textarea  id="markedSource" class="inputPane"></textarea>
        </div>
        <div class="container">
            <select id="outputType" style="width: 150px;height: 25px">
                <option value="markedResult">markedResult</option>
                <option value="html">HTML Source</option>
                <option value="lexer">Lexer Data</option>
            </select>
            <div id="markedResult" class="pane"></div>
            <textarea id="html" class="pane" readonly="readonly" style="display: none;"></textarea>
            <textarea id="lexer" class="pane" readonly="readonly" style="display: none;"></textarea>
            <textarea id="quickref" class="pane" readonly="readonly" style="display: none;"></textarea>
        </div>
    </div>
 
 
    <script>
        var rendererMD = new marked.Renderer();
        marked.setOptions({
            renderer: rendererMD,
            gfm: true,
            tables: true,
            breaks: false,//允许回车换行。该选项要求 gfm 为true
            pedantic: false,
            sanitize: false,//不输出HTML
            smartLists: true,
            smartypants: false,
            highlight: function (code) {
                return hljs.highlightAuto(code).value;
            }
        });
 
        // 监听输入值变化
        $('#markedSource').on('keyup', (cm) => {
            document.getElementById('markedResult').innerHTML = marked(cm.currentTarget.value);
 
            var lexed = marked.lexer(cm.currentTarget.value);
            var lexedList = jsonString(lexed);
            var parsed = marked.parser(lexed);
            document.getElementById('lexer').value=lexedList
            document.getElementById('html').value=parsed
        })
 
        $('#clear').on('click',function (e){
            document.getElementById('markedSource').value = '';
            document.getElementById('markedResult').innerHTML= '';
        })
        $('#outputType').on('change',function (e){
            for(var i = 0; i< $('.pane').length; i++) {
                if ($('.pane')[i].id === e.currentTarget.value) {
                    $('.pane')[i].style.display = '';
                    active = $('.pane')[i];
                } else {
                    $('.pane')[i].style.display = 'none';
                }
            }
        })
 
        function stringRepeat(char, times) {
            var s = '';
            for (var i = 0; i < times; i++) {
                s += char;
            }
            return s;
        }
        function jsonString(input, level) {
            level = level || 0;
            if (Array.isArray(input)) {
                if (input.length === 0) {
                    return '[]';
                }
                var items = [],
                    i;
                if (!Array.isArray(input[0]) && typeof input[0] === 'object' && input[0] !== null) {
                    for (i = 0; i < input.length; i++) {
                        items.push(stringRepeat(' ', 2 * level) + jsonString(input[i], level + 1));
                    }
                    return '[\n' + items.join('\n') + '\n]';
                }
                for (i = 0; i < input.length; i++) {
                    items.push(jsonString(input[i], level));
                }
                return '[' + items.join(', ') + ']';
            } else if (typeof input === 'object' && input !== null) {
                var props = [];
                for (var prop in input) {
                    props.push(prop + ':' + jsonString(input[prop], level));
                }
                return '{' + props.join(', ') + '}';
            } else {
                return JSON.stringify(input);
            }
        }
 
    </script>
</body>
</html>