Skip to content

Commit

Permalink
Merge pull request #183 from serokell/Sereja313/#92-add-support-for-i…
Browse files Browse the repository at this point in the history
…mage-links

[#92] Add support for image links
  • Loading branch information
Sereja313 authored Oct 25, 2022
2 parents f43dd94 + 1740be6 commit eafff47
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Unreleased
+ Reorganize top-level config keys.
* [#178](https://github.com/serokell/xrefcheck/pull/178)
+ Rename exclusion-related config options.
* [#183](https://github.com/serokell/xrefcheck/pull/183)
+ Add support for image links.

0.2.2
==========
Expand Down
40 changes: 24 additions & 16 deletions src/Xrefcheck/Scanners/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ removeIgnored fp = withIgnoreMode . cataNodeWithParentNodeInfo remove
-- We expect to find a paragraph immediately after the
-- `ignore paragraph` annotanion. If the paragraph is not
-- found we should report an error.
(IMSParagraph, PARAGRAPH) -> (ssIgnore .= Nothing) $> defNode
(IMSParagraph, x) -> do
(IMSParagraph, PARAGRAPH) -> (ssIgnore .= Nothing) $> defNode
(IMSParagraph, x) -> do
lift . tell . makeError modePos fp . ParagraphErr $ prettyType x
ssIgnore .= Nothing
Node pos ty <$> sequence subs
Expand All @@ -182,15 +182,18 @@ removeIgnored fp = withIgnoreMode . cataNodeWithParentNodeInfo remove
-- since that annotation should be at the top of the file and
-- the file should already be ignored when `checkIgnoreFile` is called.
-- We should report an error if we find it anyway.
(IMSFile, _) -> do
(IMSFile, _) -> do
lift . tell $ makeError modePos fp FileErr
ssIgnore .= Nothing
Node pos ty <$> sequence subs

(IMSLink _, LINK {}) -> do
(IMSLink _, LINK {}) -> do
ssIgnore .= Nothing
return defNode
(IMSLink ignoreLinkState, _) -> do
(IMSLink _, IMAGE {}) -> do
ssIgnore .= Nothing
return defNode
(IMSLink ignoreLinkState, _) -> do
when (ignoreLinkState == ExpectingLinkInSubnodes) $
ssIgnore . _Just . ignoreMode .= IMSLink ParentExpectsLink
node' <- Node pos ty <$> sequence subs
Expand Down Expand Up @@ -314,20 +317,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
}
26 changes: 26 additions & 0 deletions tests/golden/check-images/check-images.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!--
- 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"


![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"

<!-- xrefcheck: ignore link -->
![bad image ref ignored](./3.png "text")
74 changes: 74 additions & 0 deletions tests/golden/check-images/expected.gold
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
=== 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 (external) at src:14:1-52:
- text: "bad image ref 1"
- link: https://serokell.io/1.png
- anchor: -
- reference (external) at src:16:1-33:
- text: "bad image ref 2"
- link: https://serokell.io/2.png
- anchor: -
- reference (relative) at src:20:1-34:
- text: "bad image ref 3"
- link: ./3.png
- anchor: -
- reference (relative) at src:21: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:14: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:16: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:20: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:21:1-33:
- text: "bad image ref 4"
- link: ./4.png
- anchor: -

⛀ File does not exist:
4.png


Invalid references dumped, 4 in total.

0 comments on commit eafff47

Please sign in to comment.