Skip to content

Commit

Permalink
Because of ial we can have any attribute on a pre
Browse files Browse the repository at this point in the history
For example:

````markdown

```erlang
foo() -ok.
```

{: #new }
````

The above would create a pre with id=new which the old regex
would not match properly.
  • Loading branch information
garazdawi authored and wojtekmach committed Dec 4, 2023
1 parent 9deb0aa commit d44adf7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/ex_doc/doc_ast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ defmodule ExDoc.DocAST do
def highlight(html, language, opts \\ []) do
highlight_info = language.highlight_info()

## Html cannot be parsed with regex, but we try our best...
Regex.replace(
~r/<pre(\s+class="\w*")?><code(?:\s+class="(\w*)")?>([^<]*)<\/code><\/pre>/,
~r/<pre(\s[^>]*)?><code(?:\s+class="(\w*)")?>([^<]*)<\/code><\/pre>/,
html,
&highlight_code_block(&1, &2, &3, &4, highlight_info, opts)
)
Expand Down
12 changes: 6 additions & 6 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
%{
"earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"},
"easyhtml": {:hex, :easyhtml, "0.2.0", "0cd913f920392b5b5f74ff94c1795f8fc5c0812213ef4c466af7705348e1c05d", [:mix], [{:floki, "~> 0.30", [hex: :floki, repo: "hexpm", optional: false]}], "hexpm", "f827387bbafe03b70df5e392f5e536b579a1dfa315714fb827ffde4e620d44a9"},
"floki": {:hex, :floki, "0.34.2", "5fad07ef153b3b8ec110b6b155ec3780c4b2c4906297d0b4be1a7162d04a7e02", [:mix], [], "hexpm", "26b9d50f0f01796bc6be611ca815c5e0de034d2128e39cc9702eee6b66a4d1c8"},
"jason": {:hex, :jason, "1.2.0", "10043418c42d2493d0ee212d3fddd25d7ffe484380afad769a0a38795938e448", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "116747dbe057794c3a3e4e143b7c8390b29f634e16c78a7f59ba75bfa6852e7f"},
"easyhtml": {:hex, :easyhtml, "0.3.0", "80e729ab4bfa7572fda74d05d7e8fb6b2e01b9d828a44e75e563d0e1a258eca7", [:mix], [{:floki, "~> 0.30", [hex: :floki, repo: "hexpm", optional: false]}], "hexpm", "9929ef5c0e85812f1d6ce66587597fe81a825ae9653bb851e942ceff45123afe"},
"floki": {:hex, :floki, "0.35.2", "87f8c75ed8654b9635b311774308b2760b47e9a579dabf2e4d5f1e1d42c39e0b", [:mix], [], "hexpm", "6b05289a8e9eac475f644f09c2e4ba7e19201fd002b89c28c1293e7bd16773d9"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"},
"makeup_c": {:hex, :makeup_c, "0.1.1", "14250b1a69770b1892f4113129417a2df098e2a72b9e1477aa9096e9e6c473a6", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "89e9cf45372822d354c19a7e18d77f84cfd70e2d206ac987eb15a1b8357f2869"},
"makeup_elixir": {:hex, :makeup_elixir, "0.15.2", "dc72dfe17eb240552857465cc00cce390960d9a0c055c4ccd38b70629227e97c", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "fd23ae48d09b32eff49d4ced2b43c9f086d402ee4fd4fcb2d7fad97fa8823e75"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"makeup_html": {:hex, :makeup_html, "0.1.0", "b0228fda985e311d8f0d25bed58f8280826633a38d7448cabdd723e116165bcf", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "0ca44e7dcb8d933e010740324470dd8ec947243b51304bd34b8165ef3281edc2"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.2", "ad87296a092a46e03b7e9b0be7631ddcf64c790fa68a9ef5323b6cbb36affc72", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f3f5a1ca93ce6e092d92b6d9c049bcda58a3b617a8d888f8e7231c85630e8108"},
"makeup_html": {:hex, :makeup_html, "0.1.1", "c3d4abd39d5f7e925faca72ada6e9cc5c6f5fa7cd5bc0158315832656cf14d7f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "44f2a61bc5243645dd7fafeaa6cc28793cd22f3c76b861e066168f9a5b2c26a4"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
}
14 changes: 14 additions & 0 deletions test/ex_doc/doc_ast_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ defmodule ExDoc.DocASTTest do
) =~
~r{<pre class="wrap"><code class=\"makeup elixir\" translate="no">.*}

# Pre id
assert DocAST.highlight(
~S[<pre id="anchor"><code class="">mix run --no-halt path/to/file.exs</code></pre>],
ExDoc.Language.Elixir
) =~
~r{<pre id="anchor"><code class=\"makeup elixir\" translate="no">.*}

# Pre id and class
assert DocAST.highlight(
~S[<pre id="anchor" class="wrap"><code class="">mix run --no-halt path/to/file.exs</code></pre>],
ExDoc.Language.Elixir
) =~
~r{<pre id="anchor" class="wrap"><code class=\"makeup elixir\" translate="no">.*}

# IEx highlight with empty class
assert DocAST.highlight(
~S[<pre><code class="">iex&gt; max(4, 5)</code></pre>],
Expand Down

0 comments on commit d44adf7

Please # to comment.