MCPcopy
hub / github.com/GitbookIO/gitbook / resolveLinks

Function resolveLinks

lib/output/modifiers/resolveLinks.js:15–51  ·  view source on GitHub ↗

Resolve all HTML links: - /test.md in hello -> ../test.html @param {String} currentFile @param {Function(String) -> String} resolveFile @param {HTMLDom} $

(currentFile, resolveFile, $)

Source from the content-addressed store, hash-verified

13 @param {HTMLDom} $
14*/
15function 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
53module.exports = resolveLinks;

Callers 1

resolveLinks.jsFile · 0.85

Calls 1

editHTMLElementFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…