1
0
Fork 0
mirror of https://gitlab.com/Oreolek/black_phone.git synced 2024-05-05 18:48:37 +03:00
black_phone/lib/markdown.coffee

37 lines
811 B
CoffeeScript

###
Indent normalization. Removes tabs AND spaces from every line beginning.
Implies that you don't mix up your tabs and spaces.
Copyright 2015 Bruno Dias
###
normaliseTabs = (text) ->
unless text?
return ""
lines = text.split('\n');
indents = lines
.filter((l) => l != '')
.map((l) => l.match(/^\s+/))
.map((m) ->
if (m == null)
return ''
return m[0]
)
smallestIndent = indents.reduce((max, curr) ->
if (curr.length < max.length)
return curr
return max
)
return lines.map((l) ->
return l.replace(new RegExp('^' + smallestIndent), '')
).join('\n')
markdown = (text) ->
unless text?
return ""
if typeof text is Function
text = text()
return marked(normaliseTabs(text), {
smartypants: true
})
module.exports = markdown