comrak (CommonMark 解析器) Rust 库的 Ruby 封装
Ruby wrapper for Rust's comrak crate.
It passes all of the CommonMark test suite, and is therefore spec-complete. It also includes extensions to the CommonMark spec as documented in the GitHub Flavored Markdown spec, such as support for tables, strikethroughs, and autolinking.
[!NOTE] By default, the following extensions are enabled for end user convenience:
strikethrough,tagfilter,table,autolink,tasklist(all from the GFM spec), andshortcodes. Thesyntax_highlighterplugin is also enabled by default, using the"base16-ocean.dark"theme.For more information on the available options and extensions, see the documentation below.
Add this line to your application's Gemfile:
gem 'commonmarker'And then execute:
$ bundleOr install it yourself as:
$ gem install commonmarkerThis gem expects to receive UTF-8 strings. Ensure your strings are the right encoding before passing them into Commonmarker.
Call to_html on a string to convert it to HTML:
require 'commonmarker'
Commonmarker.to_html('"Hi *there*"', options: {
parse: { smart: true }
})
# =>
“Hi there”
\n(The second argument is optional--see below for more information.)
You can also parse a string to receive a :document node. You can then print that node to HTML, iterate over the children, and do other fun node stuff. For example:
require 'commonmarker'
doc = Commonmarker.parse("*Hello* world", options: {
parse: { smart: true }
})
puts(doc.to_html) # =>
Hello world
\n
doc.walk do |node|
puts node.type # => [:document, :paragraph, :emph, :text, :text]
end(The second argument is optional--see below for more information.)
When it comes to modifying the document, you can perform the following operations:
insert_beforeinsert_afterprepend_childappend_childdeleteYou can also get the source position of a node by calling source_position:
doc = Commonmarker.parse("*Hello* world")
puts doc.first_child.first_child.source_position
# => {:start_line=>1, :start_column=>1, :end_line=>1, :end_column=>7}You can also modify the following attributes:
urltitleheader_levellist_typelist_startlist_tightfence_infoalert_typeYou can use walk or each to iterate over nodes:
walk will iterate on a node and recursively iterate on a node's children.each will iterate on a node's direct children, but no further.require 'commonmarker'
# parse some string
doc = Commonmarker.parse("# The site\n\n [GitHub](https://www.github.com)")
# Walk tree and print out URLs for links
doc.walk do |node|
if node.type == :link
printf("URL = %s\n", node.url)
end
end
# => URL = https://www.github.com
# Transform links to regular text
doc.walk do |node|
if node.type == :link
node.insert_before(node.first_child)
node.delete
end
end
# =>
The site
\n
GitHub
\nYou can use to_commonmark on a node to render it as raw text:
require 'commonmarker'
# parse some string
doc = Commonmarker.parse("# The site\n\n [GitHub](https://www.github.com)")
# Transform links to regular text
doc.walk do |node|
if node.type == :link
node.insert_before(node.first_child)
node.delete
end
end
doc.to_commonmark
# => # The site\n\nGitHub\nstring_content reads and writes the text of nodes whose content is plain text (like :text, :code, and :code_block).
literal reads and writes the raw string a node carries, for every node type that has one (like :text, :code, :code_block, :html_block, :html_inline, :raw, :math, and :frontmatter).
The two exist separately because they mean different things. Rewriting string_content only ever swaps text for text. Rewriting literal on an :html_block, :html_inline, or :raw node writes markup that is emitted unescaped:
doc = Commonmarker.parse("A bold claim")
doc.walk do |node|
node.literal = "" if node.type == :html_inline && node.literal == ""
endNote that a :frontmatter node's literal includes its delimiters and trailing newlines, so anything you assign must include them too.
Commonmarker accepts the same parse, render, and extensions options that comrak does, as a hash dictionary with symbol keys:
Commonmarker.to_html('"Hi *there*"', options:{
parse: { smart: true },
render: { hardbreaks: false}
})Note that there is a distinction in comrak for "parse" options and "render" options, which are represented in the tables below. As well, if you wish to disable any-non boolean option, pass in nil.
| Name | Description | Default |
|---|---|---|
smart |
Punctuation (quotes, full-stops and hyphens) are converted into 'smart' punctuation. | false |
default_info_string |
The default info string for fenced code blocks. | "" |
relaxed_tasklist_matching |
Enables relaxing of the tasklist extension matching, allowing any non-space to be used for the "checked" state instead of only x and X. |
false |
relaxed_autolinks |
Enable relaxing of the autolink extension parsing, allowing links to be recognized when in brackets, as well as permitting any url scheme. | false |
leave_footnote_definitions |
Allow footnote definitions to remain in their original positions instead of being moved to the document's end (only affects AST) | false |
ignore_setext |
Ignores setext-style headings. | false |
sourcepos_chars |
Use character-based column tracking in source positions instead of byte-based. Relevant for multi-byte UTF-8 documents with sourcepos. |
false |
| Name | Description | Default |
|---|---|---|
hardbreaks |
Soft line breaks translate into hard line breaks. | true |
github_pre_lang |
GitHub-style `` is used for fenced code blocks with info tags. | true |
full_info_string |
Gives info string data after a space in a data-meta attribute on code blocks. |
false |
width |
The wrap column when outputting CommonMark. | 80 |
unsafe |
Allow rendering of raw HTML and potentially dangerous links. | false |
escape |
Escape raw HTML instead of clobbering it. | false |
sourcepos |
Include source position attribute in HTML and XML output. | false |
escaped_char_spans |
Wrap escaped characters in span tags. | true |
ignore_empty_links |
Ignores empty links, leaving the Markdown text in place. | false |
gfm_quirks |
Outputs HTML with GFM-style quirks; namely, not nesting `` inlines. | false |
prefer_fenced |
Always output fenced code blocks, even where an indented one could be used. | false |
tasklist_classes |
Add CSS classes to the HTML output of the tasklist extension | false |
compact_html |
Suppress newlines in pretty-printed HTML output. | false |
alert_style |
The style of alert output: "specific" () or `"semantic"` (). |
"specific" |
As well, there are several extensions which you can toggle in the same manner:
Commonmarker.to_html('"Hi *there*"', options: {
extension: { footnotes: true, description_lists: true },
render: { hardbreaks: false }
})| Name | Description | Default |
|---|---|---|
strikethrough |
Enables the strikethrough extension from the GFM spec. | true |
tagfilter |
Enables the tagfilter extension from the GFM spec. | true |
table |
Enables the table extension from the GFM spec. | true |
autolink |
Enables the autolink extension from the GFM spec. | true |
tasklist |
Enables the task list extension from the GFM spec. | true |
superscript |
Enables the superscript Comrak extension. | false |
header_ids |
Enables the header IDs Comrak extension. from the GFM spec. | "" |
header_id_prefix_in_href |
Also add the prefix to generated href attributes pointing to headers. |
false |
footnotes |
Enables the footnotes extension per cmark-gfm. |
false |
inline_footnotes |
Enables the inline footnotes extension. | false |
description_lists |
Enables the description lists extension. | false |
front_matter_delimiter |
Enables the front matter extension. | "" |
multiline_block_quotes |
Enables the multiline block quotes extension. | false |
| `math_dollars |
暂无开放 Issues,或尚未同步最近议题。