MCPcopy
hub / github.com/SparkPost/heml / parse

Function parse

packages/heml-parse/src/index.js:9–81  ·  view source on GitHub ↗
(contents, options = {})

Source from the content-addressed store, hash-verified

7const wrappingHtmlTags = difference(htmlTags, selfClosingHtmlTags)
8
9function parse (contents, options = {}) {
10 const {
11 elements = [],
12 cheerio: cheerioOptions = {}
13 } = options
14
15 const $ = load(contents, {
16 xmlMode: true,
17 lowerCaseTags: true,
18 decodeEntities: false,
19 ...cheerioOptions
20 })
21
22 $.findNodes = function (q) {
23 return $(Array.isArray(q) ? q.join(',') : q)
24 .not('[heml-ignore]')
25 .toNodes()
26 }
27
28 $.prototype.toNodes = function () {
29 return this
30 .toArray()
31 .map((node) => $(node))
32 }
33
34 const selfClosingTags = [
35 ...selfClosingHtmlTags,
36 ...elements.filter((element) => element.children === false).map(({ tagName }) => tagName) ]
37 const wrappingTags = [
38 ...wrappingHtmlTags,
39 ...elements.filter((element) => element.children !== false).map(({ tagName }) => tagName) ]
40
41 const $selfClosingNodes = $.findNodes(selfClosingTags).reverse()
42 const $wrappingNodes = $.findNodes(wrappingTags).reverse()
43
44 /** Move contents from self wrapping tags outside of itself */
45 $selfClosingNodes.forEach(($node) => {
46 $node.after($node.html())
47 $node.html('')
48 })
49
50 /** ensure that all wrapping tags have at least a zero-width, non-joining character */
51 $wrappingNodes.forEach(($node) => {
52 if ($node.html().length === 0) {
53 $node.html(' ')
54 }
55 })
56
57 /** try for head, fallback to body, then heml */
58 const $head = first(compact([...$('head').toNodes(), ...$('body').toNodes(), ...$('heml').toNodes()]))
59
60 /** move inline styles to a style tag with unique ids so they can be hit by the css processor */
61 if ($head) {
62 const $inlineStyleNodes = $.findNodes(elements.map(({ tagName }) => tagName)).filter($node => !!$node.attr('style'))
63
64 const inlineCSS = $inlineStyleNodes.map(($node) => {
65 let id = $node.attr('id')
66 const css = $node.attr('style')

Callers 1

hemlFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected