Last active
August 29, 2015 13:58
-
-
Save rymizuki/9928349 to your computer and use it in GitHub Desktop.
convert markdown to hatena on coffee-script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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