Skip to content

Commit

Permalink
Merge pull request #88 from curbengh/url_for-options
Browse files Browse the repository at this point in the history
docs: url_for option
  • Loading branch information
curbengh authored Sep 15, 2019
2 parents 2e30d0c + 87b2b25 commit cc5faf2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ truncate('And they found that many people were sleeping better.', {length: 25, o
// "And they f... (continued)"
```

### url_for(path)
### url_for(path, {relative: boolean})

Returns a url with the root path prefixed. Output is [encoded](#encodeurlstr) automatically. Requires [`bind(hexo)`](#bindhexo).

Expand All @@ -330,6 +330,24 @@ url_for('/a/path')
// /blog/a/path
```

Relative link, follows `relative_link` option by default
e.g. post/page path is '/foo/bar/index.html'

``` yml
_config.yml
relative_link: true
```
``` js
url_for('/css/style.css')
// ../../css/style.css

// Override option
// you could also enable it to output a relative link,
// even when `relative_link` is disabled
url_for('/css/style.css', {relative: false})
// /css/style.css

## bind(hexo)

Following utilities require `bind(hexo)` / `bind(this)` to parse the user config when initializing:
Expand Down
5 changes: 3 additions & 2 deletions test/url_for.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ describe('url_for', () => {
});

it('internal url (options.relative)', () => {
ctx.path = '';
urlFor('index.html', {relative: true}).should.eql('index.html');
ctx.config.relative_link = false;
ctx.path = 'foo/bar/';
urlFor('index.html', {relative: true}).should.eql('../../index.html');

ctx.config.relative_link = true;
urlFor('index.html', {relative: false}).should.eql('/index.html');
Expand Down

0 comments on commit cc5faf2

Please sign in to comment.