Files: a6da62055a73bcc0ca4d0d94052c6cef7193d73e / index.js
1056 bytesRaw
1 | var toMarkdown = require('to-markdown') |
2 | |
3 | function debounce(fn, wait) { |
4 | var timeout |
5 | return function() { |
6 | clearTimeout(timeout) |
7 | timeout = setTimeout(fn, wait) |
8 | } |
9 | } |
10 | |
11 | var value |
12 | var inputEl = document.getElementById('input') |
13 | var outputEl = document.getElementById('output') |
14 | var outputText = outputEl.appendChild(document.createTextNode('')) |
15 | var opt = { |
16 | gfm: true, |
17 | converters: [{ |
18 | filter: ['span', 'div', 'small', 'font'], |
19 | replacement: function (content) { |
20 | return content |
21 | } |
22 | }, |
23 | { |
24 | filter: 'li', |
25 | replacement: function (content, node) { |
26 | content = content.replace(/^\s+/, '').replace(/\n/gm, '\n ') |
27 | var parent = node.parentNode |
28 | var index = Array.prototype.indexOf.call(parent.children, node) + 1 |
29 | |
30 | var prefix = /ol/i.test(parent.nodeName) ? index + '. ' : '- ' |
31 | return prefix + content |
32 | } |
33 | }] |
34 | } |
35 | |
36 | inputEl.onkeydown = inputEl.onkeyup = debounce(function() { |
37 | var newVal = inputEl.innerHTML |
38 | if (value == newVal) return |
39 | value = newVal |
40 | outputText.nodeValue = toMarkdown(value, opt) |
41 | }) |
42 |
Built with git-ssb-web