Example #1
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 #2
0
  highlight: (code, language) => {
    code = code.trim()

    // language is recognized by highlight.js
    if (hljs.getLanguage(language)) {
      return hljs.highlight(language, code).value
    }

    // fallback to auto
    return hljs.highlightAuto(code).value
  },
Example #3
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 #4
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 #5
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;
    }
  }
 function highlight(data) {
   if (!query.highlight && isCssTpl(url)) {
     data = handleImagesPath(data, username, repo, branch, file);
   }
   if (query.highlight) {
     var lang = query.lang || "at";
     data = hljs.highlight(lang, data).value;
     data = "<pre class='prettyprint'><code class='"+ lang +"'>" + data + "</code></pre>";
   }
   return data;
 }
Example #7
0
 highlight: function (code, lang, callback) {
     //var highlighted = hljs.highlightAuto(code, lang ? [ (lang !== 'sh') ? lang : 'bash' ] : []);
     var highlighted = hljs.highlight((lang === 'sh') ? 'gams' : lang, code);
     //console.log('-------------');
     //console.log(lang, highlighted.language);
     //console.log('->');
     //console.log(code);
     //console.log('-------------');
     callback(null, highlighted ? highlighted.value : '');
     //return highlighted ? highlighted.value : '';
 }
Example #8
0
const highlight = function(code, lang) {
	if (!lang) {
		return code;
	}
	try {
		return hljs.highlight(lang, code).value;
	} catch (e) {
		// Unknown language
		return code;
	}
};
Example #9
0
 highlight: function (code, lang, callback) {
   if (lang === 'dot') {
     graphvizualize(code, callback);
   } else if (lang === 'js') {
     return callback(null, eshighlight(code));
   } else if (lang) {
     return callback(null, hljs.highlight(lang, code).value);
   } else {
     return callback(null, code);
   }
 },
Example #10
0
function highlight(code, callback){
    const re = /:::(\S+)\n([\s\S]+)/.exec(code);
    let lang, content;
    if(re){
        lang = re[1];
        content = re[2];
    }
    else{
        lang = defaultHighlight;
        content = code;
    }
    let result;
    try{
        result = highlighter.highlight(lang, content).value;
    }
    catch (e){
        result = highlighter.highlight(defaultHighlight, content).value;
    }
    return result;
}
Example #11
0
 highlight: function(code, lang) {
   if(lang === 'none') {
     return code;
   }
   else if(lang === undefined) {
     return hljs.highlightAuto(code).value;
   } 
   else {
     return hljs.highlight(lang, code).value;
   }
 }
Example #12
0
    highlight: function(code, lang) {
      code = lang
          ? hljs.highlight(lang, code).value
          // since we're not using highlight.js here, we need to espace the html
          // unescape first in order to avoid double escaping
          : he.escape(he.unescape(code));

      // Allow for highlighting portions of code blocks
      // using `**` before and after
      return code.replace(/\*\*(.+)?\*\*/g, '<mark>$1</mark>');
    }
Example #13
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 #14
0
var codeHl = function(code, language) {
  if (!language) {
    return '<pre>' + escape(code) + '</pre>';
  }
  if (language === 'html') {
    language = 'xml';
  }
  code = hl.highlight(language, code).value;

  return format('<div class="doc-code demo-highlight">' +
    '<pre><code class="%s">%s</code></pre></div>', language, code);
};
  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 #16
0
 options.markdownOptions.highlight = function(code, lang) {
   var out = code;
   try {
     out = hljs.highlight(lang, code).value;
   } catch(e) {
     out = hljs.highlightAuto(code).value;
   }
   if(shouldWrap) {
     out = wrapLines(out);
   }
   return out;
 };
Example #17
0
function highlight(code, lang) {
  try {
    try {
      return highlightjs.highlight(lang, code).value
    } catch(err) {
      if (!err.message.match(/Unknown language/)) throw err
      return highlightjs.highlightAuto(code).value
    }
  } catch(err) {
    return code
  }
}
Example #18
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;
  }
Example #19
0
 r.code = function(code, lang) {
   var out = '<pre>';
   if (lang) {
     out += '<code class="' + lang + '">' +
         hljs.highlight(lang, code).value;
   } else {
     out += '<code>' +
         hljs.highlightAuto(code).value;
   }
   out += '\n</code></pre>';
   return out;
 };
Example #20
0
        highlight: function (code, lang) {
            if(!lang) return code;

            // Normalize lang
            lang = lnormalize(lang);

            try {
                return hljs.highlight(lang, code).value;
            } catch(e) { }

            return code;
        }
Example #21
0
exports.astToBemjson = function(c, p) {

    var lang = U.translateAlias(p),
        res = p? HL.highlight(lang, c) : HL.highlightAuto(c);

    return {
        block: 'highlight',
        lang: res.language,
        content: res.value
    };

};
Example #22
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 #23
0
          highlight: function(code, lang) {
            var hjs = require('highlight.js');

            var result;
            if (lang) {
              result = hjs.highlight(lang, code);
            } else {
              result = hjs.highlightAuto(code);
            }

            return result.value;
          }
Example #24
0
    highlight: function(code, lang){
      var ret;

      if( lang ){
        ret = hljs.highlight(lang, code).value;
      } else {
        ret = hljs.highlightAuto(code).value;
      }

      return ret;

    },
Example #25
0
  highlight: function highlight(data, lang) {
    var fallback = !this.mapping[lang] ? 'default' : undefined;
    var hl = this.unescape(data);

    if (lang !== 'markdown') {
      if (!lang) {
        hl = hljs.highlightAuto(hl);
      } else {
        try {
          hl = hljs.highlight(lang, hl);
        } catch (e) {
          hl = hljs.highlightAuto(hl);
        }
      }
      if (hl.language) {
        fallback = hl.language;
      }
      hl = hl.value;
    }

    hl = this.format(hl);

    var mappingLang = fallback || lang;
    mappingLang = !this.mapping[mappingLang] ? 'default' : mappingLang;

    // If a custom language is detected, apply its styles.
    for (var color in this.mapping[mappingLang]) {
      if (this.mapping[mappingLang].hasOwnProperty(color)) {
        var clr = String(color).replace(/[0-9]/g, '');
        hl = String(hl).replace(this.mapping[mappingLang][color], chalk[clr]('$1'));
      }
    }

    // If the "default" styles weren't applied, apply them now.
    if (mappingLang !== 'default') {
      for (var color in this.mapping['default']) {
        if (this.mapping['default'].hasOwnProperty(color)) {
          var clr = String(color).replace(/[0-9]/g, '');
          hl = String(hl).replace(this.mapping['default'][color], chalk[clr]('$1'));
        }
      }
    }

    // Catch any highlighting tags not given in
    // that theme file, and reset any color on them.
    for (var style in this.mapping.fallback) {
      if (this.mapping.fallback.hasOwnProperty(style)) {
        hl = String(hl).replace(this.mapping.fallback[style], chalk.reset('$1'));
      }
    }

    return hl;
  },
Example #26
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 #27
0
function highlight(lang, code) {
  if(!lang) {
    return code;
  }
  // Normalize lang
  lang = normalize(lang);

  try {
    return hljs.highlight(lang, code).value;
  } catch(e) { }

  return code;
}
Example #28
0
  return str.replace(/(<pre><code>)([^<]+)(<\/code)/gm, function (_, $1, $2, $3) {

    // parse out the ```language
    var code = /^(?:`{3}([^\n]+)\n)?([\s\S]*)/gm.exec($2);

    if ('js' == code[1] || !code[1]) {
      code[1] = 'javascript';
    }

    return $1
          + hl.highlight(code[1], code[2]).value.trim()
          + $3;
  });
Example #29
0
  highlight: function (str, lang) {
    if (lang && hljs.getLanguage(lang)) {
      try {
        return hljs.highlight(lang, str).value;
      } catch (__) {}
    }

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

    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 '';
    },