Skip to content

Instantly share code, notes, and snippets.

@kamimoo
Last active January 14, 2016 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamimoo/a18365b1a22777647fd3 to your computer and use it in GitHub Desktop.
Save kamimoo/a18365b1a22777647fd3 to your computer and use it in GitHub Desktop.
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