How to use the cheerio.parseHTML function in cheerio

To help you get started, we’ve selected a few cheerio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github algolia / react-instantsearch / functional-tests / utils.js View on Github external
return refinementsArray.map(refinementAsHTML => {
    // element is (simplified) <div>facetName <span>facetCount</span></div>
    const element = cheerio.parseHTML(refinementAsHTML)[0];

    // <div>**facetName** <span>facetCount</span></div>
    const name = element.firstChild.nodeValue.trim();

    // some widgets have no counts (pricesRanges)
    // <div>facetName <span>**facetCount**</span></div>
    const maybeCount = element.firstChild.nextSibling.children;

    return {
      name,

      count: maybeCount &amp;&amp; maybeCount[0] &amp;&amp; maybeCount[0].nodeValue !== undefined ?
        parseInt(maybeCount[0].nodeValue, 10) :
        'n/a'
    };
  });
github MarkBind / markbind / src / lib / markbind / src / parser.js View on Github external
if (isOptional) {
        // optional includes of segments have now been handled, so delete the attribute
        delete element.attribs.optional;
      }

      if (isIncludeSrcMd) {
        if (context.mode === 'include') {
          actualContent = isInline ? actualContent : utils.wrapContent(actualContent, '\n\n', '\n');
        } else {
          actualContent = md.render(actualContent);
        }
        actualContent = self._rebaseReferenceForStaticIncludes(actualContent, element, config);
      }
      const wrapperType = isInline ? 'span' : 'div';
      element.children = cheerio.parseHTML(
        `&lt;${wrapperType} data-included-from="${filePath}"&gt;${actualContent}`,
        true,
      );
    } else {
      if (isImport) {
        // eslint-disable-next-line no-console
        console.warn(`Missing hash for import variable ${includeSrc.pathname}`
                     + ` in ${element.attribs[ATTRIB_CWF]}.`);
        return createEmptyNode();
      }
      let actualContent = (fileContent &amp;&amp; isTrim) ? fileContent.trim() : fileContent;
      if (isIncludeSrcMd) {
        if (context.mode === 'include') {
          actualContent = isInline ? actualContent : utils.wrapContent(actualContent, '\n\n', '\n');
        } else {
          actualContent = md.render(actualContent);
github MarkBind / markbind / src / lib / markbind / src / parser.js View on Github external
const wrapperType = isInline ? 'span' : 'div';
      element.children = cheerio.parseHTML(
        `&lt;${wrapperType} data-included-from="${filePath}"&gt;${actualContent}`,
        true,
      );
    } else {
      let actualContent = (fileContent &amp;&amp; isTrim) ? fileContent.trim() : fileContent;
      if (isIncludeSrcMd) {
        if (context.mode === 'include') {
          actualContent = isInline ? actualContent : utils.wrapContent(actualContent, '\n\n', '\n');
        } else {
          actualContent = md.render(actualContent);
        }
      }
      const wrapperType = isInline ? 'span' : 'div';
      element.children = cheerio.parseHTML(
        `&lt;${wrapperType} data-included-from="${filePath}"&gt;${actualContent}`,
        true,
      );
    }

    if (element.children &amp;&amp; element.children.length &gt; 0) {
      if (childContext.callStack.length &gt; CyclicReferenceError.MAX_RECURSIVE_DEPTH) {
        const error = new CyclicReferenceError(childContext.callStack);
        this._onError(error);
        return createErrorNode(element, error);
      }
      element.children = element.children.map(e =&gt; self._preprocess(e, childContext, config));
    }
  } else if ((element.name === 'panel') &amp;&amp; hasSrc) {
    if (!isUrl &amp;&amp; includeSrc.hash) {
      element.attribs.fragment = includeSrc.hash.substring(1); // save hash to fragment attribute
github MarkBind / markbind / lib / markbind / lib / parser.js View on Github external
} else {
          actualContent = md.render(actualContent);
        }
        actualContent = self._rebaseReferenceForStaticIncludes(actualContent, element, config);
      }
      element.children = cheerio.parseHTML(actualContent, true); // the needed content;
    } else {
      let actualContent = fileContent;
      if (isIncludeSrcMd) {
        if (context.mode === 'include') {
          actualContent = isInline ? actualContent : utils.wrapContent(actualContent, '\n\n', '\n');
        } else {
          actualContent = md.render(actualContent);
        }
      }
      element.children = cheerio.parseHTML(actualContent, true);
    }

    // The element's children are in the new context
    // Process with new context
    const childContext = _.cloneDeep(context);
    childContext.cwf = filePath;
    childContext.source = isIncludeSrcMd ? 'md' : 'html';
    if (element.children && element.children.length > 0) {
      element.children = element.children.map(e => self._preprocess(e, childContext, config));
    }
  } else if (element.name === 'dynamic-panel') {
    if (!isUrl && includeSrc.hash) {
      element.attribs.fragment = includeSrc.hash.substring(1); // save hash to fragment attribute
    }
    element.attribs.src = filePath;
    this.dynamicIncludeSrc.push({ from: context.cwf, to: actualFilePath, asIfTo: filePath });
github MarkBind / markbind / src / lib / markbind / src / parser.js View on Github external
}
  if (element.name) {
    element.name = element.name.toLowerCase();
  }

  switch (element.name) {
  case 'md':
    element.name = 'span';
    cheerio.prototype.options.xmlMode = false;
    element.children = cheerio.parseHTML(md.renderInline(cheerio.html(element.children)), true);
    cheerio.prototype.options.xmlMode = true;
    break;
  case 'markdown':
    element.name = 'div';
    cheerio.prototype.options.xmlMode = false;
    element.children = cheerio.parseHTML(md.render(cheerio.html(element.children)), true);
    cheerio.prototype.options.xmlMode = true;
    break;
  case 'panel': {
    if (!_.hasIn(element.attribs, 'src')) { // dynamic panel
      break;
    }
    const fileExists = utils.fileExists(element.attribs.src)
                    || utils.fileExists(calculateBoilerplateFilePath(element.attribs.boilerplate,
                                                                     element.attribs.src, config));
    if (fileExists) {
      const { src, fragment } = element.attribs;
      const resultDir = path.dirname(path.join('{{hostBaseUrl}}', path.relative(config.rootPath, src)));
      const resultPath = path.join(resultDir, utils.setExtension(path.basename(src), '._include_.html'));
      element.attribs.src = utils.ensurePosix(fragment ? `${resultPath}#${fragment}` : resultPath);
    }
    delete element.attribs.boilerplate;
github MarkBind / markbind / src / lib / markbind / src / parser.js View on Github external
}
  if (element.name) {
    element.name = element.name.toLowerCase();
  }

  switch (element.name) {
  case 'md':
    element.name = 'span';
    cheerio.prototype.options.xmlMode = false;
    element.children = cheerio.parseHTML(md.renderInline(cheerio.html(element.children)), true);
    cheerio.prototype.options.xmlMode = true;
    break;
  case 'markdown':
    element.name = 'div';
    cheerio.prototype.options.xmlMode = false;
    element.children = cheerio.parseHTML(md.render(cheerio.html(element.children)), true);
    cheerio.prototype.options.xmlMode = true;
    break;
  case 'panel': {
    if (!_.hasIn(element.attribs, 'src')) { // dynamic panel
      break;
    }
    const fileExists = utils.fileExists(element.attribs.src)
                    || utils.fileExists(calculateBoilerplateFilePath(element.attribs.boilerplate,
                                                                     element.attribs.src, config));
    if (fileExists) {
      const { src, fragment } = element.attribs;
      const resultDir = path.dirname(path.join('{{hostBaseUrl}}', path.relative(config.rootPath, src)));
      const resultPath = path.join(resultDir, utils.setExtension(path.basename(src), '._include_.html'));
      element.attribs.src = utils.ensurePosix(fragment ? `${resultPath}#${fragment}` : resultPath);
    }
    delete element.attribs.boilerplate;
github MarkBind / markbind / lib / markbind / lib / parser.js View on Github external
}
  if (element.name) {
    element.name = element.name.toLowerCase();
  }

  switch (element.name) {
  case 'md':
    element.name = 'span';
    cheerio.prototype.options.xmlMode = false;
    element.children = cheerio.parseHTML(md.renderInline(cheerio.html(element.children)), true);
    cheerio.prototype.options.xmlMode = true;
    break;
  case 'markdown':
    element.name = 'div';
    cheerio.prototype.options.xmlMode = false;
    element.children = cheerio.parseHTML(md.render(cheerio.html(element.children)), true);
    cheerio.prototype.options.xmlMode = true;
    break;
  case 'dynamic-panel': {
    const hasIsOpen = _.hasIn(element.attribs, 'isOpen');
    element.attribs.isOpen = hasIsOpen || false;
    const fileExists = utils.fileExists(element.attribs.src)
                    || utils.fileExists(calculateBoilerplateFilePath(element.attribs.boilerplate,
                                                                     element.attribs.src, config));
    if (fileExists) {
      const { src, fragment } = element.attribs;
      const resultDir = path.dirname(path.join('{{hostBaseUrl}}', path.relative(process.cwd(), src)));
      const resultPath = path.join(resultDir, utils.setExtension(path.basename(src), '._include_.html'));
      element.attribs.src = fragment ? `${resultPath}#${fragment}` : resultPath;
    }
    element.attribs['no-close'] = 'true';
    element.attribs['no-switch'] = 'true';
github MarkBind / markbind / src / lib / markbind / src / parser.js View on Github external
function createErrorNode(element, error) {
  const errorElement = cheerio.parseHTML(utils.createErrorElement(error), true)[0];
  return Object.assign(element, _.pick(errorElement, ['name', 'attribs', 'children']));
}
github MarkBind / markbind / src / lib / markbind / src / parser.js View on Github external
function createErrorNode(element, error) {
  const errorElement = cheerio.parseHTML(utils.createErrorElement(error), true)[0];
  return Object.assign(element, _.pick(errorElement, ['name', 'attribs', 'children']));
}