Skip to content

Commit

Permalink
support comments by converting into special tags
Browse files Browse the repository at this point in the history
we turn all the comments into <_comment> tags, and then just have a
converter that handles them. this way we avoid doing isCommentNode
checks all over the place.
  • Loading branch information
notslang committed May 13, 2016
1 parent 9a1edc5 commit e447800
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/converters.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ module.exports = [
surroundingBlankLines: true
replacement: (content) -> content
}
{
filter: '_comment'
replacement: (content) -> "<!-- #{content} -->"
}
{
filter: fallback
surroundingBlankLines: true
Expand Down
11 changes: 11 additions & 0 deletions lib/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ treeAdapter = require './tree-adapter'
{cleanText, decodeHtmlEntities, isBlock, isVoid} = require './utils'

{
createElement
detachNode
getCommentNodeContent
getTextNodeContent
insertBefore
insertText
isCommentNode
isElementNode
isTextNode
Expand Down Expand Up @@ -80,6 +83,13 @@ fixHeaders = (dom, ensureFirstHeaderIsH1) ->
i++
return

convertCommentNode = (node) ->
commentElement = createElement('_comment', null, [])
insertText(commentElement, getCommentNodeContent(node))
insertBefore(node.parent, commentElement, node)
detachNode(node)
return commentElement

###*
* Flattens DOM tree into single array
###
Expand All @@ -90,6 +100,7 @@ bfsOrder = (node) ->
elem = inqueue.shift()
outqueue.push elem
for child in elem.childNodes
if isCommentNode(child) then child = convertCommentNode(child)
if isElementNode(child) then inqueue.push child

outqueue.shift() # remove root node
Expand Down

0 comments on commit e447800

Please sign in to comment.