Enable clean / extension-less url (#677)

This commit is contained in:
Endilie Yacop Sucipto 2018-06-07 04:37:49 +08:00 committed by Joel Marcey
parent aee255219b
commit 31f0c27f81
14 changed files with 124 additions and 50 deletions

View file

@ -41,8 +41,14 @@ async function execute() {
// create the folder path for a file if it does not exist, then write the file
function writeFileAndCreateFolder(file, content) {
mkdirp.sync(path.dirname(file));
fs.writeFileSync(file, content);
// build extra file for extension-less url if "cleanUrl" siteConfig is true
if (siteConfig.cleanUrl && file.indexOf('index.html') === -1) {
const extraFile = file.replace(/\.html$/, '/index.html');
mkdirp.sync(path.dirname(extraFile));
fs.writeFileSync(extraFile, content);
}
}
const TABLE_OF_CONTENTS_TOKEN = '<AUTOGENERATED_TABLE_OF_CONTENTS>';
@ -156,6 +162,7 @@ async function execute() {
// replace any links to markdown files to their website html links
Object.keys(mdToHtml).forEach(function(key, index) {
let link = mdToHtml[key];
link = siteConfig.cleanUrl ? link.replace(/\.html$/, '') : link;
link = link.replace('/en/', '/' + language + '/');
link = link.replace(
'/VERSION/',
@ -196,12 +203,15 @@ async function execute() {
env.translation.enabled &&
metadata.permalink.indexOf('docs/en') !== -1
) {
const redirectlink = siteConfig.cleanUrl
? metadata.permalink.replace(/\.html$/, '')
: metadata.permalink;
const redirectComp = (
<Redirect
metadata={metadata}
language={language}
config={siteConfig}
redirect={siteConfig.baseUrl + metadata.permalink}
redirect={siteConfig.baseUrl + redirectlink}
/>
);
const redirectStr = renderToStaticMarkupWithDoctype(redirectComp);

View file

@ -199,6 +199,7 @@ function execute(port) {
// replace any links to markdown files to their website html links
Object.keys(mdToHtml).forEach(function(key, index) {
let link = mdToHtml[key];
link = siteConfig.cleanUrl ? link.replace(/\.html$/, '') : link;
link = link.replace('/en/', '/' + language + '/');
link = link.replace(
'/VERSION/',
@ -271,7 +272,7 @@ function execute(port) {
});
// Handle all requests for blog pages and posts.
app.get(/blog\/.*html$/, (req, res) => {
app.get(/^\/blog\/.*html$/, (req, res) => {
// Regenerate the blog metadata in case it has changed. Consider improving
// this to regenerate on file save rather than on page request.
reloadMetadataBlog();
@ -305,7 +306,7 @@ function execute(port) {
// send corresponding blog page if appropriate
if (parts[1] === 'index.html') {
res.send(blogPages['/index.html']);
} else if (parts[1].endsWith('/index.html')) {
} else if (parts[1].endsWith('/index.html') && blogPages[parts[1]]) {
res.send(blogPages[parts[1]]);
} else if (parts[1].match(/page([0-9]+)/)) {
if (parts[1].endsWith('/')) {
@ -314,9 +315,14 @@ function execute(port) {
res.send(blogPages[parts[1] + '/index.html']);
}
} else {
// else send corresponding blog post
// send corresponding blog post. Ex: request to "blog/test/index.html" or
// "blog/test.html" will return html rendered version of "blog/test.md"
let file = parts[1];
file = file.replace(/\.html$/, '.md');
if (file.endsWith('/index.html')) {
file = file.replace(/\/index.html$/, '.md');
} else {
file = file.replace(/\.html$/, '.md');
}
file = file.replace(new RegExp('/', 'g'), '-');
file = join(CWD, 'blog', file);
@ -527,23 +533,35 @@ function execute(port) {
app.use(siteConfig.baseUrl, express.static(join(__dirname, '..', 'static')));
// "redirect" requests to pages ending with "/" or no extension so that,
// for example, request to "blog" returns same result as "blog/index.html"
// for example, request to "blog" returns "blog/index.html" or "blog.html"
app.get(/\/[^\.]*\/?$/, (req, res) => {
let slash = req.path.toString().endsWith('/') ? '' : '/';
request.get(
'http://localhost:' + port + req.path + slash + 'index.html',
(err, response, body) => {
if (!err) {
const requestFile = (url, notFoundCallback) => {
request.get(url, (error, response, body) => {
if (!error) {
if (response) {
res.status(response.statusCode).send(body);
if (response.statusCode === 404 && notFoundCallback) {
notFoundCallback();
} else {
res.status(response.statusCode).send(body);
}
} else {
console.error('No response');
}
} else {
console.error('Request failed:', err);
console.error('Request failed:', error);
}
}
);
});
};
let slash = req.path.toString().endsWith('/') ? '' : '/';
let requestUrl = 'http://localhost:' + port + req.path;
requestFile(requestUrl + slash + 'index.html', () => {
requestFile(
slash === '/'
? requestUrl + '.html'
: requestUrl.replace(/\/$/, '.html'),
null
);
});
});
// Start LiveReload server.