Hexo: this.relative_url is not a function

Created on 29 Dec 2015  ·  10Comments  ·  Source: hexojs/hexo

This one is related to hexo-generator-sitemap - but I don't think the plugin is responsible...

If I use this plugin and turn the option relative_link on, I get the error this.relative_url is not a function in lib/plugins/helper/url_for.js (line 24). The error occurs while trying to get the permalink-property of a post.

Since other calls to this helper function are working (this.relative_url is defined if not called from the plugin), I reckon a missing initialization step before line 337 in lib/hexo/index.js..

Most helpful comment

I encountered the same issue with [email protected] and relative_link: true.

All 10 comments

I am also encountering this with a fresh clone of https://github.com/hexojs/site (20e041162935893db51f59f394ab01544587ff88) and enabling the relative_link: true in the _config.yml:

FATAL TypeError: this.relative_url is not a function
Please report this to https://github.com/chjj/marked.
    at Hexo.urlForHelper (./Src/hexo/hexojs-site/node_modules/hexo/lib/plugins/helper/url_for.js:25:17)
    at Renderer.link (./Src/hexo/hexojs-site/node_modules/hexo-renderer-marked/lib/renderer.js:38:23)
    at InlineLexer.outputLink (./Src/hexo/hexojs-site/node_modules/hexo-renderer-marked/node_modules/marked/lib/marked.js:707:21)
    at InlineLexer.output (./Src/hexo/hexojs-site/node_modules/hexo-renderer-marked/node_modules/marked/lib/marked.js:622:19)
    at Parser.parseText (./Src/hexo/hexojs-site/node_modules/hexo-renderer-marked/node_modules/marked/lib/marked.js:968:22)
    at Parser.tok (./Src/hexo/hexojs-site/node_modules/hexo-renderer-marked/node_modules/marked/lib/marked.js:1053:18)
    at Parser.tok (./Src/hexo/hexojs-site/node_modules/hexo-renderer-marked/node_modules/marked/lib/marked.js:1043:22)
    at Parser.parse (./Src/hexo/hexojs-site/node_modules/hexo-renderer-marked/node_modules/marked/lib/marked.js:935:17)
    at Function.Parser.parse (./Src/hexo/hexojs-site/node_modules/hexo-renderer-marked/node_modules/marked/lib/marked.js:922:17)
    at marked (./Src/hexo/hexojs-site/node_modules/hexo-renderer-marked/node_modules/marked/lib/marked.js:1218:19)
    at Hexo.module.exports (./Src/hexo/hexojs-site/node_modules/hexo-renderer-marked/lib/renderer.js:69:10)
    at Hexo.tryCatcher (./Src/hexo/hexojs-site/node_modules/hexo/node_modules/bluebird/js/main/util.js:26:23)
    at Hexo.<anonymous> (./Src/hexo/hexojs-site/node_modules/hexo/node_modules/bluebird/js/main/method.js:15:34)
    at ./Src/hexo/hexojs-site/node_modules/hexo/lib/hexo/render.js:51:21
    at tryCatcher (./Src/hexo/hexojs-site/node_modules/hexo/node_modules/bluebird/js/main/util.js:26:23)
    at Promise._settlePromiseFromHandler (./Src/hexo/hexojs-site/node_modules/hexo/node_modules/bluebird/js/main/promise.js:507:31)
    at Promise._settlePromiseAt (./Src/hexo/hexojs-site/node_modules/hexo/node_modules/bluebird/js/main/promise.js:581:18)
    at Promise._settlePromiseAtPostResolution (./Src/hexo/hexojs-site/node_modules/hexo/node_modules/bluebird/js/main/promise.js:245:10)
    at Async._drainQueue (./Src/hexo/hexojs-site/node_modules/hexo/node_modules/bluebird/js/main/async.js:128:12)
    at Async._drainQueues (./Src/hexo/hexojs-site/node_modules/hexo/node_modules/bluebird/js/main/async.js:133:10)
    at Immediate.Async.drainQueues [as _onImmediate] (./Src/hexo/hexojs-site/node_modules/hexo/node_modules/bluebird/js/main/async.js:15:14)
    at processImmediate [as _immediateCallback] (timers.js:383:17)

https://github.com/hexojs/hexo-renderer-marked/blob/master/lib/renderer.js#L38 :
It seems that the context of "this" passed to the url_for.call() does not contain the relative_url function during this call stack.

It's odd. It was working for me, but I wiped the node_modules folder and ran npm install and now I get this error too. I tried a fresh clone of https://github.com/hexojs/site and added relative_link: true to the config and that didn't work either.

This is a bug introduced recently by the hexo-renderer-marked plugin in this commit: https://github.com/hexojs/hexo-renderer-marked/commit/8e3dc69f40f836e53ffda3e1a8bf6d5dd7dd502a

I'm moving this conversation over there.

The PR is reverted.

I encountered the same issue with [email protected] and relative_link: true.

發現url_for中的internal url (relative on)測試可以通過,是因為在測試中有把relative_url放入ctx
所以測試url_for中的this指向ctx自然找得到relative_url這個function

  var ctx = {
    config: {},
    relative_url: require('../../../lib/plugins/helper/relative_url')
  };

  var urlFor = require('../../../lib/plugins/helper/url_for').bind(ctx);

但在實際使用上,this指向是hexo的instance,並沒有relative_url在其中

I believe this is the culprit code:
https://github.com/hexojs/hexo/blob/master/lib/plugins/helper/url_for.js#L28

  if (options.relative) {
    return this.relative_url(this.path, path);
  }

Because changing to this solves the issue (for me):

  if (options.relative) {
    return require('./relative_url')(this.path, path);
  }

Still got this issue.

got this error too

I was also getting this error. The fix for me was to change relative_link to false in config.yml

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lushijie picture lushijie  ·  3Comments

Helihua1992 picture Helihua1992  ·  3Comments

bearpaw picture bearpaw  ·  3Comments

cxgreat2014 picture cxgreat2014  ·  3Comments

mashirozx picture mashirozx  ·  3Comments