Skip to content

Commit

Permalink
chore: update comrak to v0.33.0 (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp authored Jan 9, 2025
1 parent 74ae15c commit 6d529cd
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 69 deletions.
14 changes: 14 additions & 0 deletions lib/mdex/document.ex
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ defmodule MDEx.Document do
| MDEx.LineBreak.t()
| MDEx.Code.t()
| MDEx.HtmlInline.t()
| MDEx.Raw.t()
| MDEx.Emph.t()
| MDEx.Strong.t()
| MDEx.Strikethrough.t()
Expand Down Expand Up @@ -295,6 +296,7 @@ defmodule MDEx.Document do
MDEx.LineBreak,
MDEx.Code,
MDEx.HtmlInline,
MDEx.Raw,
MDEx.Emph,
MDEx.Strong,
MDEx.Strikethrough,
Expand Down Expand Up @@ -695,6 +697,16 @@ defmodule MDEx.HtmlInline do
use MDEx.Document.Access
end

defmodule MDEx.Raw do
@moduledoc """
A Raw output node. This will be inserted verbatim into CommonMark and HTML output. It can only be created programmatically, and is never parsed from input.
"""

@type t :: %__MODULE__{literal: String.t()}
defstruct literal: ""
use MDEx.Document.Access
end

defmodule MDEx.Emph do
@moduledoc """
Emphasis.
Expand Down Expand Up @@ -886,6 +898,7 @@ defimpl Enumerable,
MDEx.LineBreak,
MDEx.Code,
MDEx.HtmlInline,
MDEx.Raw,
MDEx.Emph,
MDEx.Strong,
MDEx.Strikethrough,
Expand Down Expand Up @@ -970,6 +983,7 @@ defimpl String.Chars,
MDEx.LineBreak,
MDEx.Code,
MDEx.HtmlInline,
MDEx.Raw,
MDEx.Emph,
MDEx.Strong,
MDEx.Strikethrough,
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ defmodule MDEx.MixProject do
MDEx.LineBreak,
MDEx.Code,
MDEx.HtmlInline,
MDEx.Raw,
MDEx.Emph,
MDEx.Strong,
MDEx.Strikethrough,
Expand Down
14 changes: 6 additions & 8 deletions native/comrak_nif/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions native/comrak_nif/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rustler = { version = "0.33", features = [
"serde",
] }
serde = "1.0"
comrak = { version = "0.31", features = ["shortcodes"] }
comrak = { version = "0.33", features = ["shortcodes"] }
ammonia = "4.0"
phf = { version = "0.11", features = ["macros"] }
tree-sitter = "0.20"
Expand All @@ -25,6 +25,7 @@ v_htmlescape = "0.15"
autumn = { path = "vendor/autumn" }
log = "0.4"
lazy_static = "1.5"
typed-arena = "2.0.2"
inkjet = { version = "0.10.5", default-features = false, features = [
"html",
"language-bash",
Expand Down Expand Up @@ -81,7 +82,6 @@ inkjet = { version = "0.10.5", default-features = false, features = [
"language-yaml",
"language-zig",
] }
typed-arena = "2.0.2"

[features]
default = ["nif_version_2_15"]
Expand Down
18 changes: 18 additions & 0 deletions native/comrak_nif/src/types/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub enum NewNode {
LineBreak(ExLineBreak),
Code(ExCode),
HtmlInline(ExHtmlInline),
Raw(ExRaw),
Emph(ExEmph),
Strong(ExStrong),
Strikethrough(ExStrikethrough),
Expand Down Expand Up @@ -89,6 +90,7 @@ impl From<NewNode> for NodeValue {
NewNode::LineBreak(n) => n.into(),
NewNode::Code(n) => n.into(),
NewNode::HtmlInline(n) => n.into(),
NewNode::Raw(n) => n.into(),
NewNode::Emph(n) => n.into(),
NewNode::Strong(n) => n.into(),
NewNode::Strikethrough(n) => n.into(),
Expand Down Expand Up @@ -533,6 +535,18 @@ impl From<ExHtmlInline> for NodeValue {
}
}

#[derive(Debug, Clone, PartialEq, NifStruct)]
#[module = "MDEx.Raw"]
pub struct ExRaw {
pub literal: String,
}

impl From<ExRaw> for NodeValue {
fn from(node: ExRaw) -> Self {
NodeValue::Raw(node.literal.to_string())
}
}

#[derive(Debug, Clone, PartialEq, NifStruct)]
#[module = "MDEx.Emph"]
pub struct ExEmph {
Expand Down Expand Up @@ -941,6 +955,10 @@ pub fn comrak_ast_to_ex_document<'a>(node: &'a AstNode<'a>) -> NewNode {
literal: literal.to_string(),
}),

NodeValue::Raw(ref literal) => NewNode::Raw(ExRaw {
literal: literal.to_string(),
}),

NodeValue::Emph => NewNode::Emph(ExEmph { nodes: children }),

NodeValue::Strong => NewNode::Strong(ExStrong { nodes: children }),
Expand Down
108 changes: 49 additions & 59 deletions native/comrak_nif/src/types/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,69 +91,59 @@ pub struct ExOptions {
}

pub fn extension_options_from_ex_options(options: &ExOptions) -> ExtensionOptions {
let mut extension_options = ExtensionOptions::default();

extension_options.strikethrough = options.extension.strikethrough;
extension_options.tagfilter = options.extension.tagfilter;
extension_options.table = options.extension.table;
extension_options.autolink = options.extension.autolink;
extension_options.tasklist = options.extension.tasklist;
extension_options.superscript = options.extension.superscript;
extension_options
.header_ids
.clone_from(&options.extension.header_ids);
extension_options.footnotes = options.extension.footnotes;
extension_options.description_lists = options.extension.description_lists;
extension_options
.front_matter_delimiter
.clone_from(&options.extension.front_matter_delimiter);
extension_options.multiline_block_quotes = options.extension.multiline_block_quotes;
extension_options.math_dollars = options.extension.math_dollars;
extension_options.math_code = options.extension.math_code;
extension_options.shortcodes = options.extension.shortcodes;
extension_options.wikilinks_title_after_pipe = options.extension.wikilinks_title_after_pipe;
extension_options.wikilinks_title_before_pipe = options.extension.wikilinks_title_before_pipe;
extension_options.underline = options.extension.underline;
extension_options.subscript = options.extension.subscript;
extension_options.spoiler = options.extension.spoiler;
extension_options.greentext = options.extension.greentext;

extension_options
ExtensionOptions::<'_> {
strikethrough: options.extension.strikethrough,
tagfilter: options.extension.tagfilter,
table: options.extension.table,
autolink: options.extension.autolink,
tasklist: options.extension.tasklist,
superscript: options.extension.superscript,
header_ids: options.extension.header_ids.clone(),
footnotes: options.extension.footnotes,
description_lists: options.extension.description_lists,
front_matter_delimiter: options.extension.front_matter_delimiter.clone(),
multiline_block_quotes: options.extension.multiline_block_quotes,
math_dollars: options.extension.math_dollars,
math_code: options.extension.math_code,
shortcodes: options.extension.shortcodes,
wikilinks_title_after_pipe: options.extension.wikilinks_title_after_pipe,
wikilinks_title_before_pipe: options.extension.wikilinks_title_before_pipe,
underline: options.extension.underline,
subscript: options.extension.subscript,
spoiler: options.extension.spoiler,
greentext: options.extension.greentext,
..Default::default()
}
}

pub fn parse_options_from_ex_options(options: &ExOptions) -> ParseOptions {
let mut parse_options = ParseOptions::default();

parse_options.smart = options.parse.smart;
parse_options
.default_info_string
.clone_from(&options.parse.default_info_string);
parse_options.relaxed_tasklist_matching = options.parse.relaxed_tasklist_matching;
parse_options.relaxed_autolinks = options.parse.relaxed_autolinks;

parse_options
ParseOptions::<'_> {
smart: options.parse.smart,
default_info_string: options.parse.default_info_string.clone(),
relaxed_tasklist_matching: options.parse.relaxed_tasklist_matching,
relaxed_autolinks: options.parse.relaxed_autolinks,
..Default::default()
}
}

pub fn render_options_from_ex_options(options: &ExOptions) -> RenderOptions {
let mut render_options = RenderOptions::default();

render_options.hardbreaks = options.render.hardbreaks;
render_options.github_pre_lang = options.render.github_pre_lang;
render_options.full_info_string = options.render.full_info_string;
render_options.width = options.render.width;
render_options.unsafe_ = options.render.unsafe_;
render_options.escape = options.render.escape;
render_options.list_style = ListStyleType::from(options.render.list_style.clone());
render_options.sourcepos = options.render.sourcepos;
render_options.experimental_inline_sourcepos = options.render.experimental_inline_sourcepos;
render_options.escaped_char_spans = options.render.escaped_char_spans;
render_options.ignore_setext = options.render.ignore_setext;
render_options.ignore_empty_links = options.render.ignore_empty_links;
render_options.gfm_quirks = options.render.gfm_quirks;
render_options.prefer_fenced = options.render.prefer_fenced;
render_options.figure_with_caption = options.render.figure_with_caption;
render_options.tasklist_classes = options.render.tasklist_classes;
render_options.ol_width = options.render.ol_width;

render_options
RenderOptions {
hardbreaks: options.render.hardbreaks,
github_pre_lang: options.render.github_pre_lang,
full_info_string: options.render.full_info_string,
width: options.render.width,
unsafe_: options.render.unsafe_,
escape: options.render.escape,
list_style: ListStyleType::from(options.render.list_style.clone()),
sourcepos: options.render.sourcepos,
experimental_inline_sourcepos: options.render.experimental_inline_sourcepos,
escaped_char_spans: options.render.escaped_char_spans,
ignore_setext: options.render.ignore_setext,
ignore_empty_links: options.render.ignore_empty_links,
gfm_quirks: options.render.gfm_quirks,
prefer_fenced: options.render.prefer_fenced,
figure_with_caption: options.render.figure_with_caption,
tasklist_classes: options.render.tasklist_classes,
ol_width: options.render.ol_width,
}
}
12 changes: 12 additions & 0 deletions test/html_format_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,16 @@ defmodule MDEx.HTMLFormatTest do
test "subscript" do
assert_format("H~2~O", "<p>H<sub>2</sub>O</p>", subscript: true)
end

test "raw" do
ast = %MDEx.Document{
nodes: [
%MDEx.Raw{
literal: "&lbrace; <!-- literal --> &rbrace;"
}
]
}

assert MDEx.to_html(ast) == {:ok, "&lbrace; <!-- literal --> &rbrace;"}
end
end

0 comments on commit 6d529cd

Please sign in to comment.