Example #1
0
var getLang = function(language) {
  if (!language) {
    return null;
  }

  if (language === 'html') {
    return 'html';
  }

  var shortcuts = {
    'js': 'javascript',
    'json': 'javascript',
    'py': 'python',
    'rb': 'ruby',
    'md': 'markdown',
    'mkd': 'markdown',
    'c++': 'cpp'
  };

  if (language && shortcuts[language]) {
    language = shortcuts[language];
  }

  if (!language || !hl.getLanguage(language)) {
    return null;
  }

  return language;
};
Example #2
0
  highlight: function (aCode, aLang) {
    var obj = null;

    if (aLang && hljs.getLanguage(aLang)) {
      try {
        return hljs.highlight(aLang, aCode).value;
      } catch (aErr) {
      }
    }

    try {
      obj = hljs.highlightAuto(aCode);

      switch (obj.language) {
        // Transform list of auto-detected language highlights
        case 'dust':
        case '1c':
          // Narrow auto-detection to something that is more likely
          return hljs.highlightAuto(aCode, ['css', 'html', 'js', 'json']).value;
        // Any other detected go ahead and return
        default:
          return obj.value;
      }
    } catch (aErr) {
    }

    // If any external package failure don't block return e.g. prevent empty
    return aCode;
  },
Example #3
0
 highlight : function(code, lang){
     if(lang && hljs.getLanguage(lang)){
         return hljs.highlight(lang, code).value;
     } else {
         return hljs.highlightAuto(code).value;
     }
 }
Example #4
0
 , highlight   : function (str, lang) {
       if (lang && hljs.getLanguage(lang)) {
           return hljs.highlight(lang, str, true).value;
       } else {
           return hljs.highlightAuto(str).value;
       }
   }
Example #5
0
 highlight: (str, lang)=> {
   if (lang && hljs.getLanguage(lang))
     try {
       return hljs.highlight(lang, str).value;
     } catch (__) {}
   return ''; // use external default escaping
 }
Example #6
0
 highlight: function (aCode, aLang) {
   if (aLang && hljs.getLanguage(aLang)) {
     return hljs.highlight(aLang, aCode).value;
   } else {
     return hljs.highlightAuto(aCode).value;
   }
 },
Example #7
0
 highlight: function (str, lang) {
   if (lang && hljs.getLanguage(lang)) {
     try {
       return hljs.highlight(lang, str).value;
     } catch (__) {}
   }
   return str;
 }
Example #8
0
function generateCode(code, lang) {
  
  const result = lang && highlight.getLanguage(lang)
    ? highlight.highlight(lang, code)
    : highlight.highlightAuto(code);

  return `<div class="highlight"><pre><code language=${result.language}>${result.value}</code></pre></div>`;
}
Example #9
0
export function highlight(str, lang) {
    if (lang && hljs.getLanguage(lang)) {
        try {
            return hljs.highlight(lang, str).value;
        } catch (__) {}
    }
    return ''; // use external default escaping
}
 highlight: function (code, lang) {
   var highlight = require('highlight.js'),
       languageObject = highlight.getLanguage(lang);
   if(languageObject) {
     return highlight.highlight(lang, code, true).value;
   } else {
     return highlight.highlightAuto(code).value;
   }
 },
function hl(code, lang) {
  var result;
  if (typeof lang === 'string' && hljs.getLanguage(lang)) {
    result = hljs.highlight(lang, code, true).value;
  } else {
    result = hljs.highlightAuto(code).value;
  }
  return '<pre class="hljs"><code>' + result + '</code></pre>';
}
Example #12
0
    highlight: function (str, lang) {
        if (lang && hljs.getLanguage(lang)) {
            try {
                return `<pre class="hljs" lang=${lang}><code>${hljs.highlight(lang, str, true).value}</code></pre>`;
            } catch (__) {}
        }

        return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
    }
Example #13
0
 highlight: (code, lang) => {
   code = code.trim()
   // language is recognized by highlight.js
   if (lang && hljs.getLanguage(lang)) {
     return hljs.highlight(lang, code).value
   }
   // ...or fallback to auto
   return hljs.highlightAuto(code).value
 },
Example #14
0
 highlight: function(str, lang) {
   if(lang && hljs.getLanguage(lang))
     try {
       return hljs.highlight(lang, str).value;
     }
     catch (e) {
       return '';
     }
   return '';
 }
Example #15
0
 highlight: function (str, lang) {
     if (lang && hljs.getLanguage(lang)) {
         try {
             return '<pre class="hljs"><code>' +
                 hljs.highlight(lang, str, true).value + '</code></pre>';
         } 
         catch (ex) { throw ex; }
     }
     return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
 }
Example #16
0
  highlight: function (aCode, aLang) {
    var obj = null;
    var lang = [ // NOTE: More likely to less likely
      'javascript', 'xpath', 'xml',
        'css', 'less', 'scss',
          'json',
            'diff',
              'shell',
                'bash', 'dos',
                  'vbscript'
    ];

    if (aLang && hljs.getLanguage(aLang)) {
      try {
        return hljs.highlight(aLang, aCode).value;
      } catch (aE) {
        if (isDev) {
          console.error([
            colors.red('Dependency named highlighting failed with:'),
              aE

          ].join('\n'));
        }
      }
    }

    try {
      obj = hljs.highlightAuto(aCode);

      if (lang.indexOf(obj.language) > -1) {
        return obj.value;
      } else {
        if (isDev) {
          console.log([
            colors.yellow('Unusual auto-detected md language code is')
              + '`' + colors.cyan(obj.language) + '`',

          ].join('\n'));
        }
        return hljs.highlightAuto(aCode, lang).value;
      }
    } catch (aE) {
      if (isDev) {
        console.error([
          colors.red('Dependency automatic named highlighting failed with:'),
            aE

        ].join('\n'));
      }
    }

    // If any external package failure don't block return e.g. prevent empty
    return aCode;
  },
Example #17
0
File: index.js Project: SevInf/sldr
 highlightCode: function(code, language) {
     if (language && hljs.getLanguage(language)) {
         try {
             const highlighted = hljs.highlight(language, code, true).value;
             return `<pre class="hljs code"><code>${highlighted}</code></pre>`;
         } catch (e) {
             return '';
         }
     }
     return '';
 },
Example #18
0
var highlightSyntax = (str, lang) =>
{
    if (lang && hljs.getLanguage(lang)) {
        try {
            return '<pre class="hljs"><code>' +
                hljs.highlight(lang, str, true).value +
                '</code></pre>';
        } catch (__) {}
    }
    return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
}
Example #19
0
  highlight: function(code, lang) {
    lang = hljsLangMappings[lang] || lang;

    // if this is a known hljs language then highlight
    if (hljs.getLanguage(lang)) {
      return hljs.highlight(lang, code).value;
    }
    else {
      return code;
    }
  }
Example #20
0
highlight: function (str, lang) {
if(lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(lang, str).value.replace(/\n/g, '<br>');
} catch (err) {}
}
try {
return hljs.highlightAuto(str).value.replace(/\n/g, '<br>');
} catch (err) {}
return '';
}
Example #21
0
 highlight: (str, lang) => {
   let content;
   if (lang && hljs.getLanguage(lang)) {
     try {
       content = hljs.highlight(lang, str, true).value;
     } catch (__) {}
   }
   if (!content) {
     content = md.utils.escapeHtml(str);
   }
   return `<pre class="hljs"><code>${content}</code></pre>`;
 }
Example #22
0
  /**
   * Create code block html.
   * @param {string} language - code block language
   * @param {string} codeText - code text
   * @returns {string}
   */
  createCodeBlockHtml(language, codeText) {
    const replacer = this.getReplacer(language);
    let html;

    if (replacer) {
      html = replacer(codeText, language);
    } else {
      html = hljs.getLanguage(language) ? hljs.highlight(language, codeText).value : escape(codeText, false);
    }

    return html;
  }
  renderer.code = function(code, lang) {
    lang = (lang + '').toLowerCase();

    if (lang === "text") {
      return util.format('<pre><code class="text">%s</code></pre>', htmlencode.htmlEncode(code));
    }

    if (highlight.getLanguage(lang))
      return util.format('<pre><code class="%s">%s</code></pre>', lang, highlight.highlight(lang, code).value);

    return util.format('<pre><code>%s</code></pre>', highlight.highlightAuto(code).value);
  };
Example #24
0
    highlight: (code, lang) => {
      if (lang && hljs.getLanguage(lang)) {
        try {
          return hljs.highlight(lang, code).value;
        } catch (__) { console.log(__) }
      } else {
        try {
          return hljs.highlightAuto(code).value;
        } catch (__) { console.log(__) }
      }

      return ''; // use external default escaping
    }
Example #25
0
    highlight: function (str, lang) {
      if (lang && hljs.getLanguage(lang)) {
        try {
          return hljs.highlight(lang, str).value
        } catch (err) {}
      }

      try {
        return hljs.highlightAuto(str).value
      } catch (err) {}

      return ''
    }
    highlight: function (str, lang) {
        if (lang && hljs.getLanguage(lang)) {
            try {
                return hljs.highlight(lang, str).value;
            } catch (err) { }
        }

        try {
            return hljs.highlightAuto(str).value;
        } catch (err) { }

        return ''; // use external default escaping 
    }
    highlight: (str, lang) => {
      if (lang && hljs.getLanguage(lang)) {
        try {
          return hljs.highlight(lang, str).value;
        } catch (err) { console.error(err.stack); } // eslint-disable-line no-console
      }

      try {
        return hljs.highlightAuto(str).value;
      } catch (err) { console.error(err.stack); } // eslint-disable-line no-console

      return '';
    },
Example #28
0
  highlight: (str, lang) => {
    if (lang && hljs.getLanguage(lang)) {
      try {
        return hljs.highlight(lang, str).value;
      } catch (_) {} // eslint-disable-line no-empty
    }

    try {
      return hljs.highlightAuto(str).value;
    } catch (_) {} // eslint-disable-line no-empty

    return ''; // use external default escaping
  },
Example #29
0
const highlight = (str, lang) => {
  if (lang !== null && hljs.getLanguage(lang)) {
    try {
      return hljs.highlight(lang, str).value;
    } catch (error) {
      logger.error(error);
    }
  }
  try {
    return hljs.highlightAuto(str).value;
  } catch (error) {
    logger.error(error);
  }
  return '';
};
Example #30
0
const highlight = (str, lang) => {
    if ((lang !== null) && hljs.getLanguage(lang)) {
        try {
            return hljs.highlight(lang, str).value
        } catch (_error) {
            console.error(_error)
        }
    }
    try {
        return hljs.highlightAuto(str).value
    } catch (_error) {
        console.error(_error)
    }
    return ''
}