1
0
Fork 0
mirror of https://gitlab.com/Oreolek/salet.git synced 2024-07-02 06:45:06 +03:00
salet/lib/markdown.coffee
Alexander Yakovlev 74a7600181 Markdown indentation fix
It was bugging on some weird cases.
2016-05-09 10:27:51 +07:00

40 lines
920 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) ->
if not text? or typeof(text) != "string" or 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
)
if smallestIndent == ""
return text
return lines.map((l) ->
return l.replace(new RegExp('^' + smallestIndent), '')
).join('\n')
markdown = (text) ->
unless text?
return ""
if typeof text is Function
text = text()
text = text.toString()
return marked(normaliseTabs(text), {
smartypants: true
})
module.exports = markdown