Skip to content

Commit 2e43f95

Browse files
committed
Fix SVG xlink:href rewriting in link-rewriter
Add support for rewriting xlink:href and href attributes in SVG image, use, and feImage elements to enable proper offline viewing of SVG-referenced assets.
1 parent d978ddd commit 2e43f95

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/link-rewriter.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,30 @@ export function rewriteLinks(html, pageUrl, urlMap, _options = {}) {
171171
}
172172
});
173173

174+
// Rewrite SVG <image>, <use>, <feImage> xlink:href and href attributes
175+
$('image[xlink\\:href], use[xlink\\:href], feImage[xlink\\:href]').each(
176+
(_, el) => {
177+
const href = $(el).attr('xlink:href');
178+
if (shouldSkipUrl(href)) return;
179+
180+
const localPath = getLocalPath(href);
181+
if (localPath) {
182+
$(el).attr('xlink:href', localPath);
183+
}
184+
},
185+
);
186+
187+
// SVG 2 uses href without namespace
188+
$('image[href], use[href], feImage[href]').each((_, el) => {
189+
const href = $(el).attr('href');
190+
if (shouldSkipUrl(href)) return;
191+
192+
const localPath = getLocalPath(href);
193+
if (localPath) {
194+
$(el).attr('href', localPath);
195+
}
196+
});
197+
174198
// Rewrite style attributes
175199
$('[style]').each((_, el) => {
176200
const style = $(el).attr('style');

0 commit comments

Comments
 (0)