Skip to content

Commit

Permalink
Fix URL
Browse files Browse the repository at this point in the history
`path.is_relative` return true for links like https://example.com.

So we have to check if the target of link and image
is a path to local file before we update them.
  • Loading branch information
black-desk committed Oct 12, 2024
1 parent 364394e commit f671f7e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
17 changes: 17 additions & 0 deletions expected-auto.native
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,29 @@
( "subdir/someimage.png" , "" )
]
]
, Figure
( "" , [] , [] )
(Caption
Nothing [ Plain [ Str "Image" , Space , Str "title" ] ])
[ Plain
[ Image
( "" , [] , [] )
[ Str "Image" , Space , Str "title" ]
( "https://example.com/someimage.png" , "" )
]
]
, Para
[ Link
( "" , [] , [] )
[ Str "Some" , Space , Str "link" ]
( "subdir/someimage.png" , "" )
]
, Para
[ Link
( "" , [] , [] )
[ Str "Some" , Space , Str "link" ]
( "https://example.com/someimage.png" , "" )
]
, Header
2
( "source-include" , [] , [] )
Expand Down
17 changes: 17 additions & 0 deletions expected.native
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,29 @@
( "subdir/someimage.png" , "" )
]
]
, Figure
( "" , [] , [] )
(Caption
Nothing [ Plain [ Str "Image" , Space , Str "title" ] ])
[ Plain
[ Image
( "" , [] , [] )
[ Str "Image" , Space , Str "title" ]
( "https://example.com/someimage.png" , "" )
]
]
, Para
[ Link
( "" , [] , [] )
[ Str "Some" , Space , Str "link" ]
( "subdir/someimage.png" , "" )
]
, Para
[ Link
( "" , [] , [] )
[ Str "Some" , Space , Str "link" ]
( "https://example.com/someimage.png" , "" )
]
, Header
1
( "source-include" , [] , [] )
Expand Down
6 changes: 6 additions & 0 deletions include-files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ local function update_contents(blocks, shift_by, include_path)
end,
-- If link paths are relative then prepend include file path
Link = function (link)
if string.match(link.target, "^%a+://") then
return link
end
if path.is_relative(link.target) then
link.target = path.normalize(path.join({include_path, link.target}))
end
return link
end,
-- If image paths are relative then prepend include file path
Image = function (image)
if string.match(image.src, "^%a+://") then
return image
end
if path.is_relative(image.src) then
image.src = path.normalize(path.join({include_path, image.src}))
end
Expand Down
4 changes: 4 additions & 0 deletions subdir/file-g.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ Image relative path will be updated.

![Image title](someimage.png)

![Image title](https://example.com/someimage.png)

[Some link](someimage.png)

[Some link](https://example.com/someimage.png)

# Source include

File inclusion codeblocks for use with include-code-files will be
Expand Down

0 comments on commit f671f7e

Please sign in to comment.