I want Ruby to retrieve the string in the markdown check box from the string.

Asked 2 years ago, Updated 2 years ago, 132 views

As the title says,

I would like to implement something like getting only the string (-[]-[x]) in the check box from the Ruby markdown text and returning the array of the string. Could you please let me know if you have a good library?

ruby markdown

2022-09-30 11:01

2 Answers

Output HTML through the Markdown parser and search for DOM elements using Nokogiri.If Markdown and/or search criteria are complicated, this is the surest thing.

An advanced Markdown parser such as Redcarpet may have a way to customize the behavior, so you can use it to hook the rendering of the list element and output the data somewhere.

If it's a simple way, you might want to search for the Markdown source by regular expression.

source.scan(/^\s*-\s*\[(.*)\]\s*(.*)$/){|s|...}

US>%s is passed in an array of characters and subsequent strings.There are difficulties such as being indistinguishable from inside and outside the code block or being difficult to handle nested lists, but this is sufficient if you are certain that the source format will be limited.


2022-09-30 11:01

How about regular expressions (Ruby can only be used in standard libraries)

Sample
http://rubular.com/r/nkgIQByUuS

Easy-to-understand regular expression commentary
http://qiita.com/jnchito/items/893c887fbf19e17d3ff9

Function to search for regular expressions in Ruby
http://ref.xaio.jp/ruby/classes/string/scan

If you make good use of groups, you can get only the checked strings.
Try it.


2022-09-30 11:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.