Skip to content

Commit

Permalink
[#92] Add support for image links
Browse files Browse the repository at this point in the history
Problem: We should add support for image links.

Solution: Extract image links as regular links.
  • Loading branch information
Sereja313 committed Sep 29, 2022
1 parent c5096e5 commit 5bf655d
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Unreleased
+ Fixed bug when we reported footnotes as broken links
* [#163](https://github.com/serokell/xrefcheck/pull/163)
+ Fixed an issue where the progress bar thread might be unexpectedly cancelled and jumble up the output.
* [#183](https://github.com/serokell/xrefcheck/pull/183)
+ Add support for image links.

0.2.1
==========
Expand Down
27 changes: 16 additions & 11 deletions src/Xrefcheck/Scanners/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,25 @@ nodeExtractInfo fp input@(Node _ _ nSubs) = do
Nothing -> do
return mempty

LINK url _ -> do
let rName = nodeExtractText node
rPos = toPosition pos
link = if null url then rName else url
let (rLink, rAnchor) = case T.splitOn "#" link of
[t] -> (t, Nothing)
t : ts -> (t, Just $ T.intercalate "#" ts)
[] -> error "impossible"
return $ FileInfoDiff
(DList.singleton $ Reference {rName, rPos, rLink, rAnchor})
DList.empty
LINK url _ -> extractLink url

IMAGE url _ -> extractLink url

_ -> return mempty

where
extractLink url = do
let rName = nodeExtractText node
rPos = toPosition pos
link = if null url then rName else url
let (rLink, rAnchor) = case T.splitOn "#" link of
[t] -> (t, Nothing)
t : ts -> (t, Just $ T.intercalate "#" ts)
[] -> error "impossible"
return $ FileInfoDiff
(DList.singleton $ Reference {rName, rPos, rLink, rAnchor})
DList.empty

-- | Check if there is `ignore file` at the beginning of the file,
-- ignoring preceding comments if there are any.
checkIgnoreFile :: [Node] -> Bool
Expand Down
16 changes: 16 additions & 0 deletions tests/golden/check-images/check-images.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bats

# SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io>
#
# SPDX-License-Identifier: MPL-2.0

load '../helpers/bats-support/load'
load '../helpers/bats-assert/load'
load '../helpers/bats-file/load'
load '../helpers'

@test "Check images" {
to_temp xrefcheck -v

assert_diff expected.gold
}
29 changes: 29 additions & 0 deletions tests/golden/check-images/check-images.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--
- SPDX-FileCopyrightText: 2021 Serokell <https://serokell.io>
-
- SPDX-License-Identifier: MPL-2.0
-->

![good image ref 1](https://avatars.githubusercontent.com/u/13840520 "text")

![good image ref 2][img-ref-good-2]

[img-ref-good-2]: https://avatars.githubusercontent.com/u/13840520 "text"

![good image ref 3](./serokell.png "text")

![good image ref 4][img-ref-good-4]

[img-ref-good-4]: ./serokell.png "text"


![bad image ref 1](https://serokell.io/1.png "text")

![bad image ref 2][img-ref-bad-2]

[img-ref-bad-2]: https://serokell.io/2.png "text"

![bad image ref 3](./3.png "text")
![bad image ref 4][img-ref-bad-4]

[img-ref-bad-4]: ./4.png "text"
82 changes: 82 additions & 0 deletions tests/golden/check-images/expected.gold
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
=== Repository data ===

⮚ check-images.md:
- references:
- reference (external) at src:7:1-76:
- text: "good image ref 1"
- link: https://avatars.githubusercontent.com/u/13840520
- anchor: -
- reference (external) at src:9:1-35:
- text: "good image ref 2"
- link: https://avatars.githubusercontent.com/u/13840520
- anchor: -
- reference (relative) at src:13:1-42:
- text: "good image ref 3"
- link: ./serokell.png
- anchor: -
- reference (relative) at src:15:1-35:
- text: "good image ref 4"
- link: ./serokell.png
- anchor: -
- reference (external) at src:20:1-52:
- text: "bad image ref 1"
- link: https://serokell.io/1.png
- anchor: -
- reference (external) at src:22:1-33:
- text: "bad image ref 2"
- link: https://serokell.io/2.png
- anchor: -
- reference (relative) at src:26:1-34:
- text: "bad image ref 3"
- link: ./3.png
- anchor: -
- reference (relative) at src:27:1-33:
- text: "bad image ref 4"
- link: ./4.png
- anchor: -
- anchors: []




=== Invalid references found ===

➥ In file check-images.md
bad reference (external) at src:20:1-52:
- text: "bad image ref 1"
- link: https://serokell.io/1.png
- anchor: -

⛂ Resource unavailable (404 Not Found)


➥ In file check-images.md
bad reference (external) at src:22:1-33:
- text: "bad image ref 2"
- link: https://serokell.io/2.png
- anchor: -

⛂ Resource unavailable (404 Not Found)


➥ In file check-images.md
bad reference (relative) at src:26:1-34:
- text: "bad image ref 3"
- link: ./3.png
- anchor: -

⛀ File does not exist:
3.png


➥ In file check-images.md
bad reference (relative) at src:27:1-33:
- text: "bad image ref 4"
- link: ./4.png
- anchor: -

⛀ File does not exist:
4.png


Invalid references dumped, 4 in total.
Binary file added tests/golden/check-images/serokell.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5bf655d

Please sign in to comment.