refactor(Blogex.LinkValidator): simplify dead code and naming
- Drop redundant :ok return in _validate_links/2 (blog.ex)
- Remove dead HTML link regex from extract_links/1 (body is raw markdown)
- Rename slug_slug_end/1 to slug_end/1
- Simplify parse_blog_link/1 to return {blog_id, slug}, removing
parse_query_fragment/1 and dead case branches
This commit is contained in:
@@ -5,7 +5,7 @@ defmodule Blogex.LinkValidatorTest do
|
||||
describe "extract_links/1" do
|
||||
test "extracts internal blog links from markdown body" do
|
||||
body =
|
||||
"<p>Check out [hello world](/blog/engineering/hello-world) and [release v1](/blog/releases/v1-0-0).</p>"
|
||||
"Check out [hello world](/blog/engineering/hello-world) and [release v1](/blog/releases/v1-0-0)."
|
||||
|
||||
assert LinkValidator.extract_links(body) == [
|
||||
"/blog/engineering/hello-world",
|
||||
@@ -14,25 +14,25 @@ defmodule Blogex.LinkValidatorTest do
|
||||
end
|
||||
|
||||
test "ignores external links" do
|
||||
body = "<p>See [GitHub](https://github.com) and [internal](/blog/engineering/post).</p>"
|
||||
body = "See [GitHub](https://github.com) and [internal](/blog/engineering/post)."
|
||||
|
||||
assert LinkValidator.extract_links(body) == ["/blog/engineering/post"]
|
||||
end
|
||||
|
||||
test "ignores non-blog internal links" do
|
||||
body = "<p>See [/about](/about) and [/blog/engineering/post](/blog/engineering/post).</p>"
|
||||
body = "See [/about](/about) and [/blog/engineering/post](/blog/engineering/post)."
|
||||
|
||||
assert LinkValidator.extract_links(body) == ["/blog/engineering/post"]
|
||||
end
|
||||
|
||||
test "returns empty list when no internal blog links" do
|
||||
body = "<p>Just external links: [GitHub](https://github.com).</p>"
|
||||
body = "Just external links: [GitHub](https://github.com)."
|
||||
|
||||
assert LinkValidator.extract_links(body) == []
|
||||
end
|
||||
|
||||
test "handles multiple links on one line" do
|
||||
body = "<p>[a](/blog/engineering/a) [b](/blog/releases/b) [c](/blog/engineering/c)</p>"
|
||||
body = "[a](/blog/engineering/a) [b](/blog/releases/b) [c](/blog/engineering/c)"
|
||||
|
||||
assert LinkValidator.extract_links(body) == [
|
||||
"/blog/engineering/a",
|
||||
@@ -42,13 +42,13 @@ defmodule Blogex.LinkValidatorTest do
|
||||
end
|
||||
|
||||
test "handles links with query strings" do
|
||||
body = "<p>[link](/blog/engineering/post?foo=bar)</p>"
|
||||
body = "[link](/blog/engineering/post?foo=bar)"
|
||||
|
||||
assert LinkValidator.extract_links(body) == ["/blog/engineering/post?foo=bar"]
|
||||
end
|
||||
|
||||
test "handles links with anchor fragments" do
|
||||
body = "<p>[link](/blog/engineering/post#section)</p>"
|
||||
body = "[link](/blog/engineering/post#section)"
|
||||
|
||||
assert LinkValidator.extract_links(body) == ["/blog/engineering/post#section"]
|
||||
end
|
||||
@@ -190,19 +190,19 @@ defmodule Blogex.LinkValidatorTest do
|
||||
|
||||
describe "validate_body/2" do
|
||||
test "returns :ok when body has no internal blog links" do
|
||||
body = "<p>Just text, no links.</p>"
|
||||
body = "Just text, no links."
|
||||
|
||||
assert LinkValidator.validate_body(body, :engineering) == :ok
|
||||
end
|
||||
|
||||
test "returns :ok when all links are valid" do
|
||||
body = "<p>[link](/blog/engineering/post)</p>"
|
||||
body = "[link](/blog/engineering/post)"
|
||||
|
||||
assert LinkValidator.validate_body(body, :engineering) == :ok
|
||||
end
|
||||
|
||||
test "returns errors with post context" do
|
||||
body = "<p>[link](/blog/unknown/post)</p>"
|
||||
body = "[link](/blog/unknown/post)"
|
||||
|
||||
assert LinkValidator.validate_body(body, :engineering) == {
|
||||
:error,
|
||||
@@ -218,7 +218,7 @@ defmodule Blogex.LinkValidatorTest do
|
||||
end
|
||||
|
||||
test "includes post_id in error tuples when provided" do
|
||||
body = "<p>[link](/blog/unknown/post)</p>"
|
||||
body = "[link](/blog/unknown/post)"
|
||||
|
||||
assert LinkValidator.validate_body(body, :engineering, post_id: "test-post") == {
|
||||
:error,
|
||||
|
||||
Reference in New Issue
Block a user