Example #1
0
module.exports = (language, code, lineNumbersHighlight = []) => {
  // (Try to) load languages on demand.
  if (!Prism.languages[language]) {
    try {
      loadPrismLanguage(language)
    } catch (e) {
      // Language wasn't loaded so let's bail.
      if (language === `none`) {
        return code // Don't escape if set to none.
      } else {
        return _.escape(code)
      }
    }
  }

  const grammar = Prism.languages[language]

  const highlighted = Prism.highlight(code, grammar, language)
  const codeSplits = handleDirectives(highlighted, lineNumbersHighlight)

  let finalCode = ``

  const lastIdx = codeSplits.length - 1
  // Don't add back the new line character after highlighted lines
  // as they need to be display: block and full-width.
  codeSplits.forEach((split, idx) => {
    split.highlight
      ? (finalCode += split.code)
      : (finalCode += `${split.code}${idx == lastIdx ? `` : `\n`}`)
  })

  return finalCode
}
Example #2
0
/**
 * The highlight method for use with markdown-it. Uses highlight.js.
 * @param  {String} str  HTML string to highlight
 * @param  {String} lang The language to use for highlighting
 * @return {String}
 */
function highlight(str, lang) {

  if (lang && hljs.getLanguage(lang)) {
    try {
      return hljs.highlight(lang, str).value;
    } catch (e) {
      return ``;
    }
  }

  return ``;

}
Example #3
0
module.exports = (language, code, lineNumbersHighlight = []) => {
  // (Try to) load languages on demand.
  if (!Prism.languages[language]) {
    try {
      loadPrismLanguage(language)
    } catch (e) {
      // Language wasn't loaded so let's bail.
      if (language === `none`) {
        return code // Don't escape if set to none.
      } else {
        return _.escape(code)
      }
    }
  }

  const grammar = Prism.languages[language]

  let highlightedCode = Prism.highlight(code, grammar, language)
  if (lineNumbersHighlight.length > 0) {
    const codeSplits = highlightedCode.split(`\n`).map((split, i) => {
      if (_.includes(lineNumbersHighlight, i + 1)) {
        return {
          highlighted: true,
          code: `<span class="gatsby-highlight-code-line">${split}\n</span>`,
        }
      } else {
        return { code: split }
      }
    })

    highlightedCode = ``
    const lastIdx = codeSplits.length - 1
    // Don't add back the new line character after highlighted lines
    // as they need to be display: block and full-width.
    codeSplits.forEach((split, idx) => {
      split.highlighted
        ? (highlightedCode += split.code)
        : (highlightedCode += `${split.code}${idx == lastIdx ? `` : `\n`}`)
    })
  }

  return highlightedCode
}
module.exports = function (language, code) {
  var lineNumbersHighlight = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];

  // (Try to) load languages on demand.
  if (!Prism.languages[language]) {
    try {
      loadPrismLanguage(language);
    } catch (e) {
      // Language wasn't loaded so let's bail.
      if (language === `none`) {
        return code; // Don't escape if set to none.
      } else {
        return _.escape(code);
      }
    }
  }

  var lang = Prism.languages[language];

  var highlightedCode = Prism.highlight(code, lang);
  if (lineNumbersHighlight.length > 0) {
    var codeSplits = highlightedCode.split(`\n`).map(function (split, i) {
      if (_.includes(lineNumbersHighlight, i + 1)) {
        return {
          highlighted: true,
          code: `<span class="gatsby-highlight-code-line">${split}\n</span>`
        };
      } else {
        return { code: split };
      }
    });

    highlightedCode = ``;
    var lastIdx = codeSplits.length - 1;
    // Don't add back the new line character after highlighted lines
    // as they need to be display: block and full-width.
    codeSplits.forEach(function (split, idx) {
      split.highlighted ? highlightedCode += split.code : highlightedCode += `${split.code}${idx == lastIdx ? `` : `\n`}`;
    });
  }

  return highlightedCode;
};
Example #5
0
module.exports = (language, code, lineNumbersHighlight = []) => {
  // (Try to) load languages on demand.
  if (!Prism.languages[language]) {
    try {
      require(`prismjs/components/prism-${language}.js`)
    } catch (e) {
      // Language wasn't loaded so let's bail.
      return code
    }
  }

  const lang = Prism.languages[language]

  let highlightedCode = Prism.highlight(code, lang)
  if (lineNumbersHighlight) {
    const codeSplits = highlightedCode.split(`\n`).map((split, i) => {
      if (_.includes(lineNumbersHighlight, i + 1)) {
        return {
          highlighted: true,
          code: `<span class="gatsby-highlight-code-line">${split}\n</span>`,
        }
      } else {
        return { code: split }
      }
    })

    highlightedCode = ``
    // Don't add a new line character after highlighted lines as they
    // need to be display: block and full-width.
    codeSplits.forEach(split => {
      split.highlighted
        ? (highlightedCode += split.code)
        : (highlightedCode += `${split.code}\n`)
    })
  }

  return highlightedCode
}