Last active
January 14, 2019 17:25
-
-
Save ducksoupdev/e39ef1d2d741db44b3e75d41fb015537 to your computer and use it in GitHub Desktop.
HTML to Emmet
This file contains hidden or 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
| const jsdom = require('jsdom') | |
| const { JSDOM } = jsdom | |
| function allDescendants (html, node) { | |
| for (let i = 0; i < node.children.length; i++) { | |
| const child = node.children[i] | |
| let s = '+(' | |
| if (i === 0) { | |
| s = '>' | |
| } | |
| html.push(s) | |
| html.push(`${child.tagName.toLowerCase()}`) | |
| allAttributes(html, child) | |
| if (child.children && child.children.length > 0) { | |
| allDescendants(html, child) | |
| } | |
| if (s === '+(') { | |
| html.push(')') | |
| } | |
| } | |
| } | |
| function allAttributes (html, node) { | |
| if (node && node.attributes && node.attributes.length > 0) { | |
| const attrs = node.attributes | |
| const na = [] | |
| for (let i = attrs.length - 1; i >= 0; i--) { | |
| na.push(`${attrs[i].name}="${attrs[i].value}"`) | |
| } | |
| html.push(`[${na.join(' ')}]`) | |
| } | |
| } | |
| function createEmmet (snippet) { | |
| const html = [] | |
| const dom = new JSDOM(snippet) | |
| allDescendants(html, dom.window.document.body) | |
| return html.slice(1).join('') | |
| } | |
| console.log(createEmmet(`<mosaic-breadcrumb> | |
| <ol> | |
| <li><a href="about:blank">Home</a></li> | |
| <li><a href="about:blank">Library</a></li> | |
| <li><a href="about:blank">Contents</a></li> | |
| <li><a href="about:blank">Technology</a></li> | |
| <li active>Data</li> | |
| </ol> | |
| </mosaic-breadcrumb>`)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment