Skip to content

Instantly share code, notes, and snippets.

@rymizuki
Last active August 29, 2015 13:58
Show Gist options
  • Save rymizuki/9928349 to your computer and use it in GitHub Desktop.
Save rymizuki/9928349 to your computer and use it in GitHub Desktop.
convert markdown to hatena on coffee-script
#!/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 md2hatena content.join ""
process.exit()
md2hatena = (text) ->
# table none
text = text.replace /((\|[:-]+?)+\|\n)/mg, (match) ->
""
# pre none
text = text.replace /^```(\w*)\n([\s\S]*?)\n```$/mg, (match, lang, content) ->
">|#{ lang }|\n#{ content }\n||<\n"
# blockquote
text = text.replace /^> ([\s\S]*?)\n\n/mg, (match, content) ->
">>\n#{ content }\n<<\n"
# 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 ''}+"
# link
text = text.replace /\[(.*?)\]\((https?:\/\/.*?)\)/mg, (match, label, href) ->
"[#{ href }:title=#{ label }]"
# headers
text = text.replace /^([\#]{1,4})/mg, (match) -> match.replace(/#/g, '*')
text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment