Skip to content

Commit

Permalink
Merge eabeb73 into e4eb60a
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Dec 5, 2019
2 parents e4eb60a + eabeb73 commit 4cc739e
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 9 deletions.
34 changes: 34 additions & 0 deletions README.md
Expand Up @@ -29,6 +29,7 @@ Utilities for [Hexo].
- [isExternalLink](#isexternallinkurl-sitehost-exclude)
- [Pattern](#patternrule)
- [Permalink](#permalinkrule-options)
- [prettyUrls](#prettyurlsurl-options)
- [relative_url](#relative_urlfrom-to)
- [slugize](#slugizestr-options)
- [spawn](#spawncommand-args-options)
Expand Down Expand Up @@ -360,6 +361,39 @@ permalink.stringify({year: '2014', month: '01', day: '31', title: 'test'})
// 2014/01/31/test
```

### prettyUrls(url, [options])

Rewrite urls to pretty URLs.

Option | Description | Default
--- | --- | ---
`trailing_index` | `/about/index.html -> /about/` when `false` | `true`
`trailing_html` | `/about.html -> /about` when `false` | `true`

Notice if only `trailing_html` is enabled, trailing `index.html` will remains the same (not to be rewritten in `index`).

``` js
prettyUrls('/foo/bar.html');
// /foo/bar.html
prettyUrls('/foo/bar/index.html');
// /foo/bar/index.html

prettyUrls('/foo/bar.html', { trailing_index: false });
// /foo/bar.html
prettyUrls('/foo/bar/index.html', { trailing_index: false });
// /foo/bar/

prettyUrls('/foo/bar.html', { trailing_html: false });
// /foo/bar
prettyUrls('/foo/bar/index.html', { trailing_html: false });
// /foo/bar/index.html

prettyUrls('/foo/bar.html', { trailing_index: false, trailing_html: false });
// /foo/bar
prettyUrls('/foo/bar/index.html', { trailing_index: false, trailing_html: false });
// /foo/bar/
```

### relative_url(from, to)

Returns the relative URL from `from` to `to`. Output is [encoded](#encodeurlstr) automatically. Requires [`bind(hexo)`](#bindhexo).
Expand Down
8 changes: 5 additions & 3 deletions lib/full_url_for.js
Expand Up @@ -2,6 +2,7 @@

const { parse, URL } = require('url');
const encodeURL = require('./encode_url');
const prettyUrls = require('./pretty_urls');

function fullUrlForHelper(path = '/') {
const pathRegex = /^(\/\/|http(s)?:)/;
Expand All @@ -16,11 +17,12 @@ function fullUrlForHelper(path = '/') {

path = encodeURL(config.url + `/${path}`.replace(/\/{2,}/g, '/'));

const { trailing_index } = Object.assign({
trailing_index: true
const prettyUrlsOptions = Object.assign({
trailing_index: true,
trailing_html: true
}, config.pretty_urls);

if (!trailing_index) path = path.replace(/index\.html$/, '');
path = prettyUrls(path, prettyUrlsOptions);

return path;
}
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -21,6 +21,7 @@ exports.htmlTag = require('./html_tag');
exports.isExternalLink = require('./is_external_link');
exports.Pattern = require('./pattern');
exports.Permalink = require('./permalink');
exports.prettyUrls = require('./pretty_urls');
exports.relative_url = require('./relative_url');
exports.slugize = require('./slugize');
exports.spawn = require('./spawn');
Expand Down
18 changes: 18 additions & 0 deletions lib/pretty_urls.js
@@ -0,0 +1,18 @@
'use strict';

function prettyUrls(url, options = {}) {
options = Object.assign({
trailing_index: true,
trailing_html: true
}, options);

const indexPattern = /index\.html$/;
if (options.trailing_index === false) url = url.replace(indexPattern, '');
if (options.trailing_html === false && !indexPattern.test(url)) {
url = url.replace(/\.html$/, '');
}

return url;
}

module.exports = prettyUrls;
8 changes: 5 additions & 3 deletions lib/url_for.js
Expand Up @@ -3,6 +3,7 @@
const { parse, URL } = require('url');
const encodeURL = require('./encode_url');
const relative_url = require('./relative_url');
const prettyUrls = require('./pretty_urls');

function urlForHelper(path = '/', options) {
const pathRegex = /^(#|\/\/|http(s)?:)/;
Expand All @@ -28,11 +29,12 @@ function urlForHelper(path = '/', options) {
// Prepend root path
path = encodeURL((root + path).replace(/\/{2,}/g, '/'));

const { trailing_index } = Object.assign({
trailing_index: true
const prettyUrlsOptions = Object.assign({
trailing_index: true,
trailing_html: true
}, config.pretty_urls);

if (!trailing_index) path = path.replace(/index\.html$/, '');
path = prettyUrls(path, prettyUrlsOptions);

return path;
}
Expand Down
26 changes: 25 additions & 1 deletion test/full_url_for.spec.js
Expand Up @@ -29,13 +29,37 @@ describe('full_url_for', () => {
it('internal url - pretty_urls.trailing_index disabled', () => {
ctx.config.url = 'https://example.com';
ctx.config.pretty_urls = {
trailing_index: false
trailing_index: false,
trailing_html: true
};

fullUrlFor('index.html').should.eql(ctx.config.url + '/');
fullUrlFor('/').should.eql(ctx.config.url + '/');
});

it('internal url - pretty_urls.trailing_html disabled', () => {
ctx.config.url = 'https://example.com';
ctx.config.pretty_urls = {
trailing_index: true,
trailing_html: false
};

fullUrlFor('index.html').should.eql(ctx.config.url + '/index.html');
fullUrlFor('/foo/bar.html').should.eql(ctx.config.url + '/foo/bar');
});

it('internal url - pretty_urls.trailing_index & pretty_urls.trailing_html disabled', () => {
ctx.config.url = 'https://example.com';
ctx.config.pretty_urls = {
trailing_index: false,
trailing_html: false
};

fullUrlFor('index.html').should.eql(ctx.config.url + '/');
fullUrlFor('/').should.eql(ctx.config.url + '/');
fullUrlFor('/foo/bar.html').should.eql(ctx.config.url + '/foo/bar');
});


it('absolute url', () => {
[
Expand Down
31 changes: 31 additions & 0 deletions test/pretty_utls.spec.js
@@ -0,0 +1,31 @@
'use strict';

require('chai').should();

describe('prettyUrls', () => {
const prettyUrls = require('../lib/pretty_urls');

it('default', () => {
prettyUrls('//example.com/index.html').should.eql('//example.com/index.html');
prettyUrls('/bar/foo.html').should.eql('/bar/foo.html');
prettyUrls('/bar/foo/').should.eql('/bar/foo/');
});

it('trailing_index', () => {
prettyUrls('//example.com/index.html', { trailing_index: false }).should.eql('//example.com/');
prettyUrls('/bar/foo/index.html/index.html', { trailing_index: false }).should.eql('/bar/foo/index.html/');
prettyUrls('/bar/foo.html', { trailing_index: false }).should.eql('/bar/foo.html');
});

it('trailing_html', () => {
prettyUrls('//example.com/index.html', { trailing_html: false }).should.eql('//example.com/index.html');
prettyUrls('/bar/foo/index.html/index.html', { trailing_html: false }).should.eql('/bar/foo/index.html/index.html');
prettyUrls('/bar/foo.html', { trailing_html: false }).should.eql('/bar/foo');
});

it('trailing_index & trailing_html', () => {
prettyUrls('//example.com/index.html', { trailing_index: false, trailing_html: false }).should.eql('//example.com/');
prettyUrls('/bar/foo/index.html/index.html', { trailing_index: false, trailing_html: false }).should.eql('/bar/foo/index.html/');
prettyUrls('/bar/foo.html', { trailing_index: false, trailing_html: false }).should.eql('/bar/foo');
});
});
40 changes: 38 additions & 2 deletions test/url_for.spec.js
Expand Up @@ -62,10 +62,11 @@ describe('url_for', () => {
ctx.config.relative_link = false;
});

it('internal url (pretty_url.trailing_index disabled)', () => {
it('internal url - pretty_urls.trailing_index disabled', () => {
ctx.config.root = '/';
ctx.config.pretty_urls = {
trailing_index: false
trailing_index: false,
trailing_html: true
};

urlFor('index.html').should.eql('/');
Expand All @@ -78,6 +79,41 @@ describe('url_for', () => {
urlFor('/index.html').should.eql('/blog/');
});


it('internal url - pretty_urls.trailing_html disabled', () => {
ctx.config.root = '/';
ctx.config.pretty_urls = {
trailing_index: true,
trailing_html: false
};

urlFor('index.html').should.eql('/index.html');
urlFor('/').should.eql('/');
urlFor('/foo/bar.html').should.eql('/foo/bar');

ctx.config.root = '/blog/';
urlFor('index.html').should.eql('/blog/index.html');
urlFor('/').should.eql('/blog/');
urlFor('/foo/bar.html').should.eql('/blog/foo/bar');
});

it('internal url - pretty_urls.trailing_index & pretty_urls.trailing_html disabled', () => {
ctx.config.root = '/';
ctx.config.pretty_urls = {
trailing_index: false,
trailing_html: false
};

urlFor('index.html').should.eql('/');
urlFor('/').should.eql('/');
urlFor('/foo/bar.html').should.eql('/foo/bar');

ctx.config.root = '/blog/';
urlFor('index.html').should.eql('/blog/');
urlFor('/').should.eql('/blog/');
urlFor('/foo/bar.html').should.eql('/blog/foo/bar');
});

it('absolute url', () => {
[
'https://hexo.io/',
Expand Down

0 comments on commit 4cc739e

Please sign in to comment.