Skip to content

Instantly share code, notes, and snippets.

@cancer
Last active August 29, 2015 14:05
Show Gist options
  • Save cancer/ee5ac31b58f1e5eaaf92 to your computer and use it in GitHub Desktop.
Save cancer/ee5ac31b58f1e5eaaf92 to your computer and use it in GitHub Desktop.
Translate markdown to jira notation. `pbpaste |coffee /usr/bin/md2jira.coffee |pbcopy`
#!/usr/bin/env node
content = []
process.stdin.setEncoding "utf8"
process.stdin.resume()
process.stdin.on "data", (chunk) ->
content.push chunk
process.stdin.on "end", () ->
process.stdout.write md2jira content.join ""
process.exit()
md2jira = (text) ->
# table none
text = text.replace /((\|[:-]+?)+\|\n)/mg, (match) ->
""
# pre none
text = text.replace /^```(\w*)(?::(\w*\.\w*))?([\s\S]*?)```$/mg, (match, lang, file, content) ->
if file
"{code:title=#{ file }}\n#{ content }\n{code}"
else if lang
"{code:title=#{ lang }}\n#{ content }\n{code}"
else
"{noformat}#{ content }{noformat}"
# blockquote
text = text.replace /^> ([\s\S]*?)\n\n/mg,
"{quote}#{ content }{quote}"
# list
text = text.replace /^((?:[ ]{2})+)\*/mg, (match, content) ->
"#{ content.replace /[ ]{2}/g, '*' }-"
text = text.replace /^([ ]{2})?(?:\*)/mg, (match, content) ->
"#{ if content? then content.repalce /[ ]{2}/g, '*' else ''}-"
text = text.replace /^((?:[ ]{2})+)\-/mg, (match, content) ->
"#{ content.repalce /[ ]{2}/g, '-' }-"
text = text.replace /^([ ]{2})?(?:[0-9]+\.)/mg, (match, content) ->
"#{ if content then content.repalce /[ ]{2}/g, '+' else ''}+"
text = text.replace /^ -/mg, "--"
# strong
text = text.replace /\*{2}(?![ ])(.*?)(?![ ])\*{2}/g, "*$1*"
text = text.replace /-\*(?![ ])(.*?)(?![ ])\*{2}/g, "*$1*"
# link
text = text.replace /\[(.*?)\]\((https?:\/\/.*?)\)/mg, (match, label, href) ->
"[#{ label }|#{ href }]"
# headers
text = text.replace /^([\#]{1,4})/mg, (match, headers) ->
"h#{headers.length}. "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment