Skip to content
This repository has been archived by the owner on Aug 27, 2019. It is now read-only.

Commit

Permalink
Refactor Navigation Include to use ESI
Browse files Browse the repository at this point in the history
Merging since adobe/project-helix#184 has been merged.
  • Loading branch information
kptdobe authored Jun 19, 2018
2 parents 0df74b5 + 1722f0d commit 57cdcd9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/default.htl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</div>
<div class="main">
<div class="nav">
<div data-sly-test="${it.nav}" data-sly-list="${it.nav}">${item}</div>
<esi:include src="/${it.contextPath}/SUMMARY.html" />
</div>
<div class="content">
<div class="content-body">
Expand Down
28 changes: 0 additions & 28 deletions src/default.pre.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const moment = require('moment');
const request = require('request-promise');
const md2json = require('md2json');

/**
* Appends the context path to the resource based on the strain
Expand Down Expand Up @@ -50,32 +49,6 @@ function collectMetadata(ctx) {
});
};

/**
* Collects the nav and append it to the resource
* @param {RequestContext} ctx Context
*/
function collectNav(ctx) {
const params = {
org: ctx.strainConfig.urls.content.owner,
repo: ctx.strainConfig.urls.content.repo,
tree: ctx.strainConfig.urls.content.ref,
path: 'SUMMARY.md'
};

return md2json.main(params).then(info => {
let nav = info.body.children;
// remove first title
delete nav[0];

// link re-writing
// TODO: move into md2json + parameters
ctx.resource.nav = nav.map(element => {
return element.replace(new RegExp('href="', 'g'), 'href="/' + ctx.strain + '/');
});
return Promise.resolve(ctx);
});
};

/**
* Extracts some committers data from the list of commits and appends the list to the resource
* @param {RequestContext} ctx Context
Expand Down Expand Up @@ -123,7 +96,6 @@ module.exports.main = function (ctx) {
.then(collectMetadata)
.then(extractCommittersFromMetadata)
.then(extractLastModifiedFromMetadata)
.then(collectNav)
.catch(error => {
console.error('Error while executing default.pre.js', error);
});
Expand Down
1 change: 1 addition & 0 deletions src/nav.htl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div data-sly-test="${it.children}" data-sly-list="${it.children}">${item}</div>
30 changes: 30 additions & 0 deletions src/nav.pre.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const request = require('request-promise');

/**
* Appends the context path to the resource based on the strain
* @param {RequestContext} ctx Context
*/
function setContextPath(ctx) {
ctx.resource.contextPath = ctx.strain;
return Promise.resolve(ctx);
};

/**
* Removes the first title from the resource children
* @param {RequestContext} ctx Context
*/
function removeFirstTitle(ctx) {
delete ctx.resource.children[0];
return Promise.resolve(ctx);
};

module.exports.main = function (ctx) {
ctx.resource = ctx.resource || {};

return Promise.resolve(ctx)
.then(setContextPath)
.then(removeFirstTitle)
.catch(error => {
console.error('Error while executing nav.pre.js', error);
});
};

0 comments on commit 57cdcd9

Please sign in to comment.