Skip to content

Commit e8d14d3

Browse files
committed
Fix markdown parsing for html tags inside pre code tags
1 parent d64f361 commit e8d14d3

File tree

4 files changed

+177
-123
lines changed

4 files changed

+177
-123
lines changed

lib/tilex/markdown.ex

+15-24
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@ defmodule Tilex.Markdown do
22
alias Tilex.Cache
33

44
@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{
512
code_class_prefix: "language-",
613
escape: false,
7-
pure_links: false
14+
pure_links: false,
15+
smartypants: false
816
}
917

1018
def to_html_live(markdown) do
1119
markdown
12-
|> HtmlSanitizeEx.markdown_html()
1320
|> Earmark.as_html!(@earmark_options)
14-
|> String.replace("”", "\"")
15-
|> expand_relative_links()
21+
|> HtmlSanitizeEx.html5()
1622
|> String.trim()
1723
end
1824

@@ -23,28 +29,13 @@ defmodule Tilex.Markdown do
2329
end
2430

2531
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
2937
{:ok, fragment} -> fragment |> Floki.text() |> String.trim()
3038
_error -> markdown
3139
end
3240
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)
5041
end

lib/tilex_web/router.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
defmodule TilexWeb.Router do
22
use TilexWeb, :router
3-
import Plug.BasicAuth, only: [basic_auth: 2]
43

54
@auth_controller Application.compile_env(:tilex, :auth_controller)
65
@basic_auth Application.compile_env(:tilex, :basic_auth)
76

7+
defdelegate basic_auth(conn, options \\ []), to: Plug.BasicAuth
8+
89
pipeline :browser do
910
plug Tilex.Plug.RequestRejector
1011
plug Tilex.Plug.SetCanonicalUrl

priv/repo/structure.sql

+2-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
-- PostgreSQL database dump
33
--
44

5-
-- Dumped from database version 14.5
6-
-- Dumped by pg_dump version 15.0
5+
-- Dumped from database version 15.1
6+
-- Dumped by pg_dump version 15.1
77

88
SET statement_timeout = 0;
99
SET lock_timeout = 0;
@@ -16,13 +16,6 @@ SET xmloption = content;
1616
SET client_min_messages = warning;
1717
SET row_security = off;
1818

19-
--
20-
-- Name: public; Type: SCHEMA; Schema: -; Owner: -
21-
--
22-
23-
-- *not* creating schema, since initdb creates it
24-
25-
2619
SET default_tablespace = '';
2720

2821
SET default_table_access_method = heap;

0 commit comments

Comments
 (0)