Skip to content

Commit

Permalink
allow viewdocs.io style links in an index.md (which don't have the ex…
Browse files Browse the repository at this point in the history
…tension) which can also work inside the wiki using the file-extension attribute; also fix relative links when viewing a folder or the index.md/readme.md file; remove the file if it has an extension inside if we're linking to a relative link
  • Loading branch information
jstrachan committed Nov 18, 2013
1 parent 1ff2dd4 commit e970aab
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions hawtio-web/src/main/webapp/app/wiki/js/wikiPlugin.ts
Expand Up @@ -58,31 +58,42 @@ module Wiki {
a = $(a);

var href = (a.attr('href') || "").trim();
var fileExtension = a.attr('file-extension');
var extension = fileExtension ? "." + fileExtension : "";

// Deal with relative URLs first...
var path = $location.path();
if (href.startsWith('../')) {
var path = $location.path();
var parts = href.split('/');
var pathParts = path.split('/');
var parents = parts.filter((part) => { return part === ".."; });
parts = parts.last(parts.length - parents.length);
pathParts = pathParts.first(pathParts.length - parents.length);

a.attr('href', '#' + pathParts.join('/') + '/' + parts.join('/') + $location.hash());
a.attr('href', '#' + pathParts.join('/') + '/' + parts.join('/') + extension + $location.hash());
Core.$apply($scope);
return;
}

// Turn an absolute link into a wiki link...
if (href.startsWith('/')) {
a.attr('href', Wiki.branchLink($scope.branch, href, $location));
a.attr('href', Wiki.branchLink($scope.branch, href + extension, $location) + extension);
return;
}

if (!Wiki.excludeAdjustmentPrefixes.any((exclude) => {
return href.startsWith(exclude);
})) {
a.attr('href', '#' + $location.path() + "/" + href + $location.hash());
// if the path has a dot in it lets exclude it as we are relative to a markdown or html file in a folder
var folderPath = path;
var idx = path.lastIndexOf("/");
if (idx > 0) {
var lastName = path.substring(idx + 1);
if (lastName.indexOf(".") >= 0) {
folderPath = path.substring(0, idx);
}
}
a.attr('href', '#' + folderPath + "/" + href + extension + $location.hash());
Core.$apply($scope);
return;
}
Expand Down

0 comments on commit e970aab

Please sign in to comment.