Skip to content

Commit

Permalink
Fix markdown parsing for html tags inside pre code tags
Browse files Browse the repository at this point in the history
  • Loading branch information
vnegrisolo committed Feb 3, 2023
1 parent d64f361 commit e8d14d3
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 123 deletions.
39 changes: 15 additions & 24 deletions lib/tilex/markdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ defmodule Tilex.Markdown do
alias Tilex.Cache

@earmark_options %Earmark.Options{
code_class_prefix: "language-",
escape: true,
pure_links: false,
smartypants: false
}

@content_earmark_options %Earmark.Options{
code_class_prefix: "language-",
escape: false,
pure_links: false
pure_links: false,
smartypants: false
}

def to_html_live(markdown) do
markdown
|> HtmlSanitizeEx.markdown_html()
|> Earmark.as_html!(@earmark_options)
|> String.replace("”", "\"")
|> expand_relative_links()
|> HtmlSanitizeEx.html5()
|> String.trim()
end

Expand All @@ -23,28 +29,13 @@ defmodule Tilex.Markdown do
end

def to_content(markdown) do
html = to_html_live(markdown)

case Floki.parse_fragment(html) do
markdown
|> Earmark.as_html!(@content_earmark_options)
|> HtmlSanitizeEx.html5()
|> Floki.parse_fragment()
|> case do
{:ok, fragment} -> fragment |> Floki.text() |> String.trim()
_error -> markdown
end
end

defp expand_relative_links(dom) do
{:ok, fragment} = Floki.parse_fragment(dom)

fragment
|> Floki.find_and_update("a", &expand_relative_link/1)
|> Floki.raw_html()
end

defp expand_relative_link({"a", attrs}), do: {"a", Enum.map(attrs, &expand_link_attribute/1)}
defp expand_relative_link({tag_name, attrs}), do: {tag_name, attrs}

defp expand_link_attribute({"href", "http" <> _rest} = attr), do: attr
defp expand_link_attribute({"href", value}), do: {"href", base_url() <> value}
defp expand_link_attribute(attr), do: attr

defp base_url(), do: Application.get_env(:tilex, :canonical_domain)
end
3 changes: 2 additions & 1 deletion lib/tilex_web/router.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
defmodule TilexWeb.Router do
use TilexWeb, :router
import Plug.BasicAuth, only: [basic_auth: 2]

@auth_controller Application.compile_env(:tilex, :auth_controller)
@basic_auth Application.compile_env(:tilex, :basic_auth)

defdelegate basic_auth(conn, options \\ []), to: Plug.BasicAuth

pipeline :browser do
plug Tilex.Plug.RequestRejector
plug Tilex.Plug.SetCanonicalUrl
Expand Down
11 changes: 2 additions & 9 deletions priv/repo/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
-- PostgreSQL database dump
--

-- Dumped from database version 14.5
-- Dumped by pg_dump version 15.0
-- Dumped from database version 15.1
-- Dumped by pg_dump version 15.1

SET statement_timeout = 0;
SET lock_timeout = 0;
Expand All @@ -16,13 +16,6 @@ SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- Name: public; Type: SCHEMA; Schema: -; Owner: -
--

-- *not* creating schema, since initdb creates it


SET default_tablespace = '';

SET default_table_access_method = heap;
Expand Down
Loading

0 comments on commit e8d14d3

Please sign in to comment.