From 6d529cd005fd82f7a71d9bd32b937eeb4a45b824 Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Thu, 9 Jan 2025 13:18:10 -0500 Subject: [PATCH] chore: update comrak to v0.33.0 (#113) --- lib/mdex/document.ex | 14 +++ mix.exs | 1 + native/comrak_nif/Cargo.lock | 14 ++- native/comrak_nif/Cargo.toml | 4 +- native/comrak_nif/src/types/document.rs | 18 ++++ native/comrak_nif/src/types/options.rs | 108 +++++++++++------------- test/html_format_test.exs | 12 +++ 7 files changed, 102 insertions(+), 69 deletions(-) diff --git a/lib/mdex/document.ex b/lib/mdex/document.ex index 7569c51..9373660 100644 --- a/lib/mdex/document.ex +++ b/lib/mdex/document.ex @@ -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() @@ -295,6 +296,7 @@ defmodule MDEx.Document do MDEx.LineBreak, MDEx.Code, MDEx.HtmlInline, + MDEx.Raw, MDEx.Emph, MDEx.Strong, MDEx.Strikethrough, @@ -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. @@ -886,6 +898,7 @@ defimpl Enumerable, MDEx.LineBreak, MDEx.Code, MDEx.HtmlInline, + MDEx.Raw, MDEx.Emph, MDEx.Strong, MDEx.Strikethrough, @@ -970,6 +983,7 @@ defimpl String.Chars, MDEx.LineBreak, MDEx.Code, MDEx.HtmlInline, + MDEx.Raw, MDEx.Emph, MDEx.Strong, MDEx.Strikethrough, diff --git a/mix.exs b/mix.exs index 52de878..21c4855 100644 --- a/mix.exs +++ b/mix.exs @@ -90,6 +90,7 @@ defmodule MDEx.MixProject do MDEx.LineBreak, MDEx.Code, MDEx.HtmlInline, + MDEx.Raw, MDEx.Emph, MDEx.Strong, MDEx.Strikethrough, diff --git a/native/comrak_nif/Cargo.lock b/native/comrak_nif/Cargo.lock index e5af5f2..d27661c 100644 --- a/native/comrak_nif/Cargo.lock +++ b/native/comrak_nif/Cargo.lock @@ -207,9 +207,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.5.24" +version = "4.5.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9560b07a799281c7e0958b9296854d6fafd4c5f31444a7e5bb1ad6dde5ccf1bd" +checksum = "b95dca1b68188a08ca6af9d96a6576150f598824bdb528c1190460c2940a0b48" dependencies = [ "clap_builder", "clap_derive", @@ -217,9 +217,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.24" +version = "4.5.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "874e0dd3eb68bf99058751ac9712f622e61e6f393a94f7128fa26e3f02f5c7cd" +checksum = "9ab52925392148efd3f7562f2136a81ffb778076bcc85727c6e020d6dd57cf15" dependencies = [ "anstream", "anstyle", @@ -254,9 +254,9 @@ checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" [[package]] name = "comrak" -version = "0.31.0" +version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "453dcb42e33f7b474d7e0db12e0b8d82802c88f35cf5a1d8c297d0dfcecb154f" +checksum = "39bff2cbb80102771ca62bd2375bc6f6611dc1493373440b23aa08a155538708" dependencies = [ "bon", "caseless", @@ -264,8 +264,6 @@ dependencies = [ "emojis", "entities", "memchr", - "once_cell", - "regex", "shell-words", "slug", "syntect", diff --git a/native/comrak_nif/Cargo.toml b/native/comrak_nif/Cargo.toml index 61577c8..05841f3 100644 --- a/native/comrak_nif/Cargo.toml +++ b/native/comrak_nif/Cargo.toml @@ -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" @@ -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", @@ -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"] diff --git a/native/comrak_nif/src/types/document.rs b/native/comrak_nif/src/types/document.rs index 67851a7..c93ed72 100644 --- a/native/comrak_nif/src/types/document.rs +++ b/native/comrak_nif/src/types/document.rs @@ -44,6 +44,7 @@ pub enum NewNode { LineBreak(ExLineBreak), Code(ExCode), HtmlInline(ExHtmlInline), + Raw(ExRaw), Emph(ExEmph), Strong(ExStrong), Strikethrough(ExStrikethrough), @@ -89,6 +90,7 @@ impl From 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(), @@ -533,6 +535,18 @@ impl From for NodeValue { } } +#[derive(Debug, Clone, PartialEq, NifStruct)] +#[module = "MDEx.Raw"] +pub struct ExRaw { + pub literal: String, +} + +impl From for NodeValue { + fn from(node: ExRaw) -> Self { + NodeValue::Raw(node.literal.to_string()) + } +} + #[derive(Debug, Clone, PartialEq, NifStruct)] #[module = "MDEx.Emph"] pub struct ExEmph { @@ -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 }), diff --git a/native/comrak_nif/src/types/options.rs b/native/comrak_nif/src/types/options.rs index 4b0342e..e1f102d 100644 --- a/native/comrak_nif/src/types/options.rs +++ b/native/comrak_nif/src/types/options.rs @@ -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, + } } diff --git a/test/html_format_test.exs b/test/html_format_test.exs index b249eb3..41bff8e 100644 --- a/test/html_format_test.exs +++ b/test/html_format_test.exs @@ -379,4 +379,16 @@ defmodule MDEx.HTMLFormatTest do test "subscript" do assert_format("H~2~O", "

H2O

", subscript: true) end + + test "raw" do + ast = %MDEx.Document{ + nodes: [ + %MDEx.Raw{ + literal: "{ }" + } + ] + } + + assert MDEx.to_html(ast) == {:ok, "{ }"} + end end