Skip to content

Commit

Permalink
Closes #42
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei Matiushkin committed May 21, 2024
1 parent 051efbc commit 95866dd
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Instead of `@syntax` module attribute, one might use

### Changelog

- **`0.10.4`** fix pairs with special symbols inside
- **`0.10.2`** spaces as delimiters in attributes
- **`0.10.1`** support for the flaky `floki` update
- **`0.10.0`** [:tag:] attributes for tags
Expand Down
15 changes: 11 additions & 4 deletions lib/md/parser/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,10 @@ defmodule Md.Engine do
<<x::utf8, delim::utf8, rest::binary>>,
%Md.Parser.State{
bag: %{stock: [stock, unquote(md)]},
mode: [mode | _]
mode: [:magnet | _]
} = state
)
when mode == :magnet and is_utf8_space(delim) do
when is_utf8_space(delim) do
{pre, post, delim} =
case unquote(greedy) do
:left -> {unquote(md), "", <<delim::utf8>>}
Expand Down Expand Up @@ -439,8 +439,15 @@ defmodule Md.Engine do
do_parse(rest, state)
end

Enum.each(magnets, fn {md, _properties} ->
defp do_parse(unquote(md) <> rest, state()) when mode not in [:raw, {:inner, :raw}] do
Enum.each(magnets, fn {md, properties} ->
ignore_in = properties |> Map.get(:ignore_in) |> List.wrap()

defp do_parse(
unquote(md) <> rest,
%Md.Parser.State{mode: [mode | _], path: path} = state
)
when mode not in [:raw, {:inner, :raw}] and
(path == [] or elem(hd(path), 0) not in unquote(ignore_in)) do
state =
%Md.Parser.State{state | bag: %{state.bag | stock: ["", unquote(md)]}}
|> push_mode(:magnet)
Expand Down
39 changes: 34 additions & 5 deletions lib/md/parser/syntax/default.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,40 @@ defmodule Md.Parser.Syntax.Default do
],
magnet: [
# {"⚓", %{transform: Anchor, terminators: []}},
{"[^", %{transform: Footnote, terminators: [?\]], greedy: true}},
{"@", %{transform: &TwitterHandle.apply/2, terminators: [?,, ?., ?!, ??, ?:, ?;]}},
{"https://", %{transform: Anchor, terminators: [], greedy: :left}},
{"http://", %{transform: Anchor, terminators: [], greedy: :left}},
{"✇", %{transform: &Youtube.apply/2, terminators: [], greedy: :left}}
{"[^",
%{
transform: Footnote,
terminators: [?\]],
greedy: true,
ignore_in: [:img, :a, :figure, :abbr]
}},
{"@",
%{
transform: &TwitterHandle.apply/2,
terminators: [?,, ?., ?!, ??, ?:, ?;, ?[, ?], ?(, ?)],
ignore_in: [:img, :a, :figure, :abbr]
}},
{"https://",
%{
transform: Anchor,
terminators: [],
greedy: :left,
ignore_in: [:img, :a, :figure, :abbr]
}},
{"http://",
%{
transform: Anchor,
terminators: [],
greedy: :left,
ignore_in: [:img, :a, :figure, :abbr]
}},
{"✇",
%{
transform: &Youtube.apply/2,
terminators: [],
greedy: :left,
ignore_in: [:img, :a, :figure, :abbr]
}}
],
block: [
{"```", %{tag: [:pre, :code], pop: %{code: [attribute: :class, prefixes: ["", "lang-"]]}}}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Md.MixProject do
use Mix.Project

@app :md
@version "0.10.3"
@version "0.10.4"

def project do
[
Expand Down
3 changes: 3 additions & 0 deletions test/md_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ defmodule MdTest do
assert [
{:p, nil, [{:a, %{href: "https://twitter.com/%40%40bar"}, ["@@@bar"]}]}
] == Md.parse("@@@bar").ast

assert [{:a, %{href: "mailto:[email protected]"}, ["[email protected]"]}] ==
Md.parse("[[email protected]](mailto:[email protected])").ast
end

test "flush" do
Expand Down

0 comments on commit 95866dd

Please sign in to comment.