neft
Version:
Universal Platform
83 lines (66 loc) • 2.08 kB
text/coffeescript
'use strict'
escapeHtml = require 'escape-html'
exports.Heading = class Heading
constructor: ( = 0, = '', = '') ->
getLevel: ->
prefix = /^([#]+)/.exec
if prefix?
return prefix[1].length
else if /^=/m.test()
return 1
else if /^-/m.test()
return 2
return -1
getHeadingText: ->
escapeHtml /^(?:[#]*)(.*)/.exec()?[1]?.trim?() or ''
setHeadingText: (val) ->
= /^([#]*)/.exec()?[1] + val
exports.Paragraph = class Paragraph
constructor: ( = 0, = '') ->
exports.ProgramCode = class ProgramCode
constructor: ( = 0, = '') ->
exports.parse = (str) ->
file = []
lines = str.split '\n'
lines.push ''
lastType = null
pushedType = null
for line, i in lines
# break
if line.trim() is ''
if lastType
pushedType = lastType
file.push lastType
lastType = null
# heading
else if /^#+/.test(line) or /^[\-=]+$/.test(lines[i + 1].trim())
lastType ?= new Heading i
# program code
else if /^\s{4}|^\t/.test(line)
lastType ?= new ProgramCode i
# paragraph
else
lastType ?= new Paragraph i
# join the same types
if pushedType and lastType
if pushedType.__proto__ is lastType.__proto__ and not (lastType instanceof Heading)
file.pop()
pushedType.text += "#{lastType.text}\n"
lastType = pushedType
pushedType = null
# insert line into type
if lastType
lastType.text += "#{line}\n"
file
exports.stringify = (file) ->
md = ''
for type in file
md += "#{type.text}\n"
md
exports.headingToUrl = (text) ->
text = text.replace /^#*/, ''
text = text.trim()
text = text.toLowerCase()
text = text.replace /[^a-z0-9 ]/g, ''
text = text.replace /\ /g, '-'
"##{text}"