Resolve all HTML links: - /test.md in hello -> ../test.html @param {String} currentFile @param {Function(String) -> String} resolveFile @param {HTMLDom} $
(currentFile, resolveFile, $)
| 13 | @param {HTMLDom} $ |
| 14 | */ |
| 15 | function resolveLinks(currentFile, resolveFile, $) { |
| 16 | var currentDirectory = path.dirname(currentFile); |
| 17 | |
| 18 | return editHTMLElement($, 'a', function($a) { |
| 19 | var href = $a.attr('href'); |
| 20 | |
| 21 | // Don't change a tag without href |
| 22 | if (!href) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | if (LocationUtils.isExternal(href)) { |
| 27 | $a.attr('target', '_blank'); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | // Split anchor |
| 32 | var parsed = url.parse(href); |
| 33 | href = parsed.pathname || ''; |
| 34 | |
| 35 | if (href) { |
| 36 | // Calcul absolute path for this |
| 37 | href = LocationUtils.toAbsolute(href, currentDirectory, '.'); |
| 38 | |
| 39 | // Resolve file |
| 40 | href = resolveFile(href); |
| 41 | |
| 42 | // Convert back to relative |
| 43 | href = LocationUtils.relative(currentDirectory, href); |
| 44 | } |
| 45 | |
| 46 | // Add back anchor |
| 47 | href = href + (parsed.hash || ''); |
| 48 | |
| 49 | $a.attr('href', href); |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | module.exports = resolveLinks; |
no test coverage detected
searching dependent graphs…