1
0
Fork 0
mirror of https://gitlab.com/Oreolek/salet.git synced 2024-07-04 07:45:03 +03:00
salet/lib/markdown.coffee

40 lines
920 B
CoffeeScript
Raw Normal View History

2016-01-15 03:06:03 +02:00
###
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 == ""
2016-01-16 19:24:02 +02:00
return ""
2016-01-15 03:06:03 +02:00
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
2016-01-15 03:06:03 +02:00
return lines.map((l) ->
return l.replace(new RegExp('^' + smallestIndent), '')
).join('\n')
markdown = (text) ->
2016-01-16 19:24:02 +02:00
unless text?
return ""
2016-01-15 03:06:03 +02:00
if typeof text is Function
text = text()
2016-05-01 13:42:14 +03:00
text = text.toString()
2016-01-15 03:06:03 +02:00
return marked(normaliseTabs(text), {
smartypants: true
})
module.exports = markdown