Skip to content

Commit

Permalink
refactor cleanurl code
Browse files Browse the repository at this point in the history
  • Loading branch information
endiliey committed May 20, 2018
1 parent bf7f3a2 commit 63d4982
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 89 deletions.
3 changes: 2 additions & 1 deletion docs/api-site-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ h1 {

`wrapPagesHTML` - Boolean flag to indicate whether `html` files in `/pages` should be wrapped with Docusaurus site styles, header and footer. This feature is experimental and relies on the files being `html` fragments instead of complete pages. It inserts the contents of your `html` file with no extra processing. Defaults to `false`.

`cleanUrl` - If `true`, request to URL https://docusaurus.io/docs/installation will returns the same result as https://docusaurus.io/docs/installation.html.
`cleanUrl` - If `true`, allow URLs with no `html` extension. Example: request to URL https://docusaurus.io/docs/installation will returns the same result as https://docusaurus.io/docs/installation.html.

Users can also add their own custom fields if they wish to provide some data across different files.

Expand Down Expand Up @@ -246,6 +246,7 @@ const siteConfig = {
twitterUsername: 'docusaurus',
twitterImage: 'img/docusaurus.png',
ogImage: 'img/docusaurus.png',
cleanUrl: true
};

module.exports = siteConfig;
Expand Down
35 changes: 17 additions & 18 deletions lib/core/BlogPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ const utils = require('./utils');

// inner blog component for the article itself, without sidebar/header/footer
class BlogPost extends React.Component {
getPath = path => {
if (this.props.config.cleanUrl) {
if (path.endsWith('/index.html')) {
return path.replace(/\/index.html$/, '');
} else {
return path.replace(/\.html$/, '');
}
}
return path;
};

renderContent() {
if (this.props.truncate) {
let postPath = this.props.post.path;
if (this.props.config.cleanUrl) {
if (postPath.endsWith('/index.html')) {
postPath = postPath.replace(/\/index.html$/, '');
} else {
postPath = postPath.replace(/\.html$/, '');
}
}
return (
<article className="post-content">
<MarkdownBlock>
Expand All @@ -31,7 +34,11 @@ class BlogPost extends React.Component {
<div className="read-more">
<a
className="button"
href={this.props.config.baseUrl + 'blog/' + postPath}>
href={
this.props.config.baseUrl +
'blog/' +
this.getPath(this.props.post.path)
}>
Read More
</a>
</div>
Expand Down Expand Up @@ -77,17 +84,9 @@ class BlogPost extends React.Component {

renderTitle() {
const post = this.props.post;
let postPath = this.props.post.path;
if (this.props.config.cleanUrl) {
if (postPath.endsWith('/index.html')) {
postPath = postPath.replace(/\/index.html$/, '');
} else {
postPath = postPath.replace(/\.html$/, '');
}
}
return (
<h1>
<a href={this.props.config.baseUrl + 'blog/' + postPath}>
<a href={this.props.config.baseUrl + 'blog/' + this.getPath(post.path)}>
{post.title}
</a>
</h1>
Expand Down
23 changes: 11 additions & 12 deletions lib/core/BlogPostLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ const Site = require('./Site.js');

// used for entire blog posts, i.e., each written blog article with sidebar with site header/footer
class BlogPostLayout extends React.Component {
renderSocialButtons() {
let post = this.props.metadata;
getPath = path => {
if (this.props.config.cleanUrl) {
if (post.path.endsWith('/index.html')) {
post.path = post.path.replace(/\/index.html$/, '');
if (path.endsWith('/index.html')) {
return path.replace(/\/index.html$/, '');
} else {
post.path = post.path.replace(/\.html$/, '');
return path.replace(/\.html$/, '');
}
}
return path;
};

renderSocialButtons() {
let post = this.props.metadata;
post.path = this.getPath(post.path);

const fbComment = this.props.config.facebookAppId &&
this.props.config.facebookComments && (
Expand Down Expand Up @@ -100,13 +105,7 @@ class BlogPostLayout extends React.Component {

render() {
let post = this.props.metadata;
if (this.props.config.cleanUrl) {
if (post.path.endsWith('/index.html')) {
post.path = post.path.replace(/\/index.html$/, '');
} else {
post.path = post.path.replace(/\.html$/, '');
}
}
post.path = this.getPath(post.path);
return (
<Site
className="sideNavVisible"
Expand Down
33 changes: 15 additions & 18 deletions lib/core/nav/SideNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,30 @@ class SideNav extends React.Component {
}
return localizedString;
}

// return extension-less path if `cleanUrl` config is true
getPath = path => {
if (siteConfig.cleanUrl) {
if (path.endsWith('/index.html')) {
return path.replace(/\/index.html$/, '');
} else {
return path.replace(/\.html$/, '');
}
}
return path;
};

// return link to doc in sidebar
getLink(metadata) {
if (metadata.permalink) {
let targetLink = metadata.permalink;
if (siteConfig.cleanUrl) {
if (targetLink.endsWith('/index.html')) {
targetLink = targetLink.replace(/\/index.html$/, '');
} else {
targetLink = targetLink.replace(/\.html$/, '');
}
}

const targetLink = this.getPath(metadata.permalink);
if (targetLink.match(/^https?:/)) {
return targetLink;
}
return siteConfig.baseUrl + targetLink;
}
if (metadata.path) {
let targetPath = metadata.path;
if (siteConfig.cleanUrl) {
if (targetPath.endsWith('/index.html')) {
targetPath = targetPath.replace(/\/index.html$/, '');
} else {
targetPath = targetPath.replace(/\.html$/, '');
}
}
return siteConfig.baseUrl + 'blog/' + targetPath;
return siteConfig.baseUrl + 'blog/' + this.getPath(metadata.path);
}
return null;
}
Expand Down
60 changes: 20 additions & 40 deletions lib/server/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ async function execute() {
fs.writeFileSync(file, content);
}

// build extra file for extension-less url if "cleanUrl" siteConfig is true
function writeExtraFileIfCleanUrl(file, content) {
if (siteConfig.cleanUrl) {
writeFileAndCreateFolder(file.replace(/\.html$/, '/index.html'), content);
}
}

const TABLE_OF_CONTENTS_TOKEN = '<AUTOGENERATED_TABLE_OF_CONTENTS>';

// takes the content of a doc article and returns the content with a table of
Expand Down Expand Up @@ -191,14 +198,7 @@ async function execute() {

const targetFile = join(buildDir, metadata.permalink);
writeFileAndCreateFolder(targetFile, str);

// build extra files for extension-less url
if (siteConfig.cleanUrl) {
writeFileAndCreateFolder(
targetFile.replace(/\.html$/, '/index.html'),
str
);
}
writeExtraFileIfCleanUrl(targetFile, str);

// generate english page redirects when languages are enabled
if (
Expand All @@ -224,14 +224,7 @@ async function execute() {
metadata.permalink.replace('docs/en', 'docs')
);
writeFileAndCreateFolder(redirectFile, redirectStr);

// build extra files for extension-less url
if (siteConfig.cleanUrl) {
writeFileAndCreateFolder(
redirectFile.replace(/\.html$/, '/index.html'),
redirectStr
);
}
writeExtraFileIfCleanUrl(redirectFile, redirectStr);
}
});

Expand Down Expand Up @@ -294,12 +287,7 @@ async function execute() {

let targetFile = join(buildDir, 'blog', filePath);
writeFileAndCreateFolder(targetFile, str);

// build extra files for extension-less url
if (siteConfig.cleanUrl) {
let cleanTargetFile = targetFile.replace(/\.html$/, '/index.html');
writeFileAndCreateFolder(cleanTargetFile, str);
}
writeExtraFileIfCleanUrl(targetFile, str);
});
// create html files for all blog pages (collections of article previews)
const BlogPageLayout = require('../core/BlogPageLayout.js');
Expand Down Expand Up @@ -533,12 +521,10 @@ async function execute() {
targetFile.replace(sep + 'en' + sep, sep + language + sep),
str
);
// build extra files for extension-less url
if (siteConfig.cleanUrl && targetFile.indexOf('index.html') < 0) {
writeFileAndCreateFolder(
targetFile
.replace(sep + 'en' + sep, sep + language + sep)
.replace(/\.html$/, '/index.html'),

if (targetFile.indexOf('index.html') < 0) {
writeExtraFileIfCleanUrl(
targetFile.replace(sep + 'en' + sep, sep + language + sep),
str
);
}
Expand All @@ -557,12 +543,9 @@ async function execute() {
str
);

// build extra files for extension-less url
if (siteConfig.cleanUrl && targetFile.indexOf('index.html') < 0) {
writeFileAndCreateFolder(
targetFile
.replace(sep + 'en' + sep, sep)
.replace(/\.html$/, '/index.html'),
if (targetFile.indexOf('index.html') < 0) {
writeExtraFileIfCleanUrl(
targetFile.replace(sep + 'en' + sep, sep),
str
);
}
Expand All @@ -580,12 +563,9 @@ async function execute() {
str
);

// build extra files for extension-less url
if (siteConfig.cleanUrl && targetFile.indexOf('index.html') < 0) {
writeFileAndCreateFolder(
targetFile
.replace(sep + 'en' + sep, sep)
.replace(/\.html$/, '/index.html'),
if (targetFile.indexOf('index.html') < 0) {
writeExtraFileIfCleanUrl(
targetFile.replace(sep + 'en' + sep, sep),
str
);
}
Expand Down

0 comments on commit 63d4982

Please sign in to comment.