gpt4 book ai didi

ruby-on-rails - Redcarpet 语法高亮

转载 作者:数据小太阳 更新时间:2023-10-29 07:41:18 47 4
gpt4 key购买 nike

我正在尝试使用 Redcarpet 获得语法高亮显示工作

我的appliaction_helper.rb:

module ApplicationHelper

def markdown(text)
render_options = {
# will remove from the output HTML tags inputted by user
filter_html: true,
# will insert <br /> tags in paragraphs where are newlines
hard_wrap: true,
# hash for extra link options, for example 'nofollow'
link_attributes: { rel: 'nofollow' },
# Prettify
prettify: true
}
renderer = Redcarpet::Render::HTML.new(render_options)

extensions = {
#will parse links without need of enclosing them
autolink: true,
# blocks delimited with 3 ` or ~ will be considered as code block.
# No need to indent. You can provide language name too.
# ```ruby
# block of code
# ```
fenced_code_blocks: true,
# will ignore standard require for empty lines surrounding HTML blocks
lax_spacing: true,
# will not generate emphasis inside of words, for example no_emph_no
no_intra_emphasis: true,
# will parse strikethrough from ~~, for example: ~~bad~~
strikethrough: true,
# will parse superscript after ^, you can wrap superscript in ()
superscript: true
# will require a space after # in defining headers
# space_after_headers: true
}
Redcarpet::Markdown.new(renderer, extensions).render(text).html_safe
end

end

尽管输出显示在代码块( Redcarpet )中:

enter image description here

我找不到语法高亮显示

我刚接触 Redcarpet,有人知道解决方案吗?

最佳答案

好的,我找到了 -> Markdown and code syntax highlighting in Ruby on Rails (using RedCarpet and CodeRay)这非常有效(连同 Coderay 的一些自定义 CSS)。

gem 文件:

gem 'redcarpet', github: 'vmg/redcarpet'
gem 'coderay'

Application_helper.rb

class CodeRayify < Redcarpet::Render::HTML
def block_code(code, language)
CodeRay.scan(code, language).div
end
end

def markdown(text)
coderayified = CodeRayify.new(:filter_html => true,
:hard_wrap => true)
options = {
:fenced_code_blocks => true,
:no_intra_emphasis => true,
:autolink => true,
:strikethrough => true,
:lax_html_blocks => true,
:superscript => true
}
markdown_to_html = Redcarpet::Markdown.new(coderayified, options)
markdown_to_html.render(text).html_safe
end

关于ruby-on-rails - Redcarpet 语法高亮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23785970/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com