Send Msg

Javascript code posted by Gabrielle
created at 19 Apr 10:50, updated at 20 Apr 11:28

Edit | Back
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
function parseMarkdown (str) {
  var arr = str.split(' ')
  var symbols = arr[0].split('')
  var result = null

  if (symbols[0] === '#') {

    var count = 0
    symbols.forEach(function (el) {
      if (el === '#') count += 1
    })

    if (count > 6) count = 6

    result = document.createElement('h' + count)
    arr.shift()
    result.textContent = arr.join(' ')
  } else {
    result = document.createElement('p')
    result.textContent = arr.join(' ')
  }

  if (symbols[0] === '-' || symbols[0] === '*') {
    result = document.createElement('ul')
    li = document.createElement('li')
    arr.shift()
    li.textContent = arr.join(' ')
    result.appendChild(li)
  }

  document.querySelector('#result').appendChild(result)
}

parseMarkdown('* Hello World')
801 Bytes in 3 ms with coderay