Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirects for Docs #156

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,15 @@ require('./developers')(app);
// redirect old docs path
var projects = require('./package.json')._projects;
app.get('/docs/:project?/:module?/:file?', function(req, res){
if (!req.params.project || !projects[req.params.project]){
res.redirect(301, '/core/docs');
var project = projects[req.params.project] ? req.params.project : 'core';
var latestVersion = projects[project].versions[0];
if (!req.params.project) res.redirect(301, '/core/docs' + latestVersion);
var newPath;
if (!req.params.file){
newPath = ['/core/docs', latestVersion, req.params.project, req.params.module].join('/');
} else {
newPath = ['', req.params.project, 'docs', latestVersion, req.params.module, req.params.file].join('/');
}
var latestVersion = projects[req.params.project].versions[0];
var newPath = '/' + [req.params.project, 'docs', latestVersion, req.params.module, req.params.file].filter(Boolean).join('/');
res.redirect(301, newPath);
});

Expand Down
34 changes: 34 additions & 0 deletions lib/compile-md.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@ var hljs = require('highlight.js');
var slug = require ('slugify');
var jade = require('jade');
var fs = require('fs');
var projects = require('../package.json')._projects;

var viewsDir = __dirname + '/../views';
var headingTemplate = jade.compile(fs.readFileSync(viewsDir + '/partials/docs/heading.jade'));
var blockcodeTemplate = jade.compile(fs.readFileSync(viewsDir + '/partials/docs/blockcode.jade'));
var linkTemplate = jade.compile(fs.readFileSync(viewsDir + '/partials/docs/link.jade'));

function preparePath(path){
if (path.indexOf('#') != -1){
var components = path.split('#');
var file = components[0].split('/').filter(Boolean).slice(-2).join('/');
path = file + '#' + components[1];
} else {
path = path.split('/').filter(Boolean).slice(-2).join('/');
}
return path;
}

function compile(md, path){

Expand All @@ -34,6 +47,27 @@ function compile(md, path){
});
};

// fix redirection of old paths
renderer.link = function(link, title, text){
var version = path.match(/[\d.]+/);
var project;

if (link.match(/^\//)){
project = link.match(/^\/([^\d\/]+)/)[1];
var mdPath = preparePath(link);
link = '/' + project + '/docs/' + version + '/' + mdPath;
}
if (link.match(/125$/)){
project = link.match(/([^\d\/]+)\d+$/)[1];
link = projects[project].versions.filter(function(version){
return version.slice(0, 3) == '1.2';
})[0];
text = text.replace(/\s([\d.]+)\s/, ' ' + link + ' ');
}

return linkTemplate({link: link, text: text});
}

var sidebar = [];
var links = {};
var index = -1;
Expand Down
1 change: 1 addition & 0 deletions views/partials/docs/link.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a(href=link, alt=text) !{text}