Skip to content

Commit

Permalink
port of rest-js hack to fix sticky links
Browse files Browse the repository at this point in the history
  • Loading branch information
jgravois committed Sep 21, 2018
1 parent b89d026 commit 4fdad7a
Showing 1 changed file with 107 additions and 1 deletion.
108 changes: 107 additions & 1 deletion docs/acetate.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,33 @@ const { inspect } = require("util");
const _ = require("lodash");

const IS_DEV = process.env.ENV !== "prod";
const BASE_URL = process.env.ENV === "prod" ? "/hub.js" : "";
const BASE_URL = IS_DEV ? "" : "/hub.js";

const url = require("url");
const PROTOCOL_REGEX = /^(.+:)?\/\//;
const HASH_REGEX = /^#/;

function stripSlashes(string = "") {
var count = string.length - 1;
var index = 0;

while (string.charCodeAt(index) === 47 && ++index);
while (string.charCodeAt(count) === 47 && --count);

string = string.slice(index, count + 1);

return string;
}

function ensureTrailingSlash(baseUrl) {
let ext = path.extname(baseUrl);

if (!ext && baseUrl[baseUrl.length - 1] !== "/") {
baseUrl = baseUrl + "/";
}

return baseUrl;
}

module.exports = function(acetate) {
/**
Expand Down Expand Up @@ -213,6 +239,86 @@ module.exports = function(acetate) {
}@${package.version}/dist/umd/${package.name.split("/")[1]}.umd.js`;
});

acetate.helper(
"link",
function(context, destinationUrl, text) {
const currentUrl = context.options.currentUrl || context.page.url;
const options = context.options;
let finalUrl;
let isActive = false;

if (
PROTOCOL_REGEX.test(destinationUrl) ||
HASH_REGEX.test(destinationUrl)
) {
finalUrl = destinationUrl;
} else {
finalUrl = url.resolve(currentUrl, destinationUrl);

// strip all leading and trailing slashes
finalUrl = stripSlashes(finalUrl);

// ensure we have leading slash unless this starts with a protocol
if (!PROTOCOL_REGEX.test(finalUrl) && finalUrl[0] !== "/") {
finalUrl = `/${finalUrl}`;
}

// ensure there is a trailing slash unless there is a extension
// and remove index.html
let parsedUrl = url.parse(finalUrl);
parsedUrl.pathname = ensureTrailingSlash(parsedUrl.pathname).replace(
"index.html",
""
);

finalUrl = url.format(parsedUrl);
}

const hrefAttr = `href="${finalUrl}"`;
const idAttr = options.id ? `id="${options.id}"` : "";
if (`${BASE_URL}${currentUrl}` === finalUrl) {
isActive = true;
} else if (PROTOCOL_REGEX.test(destinationUrl)) {
isActive = false;
} else if (HASH_REGEX.test(destinationUrl)) {
isActive = true;
} else if (options.requireExactMatch) {
isActive = `${BASE_URL}${currentUrl}` === finalUrl;
} else {
let parsedUrl = url.parse(finalUrl);

isActive = `${BASE_URL}${currentUrl}`.match(parsedUrl.pathname) && finalUrl !== "/";
}

const classes = isActive
? _([options.activeClass, options.class])
.compact()
.join(" ")
: options.class;
const classAttr = classes && classes.length ? `class="${classes}"` : "";

delete options.id;
delete options.activeClass;
delete options.class;
delete options.requireExactMatch;
delete options.currentUrl;

const attrs = _(options)
.map((value, key) => `${key}="${value}"`)
.reverse()
.concat([classAttr, idAttr, hrefAttr])
.reverse()
.compact()
.join(" ");

return `<a ${attrs}>${text}</a>`;
},
{
activeClass: "is-active",
requireExactMatch: false
}
);

acetate.helper("npmInstallCmd", function(context, package) {
const peers = package.peerDependencies
? Object.keys(package.peerDependencies).map(
Expand Down

0 comments on commit 4fdad7a

Please sign in to comment.