是否有更简洁的方法在 HAML 中启用 markdown 输出?

发布于 2024-10-21 02:36:41 字数 437 浏览 0 评论 0原文

在 Rails 中使用 HAML,只要我有某种类型的文本字段,我希望用户能够轻松地设置格式,我就会使用 HAML 的 markdown 过滤器。在看起来像这样的模板中:

.description
  :markdown
    #{folder.description}

这可行,但我从来没有真正喜欢过它。如果我不需要在此文本字段上进行降价,我就会这样做:

.description= folder.description

在我的一些视图中,有许多可以进行降价格式化的文本实例,并且它总是会破坏模板的插入流程。

是否有任何更清晰或更简单的方法可以在 HAML 模板中使用 Markdown 格式的文本,或者是否有另一种方法可以将简单的文本格式添加到文本字段,从而生成更清晰的模板代码?

谢谢!

Using HAML in Rails, whenever I have a text field of some kind that I want the user to be able to easily format, I use HAML's markdown filter. In the template that looks like this:

.description
  :markdown
    #{folder.description}

This works, but I've never really liked it. If I didn't need markdown on this text field I would just be doing:

.description= folder.description

In some of my views there are many instances of text that can be markdown formatted, and it always breaks the flow of the template to insert them.

Is there any cleaner or simpler way to use markdown formatted text in a HAML template, or is there perhaps an alternative method of adding simple text formatting to a text field that results in cleaner template code?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱皱眉﹌ 2024-10-28 02:36:41

我建议一个简单的助手,例如:

# app/helpers/application_helper.rb
def markdown(text)
  BlueCloth::new(text).to_html.xml_safe
end
alias :md :markdown

# app/views/folders/show.html.haml
.description= md folder.description

I would suggest a simple helper, something like:

# app/helpers/application_helper.rb
def markdown(text)
  BlueCloth::new(text).to_html.xml_safe
end
alias :md :markdown

# app/views/folders/show.html.haml
.description= md folder.description
浴红衣 2024-10-28 02:36:41

当我有可编译的格式(无论是 Markdown 还是 Textile)时,我通常将 html 版本和格式版本存储在数据库中。

因此,我可能会有 folder.description_markdownfolder.description_html 字段,并且在文件夹或其他适当位置的 before_save 中,我将在那里执行 markdown 解析,并保存html 值。

然后,在您看来,您所需要的只是:

.description= raw folder.description_html

如果您有描述的编辑页面,您将确保文本字段正确编辑description_markdown 字段。

我现在用来保存 Textile 格式的 before_save 回调示例就在这里:

class Page < ActiveRecord::Base
  validates_presence_of :name
  validates_uniqueness_of :name

  # Note that the {attr}_changed? method is given to you
  # by ActiveRecord for every database field on your model.
  before_save :update_html, :if => :textile_changed?

  def convert_textile
    RedCloth.new(textile).to_html
  end

  protected
    def update_html
      self.html = convert_textile
    end
end

我觉得这使得 haml 模板更易于遵循,并且还减少了每次检索字段时必须解析 markdown 的开销。

When I have compilable formatting (whether it be markdown, or textile) I usually store both the html version, and the format version in the database.

So I would probably have folder.description_markdown and folder.description_html fields, and in a before_save on folder or some other appropriate location I would perform the markdown parsing there, and save the html value.

Then, in your view, all you would need is:

.description= raw folder.description_html

And if you have an edit page for the description you would make sure the text field is correctly editing the description_markdown field.

An example before_save callback I have right now to save Textile formatting is right here:

class Page < ActiveRecord::Base
  validates_presence_of :name
  validates_uniqueness_of :name

  # Note that the {attr}_changed? method is given to you
  # by ActiveRecord for every database field on your model.
  before_save :update_html, :if => :textile_changed?

  def convert_textile
    RedCloth.new(textile).to_html
  end

  protected
    def update_html
      self.html = convert_textile
    end
end

I feel like this makes the haml template easier to follow, and also reduces overhead from having to parse the markdown every time the field is retrieved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文