@@ -2,17 +2,23 @@ defmodule Tilex.Markdown do
2
2
alias Tilex.Cache
3
3
4
4
@ earmark_options % Earmark.Options {
5
+ code_class_prefix: "language-" ,
6
+ escape: true ,
7
+ pure_links: false ,
8
+ smartypants: false
9
+ }
10
+
11
+ @ content_earmark_options % Earmark.Options {
5
12
code_class_prefix: "language-" ,
6
13
escape: false ,
7
- pure_links: false
14
+ pure_links: false ,
15
+ smartypants: false
8
16
}
9
17
10
18
def to_html_live ( markdown ) do
11
19
markdown
12
- |> HtmlSanitizeEx . markdown_html ( )
13
20
|> Earmark . as_html! ( @ earmark_options )
14
- |> String . replace ( "”" , "\" " )
15
- |> expand_relative_links ( )
21
+ |> HtmlSanitizeEx . html5 ( )
16
22
|> String . trim ( )
17
23
end
18
24
@@ -23,28 +29,13 @@ defmodule Tilex.Markdown do
23
29
end
24
30
25
31
def to_content ( markdown ) do
26
- html = to_html_live ( markdown )
27
-
28
- case Floki . parse_fragment ( html ) do
32
+ markdown
33
+ |> Earmark . as_html! ( @ content_earmark_options )
34
+ |> HtmlSanitizeEx . html5 ( )
35
+ |> Floki . parse_fragment ( )
36
+ |> case do
29
37
{ :ok , fragment } -> fragment |> Floki . text ( ) |> String . trim ( )
30
38
_error -> markdown
31
39
end
32
40
end
33
-
34
- defp expand_relative_links ( dom ) do
35
- { :ok , fragment } = Floki . parse_fragment ( dom )
36
-
37
- fragment
38
- |> Floki . find_and_update ( "a" , & expand_relative_link / 1 )
39
- |> Floki . raw_html ( )
40
- end
41
-
42
- defp expand_relative_link ( { "a" , attrs } ) , do: { "a" , Enum . map ( attrs , & expand_link_attribute / 1 ) }
43
- defp expand_relative_link ( { tag_name , attrs } ) , do: { tag_name , attrs }
44
-
45
- defp expand_link_attribute ( { "href" , "http" <> _rest } = attr ) , do: attr
46
- defp expand_link_attribute ( { "href" , value } ) , do: { "href" , base_url ( ) <> value }
47
- defp expand_link_attribute ( attr ) , do: attr
48
-
49
- defp base_url ( ) , do: Application . get_env ( :tilex , :canonical_domain )
50
41
end
0 commit comments