gjtorikian / commonmarker

Ruby wrapper for the comrak (CommonMark parser) Rust crate
MIT License
416 stars 80 forks source link

Front-matter delimiter type #256

Closed noraj closed 8 months ago

noraj commented 9 months ago

Let's consider the following markdown file with a YAML front-matter:

---
yaml: true                                                                             
age: 42                                                                                
---                                                                                    
# Title 1   

Of course without the front-matter extension commonmarker tries to parse the YAML front-matter as markdown:

require 'commonmarker' 

Commonmarker.to_html("---\nyaml: true\nage: 42\n---\n# Title 1")
# => "<hr />\n<h2><a href=\"#yaml-true-age-42\" aria-hidden=\"true\" class=\"anchor\" id=\"yaml-true-age-42\"></a>yaml: true<br />\nage: 42</h2>\n<h1><a href=\"#title-1\" aria-hidden=\"true\" class=\"anchor\" id=\"title-1\"></a>Title 1</h1>\n"

But if I configure the extension, the error tells me the option value should be NilClass while of course it should be a string containing the delimiter.

Which is also what comrak documentation says:

  --front-matter-delimiter <DELIMITER>
      Ignore front-matter that starts and ends with the given string
require 'commonmarker' 

Commonmarker.to_html("---\nyaml: true\nage: 42\n---\n# Title 1", options: { extension: { front_matter_delimiter: '---' } } )
/home/noraj/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/commonmarker-1.0.0.pre10-x86_64-linux/lib/commonmarker/utils.rb:18:in `fetch_kv': extension option `:front_matter_delimiter` must be NilClass; got String (TypeError)                                                                                                               

        raise TypeError, "#{type} option `:#{key}` must be #{expected_type}; got #{option[key].class}"                        
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                        
        from /home/noraj/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/commonmarker-1.0.0.pre10-x86_64-linux/lib/commonmarker/config.rb:75:in `block (3 levels) in <module:Config>'
        from /home/noraj/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/commonmarker-1.0.0.pre10-x86_64-linux/lib/commonmarker/config.rb:66:in `each'
        from /home/noraj/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/commonmarker-1.0.0.pre10-x86_64-linux/lib/commonmarker/config.rb:66:in `each_with_object'
        from /home/noraj/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/commonmarker-1.0.0.pre10-x86_64-linux/lib/commonmarker/config.rb:66:in `block (2 levels) in <module:Config>'
        from /home/noraj/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/commonmarker-1.0.0.pre10-x86_64-linux/lib/commonmarker/config.rb:53:in `process_options'
        from /home/noraj/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/commonmarker-1.0.0.pre10-x86_64-linux/lib/commonmarker.rb:24:in `to_html'
        from (irb):3:in `<main>'                                                                                              
        from /home/noraj/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.6.2/exe/irb:11:in `<top (required)>'        
        from /home/noraj/.asdf/installs/ruby/3.2.2/bin/irb:25:in `load'                                                       
        from /home/noraj/.asdf/installs/ruby/3.2.2/bin/irb:25:in `<main>'

If I provide a nil instead of the string delimiter, it obviously doesn't return the expected output but also surprisingly remove the titles anchors.

Commonmarker.to_html("---\nyaml: true\nage: 42\n---\n# Title 1", options: { extension: { front_matter_delimiter: nil } } )
=> "<hr />\n<h2>yaml: true<br />\nage: 42</h2>\n<h1>Title 1</h1>\n"

This is a behavior using 1.0.0.pre10 version (with comrak).

I can't test for v0.23.10, as there is no front-matter support for this version and parser.