From 1740be676a430b5689cece3a18f64440105a6c25 Mon Sep 17 00:00:00 2001 From: Sergey Gulin Date: Thu, 29 Sep 2022 19:11:42 +1000 Subject: [PATCH] [#92] Add support for image links Problem: We should add support for image links. Solution: Extract image links as regular links. --- CHANGES.md | 2 + src/Xrefcheck/Scanners/Markdown.hs | 40 ++++++----- tests/golden/check-images/check-images.bats | 16 +++++ tests/golden/check-images/check-images.md | 26 ++++++++ tests/golden/check-images/expected.gold | 74 +++++++++++++++++++++ 5 files changed, 142 insertions(+), 16 deletions(-) create mode 100644 tests/golden/check-images/check-images.bats create mode 100644 tests/golden/check-images/check-images.md create mode 100644 tests/golden/check-images/expected.gold diff --git a/CHANGES.md b/CHANGES.md index 8aa7c4ef..253758d2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 ========== diff --git a/src/Xrefcheck/Scanners/Markdown.hs b/src/Xrefcheck/Scanners/Markdown.hs index d50c74f1..da58d735 100644 --- a/src/Xrefcheck/Scanners/Markdown.hs +++ b/src/Xrefcheck/Scanners/Markdown.hs @@ -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 @@ -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 @@ -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 diff --git a/tests/golden/check-images/check-images.bats b/tests/golden/check-images/check-images.bats new file mode 100644 index 00000000..774df07e --- /dev/null +++ b/tests/golden/check-images/check-images.bats @@ -0,0 +1,16 @@ +#!/usr/bin/env bats + +# SPDX-FileCopyrightText: 2022 Serokell +# +# 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 +} diff --git a/tests/golden/check-images/check-images.md b/tests/golden/check-images/check-images.md new file mode 100644 index 00000000..38d50e72 --- /dev/null +++ b/tests/golden/check-images/check-images.md @@ -0,0 +1,26 @@ + + +![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" + + +![bad image ref ignored](./3.png "text") diff --git a/tests/golden/check-images/expected.gold b/tests/golden/check-images/expected.gold new file mode 100644 index 00000000..021f83b0 --- /dev/null +++ b/tests/golden/check-images/expected.gold @@ -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.