Skip to content

Commit 364394e

Browse files
authored
Use relative path for links (#2)
Signed-off-by: black-desk <[email protected]>
1 parent c86e823 commit 364394e

File tree

5 files changed

+23
-2
lines changed

5 files changed

+23
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ relative to the directory from which they are included. I.e., if a file `a/b.md`
6969
is included in the main document, and another file `a/b/c.md` should be included
7070
from `a/b.md`, then the relative path from `a/b.md` must be used, in this case
7171
`b/c.md`. The full relative path will be automatically generated in the final
72-
document. The same goes for image paths and codeblock file paths using the
73-
`include-code-files` filter.
72+
document. The same goes for image paths, link paths and codeblock file paths
73+
using the `include-code-files` filter.
7474

7575
## Example
7676

expected-auto.native

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@
128128
( "subdir/someimage.png" , "" )
129129
]
130130
]
131+
, Para
132+
[ Link
133+
( "" , [] , [] )
134+
[ Str "Some" , Space , Str "link" ]
135+
( "subdir/someimage.png" , "" )
136+
]
131137
, Header
132138
2
133139
( "source-include" , [] , [] )

expected.native

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@
128128
( "subdir/someimage.png" , "" )
129129
]
130130
]
131+
, Para
132+
[ Link
133+
( "" , [] , [] )
134+
[ Str "Some" , Space , Str "link" ]
135+
( "subdir/someimage.png" , "" )
136+
]
131137
, Header
132138
1
133139
( "source-include" , [] , [] )

include-files.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ local function update_contents(blocks, shift_by, include_path)
3434
end
3535
return header
3636
end,
37+
-- If link paths are relative then prepend include file path
38+
Link = function (link)
39+
if path.is_relative(link.target) then
40+
link.target = path.normalize(path.join({include_path, link.target}))
41+
end
42+
return link
43+
end,
3744
-- If image paths are relative then prepend include file path
3845
Image = function (image)
3946
if path.is_relative(image.src) then

subdir/file-g.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Image relative path will be updated.
44

55
![Image title](someimage.png)
66

7+
[Some link](someimage.png)
8+
79
# Source include
810

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

0 commit comments

Comments
 (0)