diff --git a/src/default.htl b/src/default.htl index bedc9bb..9064e2e 100644 --- a/src/default.htl +++ b/src/default.htl @@ -17,7 +17,7 @@
diff --git a/src/default.pre.js b/src/default.pre.js index 148027f..cdfa980 100644 --- a/src/default.pre.js +++ b/src/default.pre.js @@ -1,5 +1,6 @@ const moment = require('moment'); const request = require('request-promise'); +const md2json = require('md2json'); /** * Appends the context path to the resource based on the strain @@ -49,6 +50,32 @@ 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 @@ -96,6 +123,7 @@ module.exports.main = function (ctx) { .then(collectMetadata) .then(extractCommittersFromMetadata) .then(extractLastModifiedFromMetadata) + .then(collectNav) .catch(error => { console.error('Error while executing default.pre.js', error); }); diff --git a/src/nav.htl b/src/nav.htl deleted file mode 100644 index a682967..0000000 --- a/src/nav.htl +++ /dev/null @@ -1 +0,0 @@ -
${item}
\ No newline at end of file diff --git a/src/nav.pre.js b/src/nav.pre.js deleted file mode 100644 index 0903a92..0000000 --- a/src/nav.pre.js +++ /dev/null @@ -1,42 +0,0 @@ -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); -}; - -/** - * Rewrites links in nav prefixing with strain context changing to .html - * @param {RequestContext} ctx Context - */ -function rewriteLinks(ctx) { - ctx.resource.children = ctx.resource.children.map(element => { - return element.replace(new RegExp('(href=")(.*)(\.md")', 'g'), 'href="/' + ctx.strain + '/$2.html"'); - }); - return Promise.resolve(ctx); -}; - -module.exports.main = function (ctx) { - ctx.resource = ctx.resource || {}; - - return Promise.resolve(ctx) - .then(setContextPath) - .then(removeFirstTitle) - .then(rewriteLinks) - .catch(error => { - console.error('Error while executing nav.pre.js', error); - }); -};