Skip to content

Commit

Permalink
Rework rb_markdown_to_html
Browse files Browse the repository at this point in the history
  • Loading branch information
ojab committed Feb 18, 2022
1 parent 272a721 commit 1d5032d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
18 changes: 5 additions & 13 deletions ext/commonmarker/commonmarker.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,36 +154,28 @@ static cmark_parser *prepare_parser(VALUE rb_options, VALUE rb_extensions) {
*
*/
static VALUE rb_markdown_to_html(VALUE self, VALUE rb_text, VALUE rb_options, VALUE rb_extensions) {
char *str, *html;
int len;
char *html;
cmark_parser *parser;
cmark_node *doc;

Check_Type(rb_text, T_STRING);
Check_Type(rb_options, T_FIXNUM);

parser = prepare_parser(rb_options, rb_extensions);

str = (char *)RSTRING_PTR(rb_text);
len = RSTRING_LEN(rb_text);

cmark_parser_feed(parser, str, len);
cmark_parser_feed(parser, StringValuePtr(rb_text), RSTRING_LEN(rb_text));
doc = cmark_parser_finish(parser);

if (doc == NULL) {
cmark_parser_free(parser);
rb_raise(rb_eNodeError, "error parsing document");
}

cmark_mem *default_mem = cmark_get_default_mem_allocator();
html = cmark_render_html_with_mem(doc, FIX2INT(rb_options), parser->syntax_extensions, default_mem);
html = cmark_render_html(doc, parser->options, parser->syntax_extensions);

cmark_parser_free(parser);
cmark_node_free(doc);

VALUE ruby_html = rb_str_new2(html);
default_mem->free(html);

return ruby_html;
return rb_utf8_str_new_cstr(html);
}

/*
Expand Down
4 changes: 1 addition & 3 deletions lib/commonmarker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ def self.render_html(text, options = :DEFAULT, extensions = [])
raise TypeError, "text must be a String; got a #{text.class}!" unless text.is_a?(String)

opts = Config.process_options(options, :render)
text = text.encode('UTF-8')
html = Node.markdown_to_html(text, opts, extensions)
html.force_encoding('UTF-8')
Node.markdown_to_html(text.encode('UTF-8'), opts, extensions)
end

# Public: Parses a Markdown string into a `document` node.
Expand Down

0 comments on commit 1d5032d

Please sign in to comment.