Skip to content

Commit

Permalink
chore: update comrak to v0.33.0
Browse files Browse the repository at this point in the history
- added the Raw node
  • Loading branch information
leandrocp committed Jan 9, 2025
1 parent 74ae15c commit b32e3b0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 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
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 b32e3b0

Please sign in to comment.