You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const cheerio = require('cheerio');
  2. const pick = require('pick-util');
  3. function replaceCodeBlock(html, re, block) {
  4. return html.replace(re, () => block);
  5. }
  6. module.exports = (html, options) => {
  7. const results = {};
  8. const codeBlocks = {
  9. EJS: { start: '<%', end: '%>' },
  10. HBS: { start: '{{', end: '}}' }
  11. };
  12. const codeBlockLookup = [];
  13. const encodeCodeBlocks = _html => {
  14. let __html = _html;
  15. const blocks = Object.assign(codeBlocks, options.codeBlocks);
  16. Object.keys(blocks).forEach(key => {
  17. const re = new RegExp(blocks[key].start + '([\\S\\s]*?)' + blocks[key].end, 'g');
  18. __html = __html.replace(re, match => {
  19. codeBlockLookup.push(match);
  20. return 'EXCS_CODE_BLOCK_' + (codeBlockLookup.length - 1) + '_';
  21. });
  22. });
  23. return __html;
  24. };
  25. const decodeCodeBlocks = _html => {
  26. let index;
  27. let re;
  28. let __html = _html;
  29. for (index = 0; index < codeBlockLookup.length; index++) {
  30. re = new RegExp('EXCS_CODE_BLOCK_' + index + '_(="")?', 'gi');
  31. __html = replaceCodeBlock(__html, re, codeBlockLookup[index]);
  32. }
  33. return __html;
  34. };
  35. const encodeEntities = _html => encodeCodeBlocks(_html);
  36. const decodeEntities = _html => decodeCodeBlocks(_html);
  37. let $;
  38. $ = cheerio.load(encodeEntities(html), Object.assign({
  39. decodeEntities: false
  40. }, pick(options, [
  41. 'xmlMode',
  42. 'decodeEntities',
  43. 'lowerCaseTags',
  44. 'lowerCaseAttributeNames',
  45. 'recognizeCDATA',
  46. 'recognizeSelfClosing'
  47. ])));
  48. results.hrefs = [];
  49. $('link').each((index, element) => {
  50. const $el = $(element);
  51. if ($el.attr('rel') && $el.attr('rel').toLowerCase() === 'stylesheet') {
  52. if (options.applyLinkTags) {
  53. results.hrefs.push($el.attr('href'));
  54. }
  55. if (options.removeLinkTags) {
  56. $el.remove();
  57. }
  58. }
  59. });
  60. results.html = decodeEntities($.html());
  61. return results;
  62. };