截断 Markdown?
我有一个 Rails 站点,其中的内容是用 markdown 编写的。 我希望显示每个内容的片段,并带有“阅读更多..”链接。
我该怎么办? 例如,简单地截断原始文本是行不通的。
>> "This is an [example](http://example.com)"[0..25]
=> "This is an [example](http:"
理想情况下,我希望允许作者(可选)插入一个标记来指定用作“片段”的内容,如果没有,则需要 250 个单词,并附加“。 .." - 例如..
This article is an example of something or other.
This segment will be used as the snippet on the index page.
^^^^^^^^^^^^^^^
This text will be visible once clicking the "Read more.." link
标记可以被认为是 EOF 标记(在显示完整文档时可以忽略)
我正在使用 maruku 用于 Markdown 处理(RedCloth 非常偏向 Textile,BlueCloth 非常有缺陷,我想要一个本机 Ruby 解析器,它排除了 peg-markdown 和 RDiscount)
或者(因为 Markdown 已翻译)无论如何,正确截断 HTML 是一种选择 - 尽管最好不要 markdown()
整个文档,而只是为了获取前几行。
所以,我能想到的选项是(按优先顺序)..
- 向 maruku 解析器添加一个“截断”选项,它只会解析前 x 个单词,或者直到“摘录”标记。
- 编写/查找与解析器无关的 Markdown truncate'r
- 编写/查找智能 HTML 截断函数
I have a Rails site, where the content is written in markdown. I wish to display a snippet of each, with a "Read more.." link.
How do I go about this? Simple truncating the raw text will not work, for example..
>> "This is an [example](http://example.com)"[0..25]
=> "This is an [example](http:"
Ideally I want to allow the author to (optionally) insert a marker to specify what to use as the "snippet", if not it would take 250 words, and append "..." - for example..
This article is an example of something or other.
This segment will be used as the snippet on the index page.
^^^^^^^^^^^^^^^
This text will be visible once clicking the "Read more.." link
The marker could be thought of like an EOF marker (which can be ignored when displaying the full document)
I am using maruku for the Markdown processing (RedCloth is very biased towards Textile, BlueCloth is extremely buggy, and I wanted a native-Ruby parser which ruled out peg-markdown and RDiscount)
Alternatively (since the Markdown is translated to HTML anyway) truncating the HTML correctly would be an option - although it would be preferable to not markdown()
the entire document, just to get the first few lines.
So, the options I can think of are (in order of preference)..
- Add a "truncate" option to the maruku parser, which will only parse the first x words, or till the "excerpt" marker.
- Write/find a parser-agnostic Markdown truncate'r
- Write/find an intelligent HTML truncating function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(7)
您可以使用正则表达式来查找仅包含“^”字符的行:
markdown_string = <<-eos
This article is an example of something or other.
This segment will be used as the snippet on the index page.
^^^^^^^^^^^^^^^
This text will be visible once clicking the "Read more.." link
eos
preview = markdown_string[0...(markdown_string =~ /^\^+$/)]
puts preview
不确定它是否适用于这种情况,但为了完整性添加下面的解决方案。 如果要截断 Markdown 渲染的内容,可以使用 strip_tags 方法:
truncate(strip_tags(markdown(article.contents)), length: 50)
来源:
http://devblog.boonecommunitynetwork.com/rails-and-markdown/
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
以下内容来自 http:// /mikeburnscoder.wordpress.com/2006/11/11/truncating-html-in-ruby/,经过一些修改将正确截断 HTML,并轻松允许在结束标记之前附加字符串。
修改后的代码:
The following from http://mikeburnscoder.wordpress.com/2006/11/11/truncating-html-in-ruby/, with some modifications will correctly truncate HTML, and easily allow appending a string before the closing tags.
The modified code: