Last active
January 14, 2016 04:11
-
-
Save kamimoo/a18365b1a22777647fd3 to your computer and use it in GitHub Desktop.
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
htmlParser = require('htmlparser2') | |
result = '' | |
selfClosingTags = ['img', 'br', 'hr'] | |
parser = new htmlParser.Parser( | |
onopentag: (name, attribs) => | |
result += "<#{name}" | |
for attr, val of attribs | |
if val.length | |
result += "=\"#{val}\"" | |
if selfClosingTags.indexOf(name) != -1 | |
result += ' />' | |
else | |
result += '>' | |
ontext: (text) => | |
result += text.replace(/(#\w+)/, '<span class="entry-hashtag">$1</span>') | |
onclosetag: (name) => | |
if selfClosingTags.indexOf(name) != -1 | |
result += "</#{name}>" | |
) | |
parser.write("<div>\n<font size=\"10\" data-key>これはテストです。 #hash</font>\n<br /></div>") | |
parser.end() | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment