How to use the hexo-util.highlight function in hexo-util

To help you get started, we’ve selected a few hexo-util 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 hexojs / hexo / lib / plugins / filter / before_post_render / backtick_code_block.js View on Github external
if (match[3]) {
            options.caption += `<a href="${match[3]}">${match[4] ? match[4] : 'link'}</a>`;
          }
        }
      }
    }

    // PR #3765
    if (start.includes('&gt;')) {
      const depth = start.split('&gt;').length - 1;
      const regexp = new RegExp(`^([^\\S\\r\\n]*&gt;){0,${depth}}([^\\S\\r\\n]|$)`, 'mg');
      const paddingOnEnd = ' '; // complement uncaptured whitespaces at last line
      content = (content + paddingOnEnd).replace(regexp, '').replace(/\n$/, '');
    }

    content = highlight(stripIndent(content), options)
      .replace(/{/g, '{')
      .replace(/}/g, '}');

    return `${start}${content}${end}`;
  });
}
github hexojs / hexo / lib / plugins / tag / include_code.js View on Github external
code = stripIndent(code).trim();

    if (!config.enable) {
      return `<pre><code>${code}</code></pre>`;
    }

    // If the title is not defined, use file name instead
    title = title || basename(path);

    // If the language is not defined, use file extension instead
    lang = lang || extname(path).substring(1);

    const caption = `<span>${title}</span><a href="${ctx.config.root}${codeDir}${path}">view raw</a>`;

    return highlight(code, {
      lang,
      caption,
      gutter: config.line_number,
      hljs: config.hljs,
      tab: config.tab_replace
    });
  });
};
github hexojs / hexo / test / scripts / hexo / post.js View on Github external
it('render() - escaping swig blocks with similar names', () => {
    const code = 'alert("Hello world")';
    const highlighted = util.highlight(code);

    const content = [
      '{% codeblock %}',
      code,
      '{% endcodeblock %}',
      '',
      '{% code %}',
      code,
      '{% endcode %}'
    ].join('\n');

    return post.render(null, {
      content
    }).then(data => {
      data.content.trim().should.eql([
        highlighted,
github hexojs / hexo / test / scripts / tags / include_code.js View on Github external
it('from and to', () =&gt; {
    const fixture = [
      'sleep();'
    ].join('\n');
    const expected = highlight(fixture, {
      lang: 'js',
      caption: '<span>Hello world</span><a href="/downloads/code/test.js">view raw</a>'
    });

    return code('Hello world lang:js from:2 to:2 test.js').then(result =&gt; {
      result.should.eql(expected);
    });
  });
github hexojs / hexo / test / scripts / tags / code.js View on Github external
function highlight(code, options) {
    return util.highlight(code, options || {})
      .replace(/{/g, '&#123;')
      .replace(/}/g, '&#125;');
  }
github hexojs / hexo / test / scripts / filters / backtick_code_block.js View on Github external
function highlight(code, options) {
    return util.highlight(code, options || {})
      .replace(/{/g, '&#123;')
      .replace(/}/g, '&#125;');
  }
github hexojs / hexo / test / fixtures / post_render.js View on Github external
exports.expected = [
  '<h1 id="Title"><a title="Title" class="headerlink" href="#Title"></a>Title</h1>',
  highlight(code, {lang: 'python'}),
  '\n<p>some content</p>\n',
  '<h2 id="Another-title"><a title="Another title" class="headerlink" href="#Another-title"></a>Another title</h2>',
  '<blockquote>',
  '<p>quote content</p>\n',
  '</blockquote>\n\n',
  '<blockquote><p>quote content</p>\n',
  '<footer><strong>Hello World</strong></footer></blockquote>'
].join('');

exports.expected_disable_nunjucks = [
  '<h1 id="Title"><a title="Title" class="headerlink" href="#Title"></a>Title</h1>',
  highlight(code, {lang: 'python'}),
  '\n<p>some content</p>\n',
  '<h2 id="Another-title"><a title="Another title" class="headerlink" href="#Another-title"></a>Another title</h2>',
  '<p>{% blockquote %}<br>',
  'quote content<br>',
  '{% endblockquote %}</p>\n',
  '<p>{% quote Hello World %}<br>',
  'quote content<br>',
  '{% endquote %}</p>'
].join('');
github hexojs / hexo / lib / plugins / tag / code.js View on Github external
if (rHighlight.test(arg)) {
    arg = arg.replace(rHighlight, (match, _enable) =&gt; {
      enable = _enable === 'true';
      return '';
    });
  }

  if (!enable) {
    content = escapeHTML(content);
    return `<pre><code>${content}</code></pre>`;
  }

  content = stripIndent(content);

  content = highlight(content, getHighlightOptions(config, arg));

  content = content.replace(/{/g, '{')
    .replace(/}/g, '}');

  return content;
};
github hcoona / hexo-renderer-asciidoc / lib / renderer.js View on Github external
$('.highlight code').each(function(index, elem) {
    options.lang = elem.attribs['data-lang'];
    var code = entities.decodeXML($(elem).text());
    var content = util.highlight(code, options);
    $(elem).html(content);
  });