Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Front-matter delimiter type #256

Closed
noraj opened this issue Oct 3, 2023 · 0 comments · Fixed by #257
Closed

Front-matter delimiter type #256

noraj opened this issue Oct 3, 2023 · 0 comments · Fixed by #257

Comments

@noraj
Copy link
Contributor

noraj commented Oct 3, 2023

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.

noraj added a commit to noraj/commonmarker that referenced this issue Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant