Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prepend root to image path #111

Merged
merged 9 commits into from
Sep 23, 2019
Merged

feat: prepend root to image path #111

merged 9 commits into from
Sep 23, 2019

Conversation

curbengh
Copy link
Contributor

@curbengh curbengh commented Sep 7, 2019

encodeURL is added in hexo-util 1.1.0.

url_for is added in hexo-util 1.2.0.

This feature can be enabled through prependRoot option, which is disabled by default.

@curbengh
Copy link
Contributor Author

curbengh commented Sep 7, 2019

@hexojs/core
Anyone know how can this renderer access url_for() helper?

@coveralls
Copy link

coveralls commented Sep 7, 2019

Coverage Status

Coverage increased (+1.8%) to 87.5% when pulling 788db1b on curbengh:image into bdb075b on hexojs:master.

@segayuu
Copy link
Contributor

segayuu commented Sep 7, 2019

I'm not testing it, but I think you can get it with hexo.extend.helper.get('url_for').

@curbengh
Copy link
Contributor Author

curbengh commented Sep 7, 2019

yeah, where do I put it? How does renderer.js access hexo?

@segayuu
Copy link
Contributor

segayuu commented Sep 7, 2019

hexo loads the hexo plug-in in node_modules (from the file registered in main), but forcibly passes the hexo object. Since other files cannot be automatically acquired when loaded, the plug-in itself must acquire the hexo object on its own.

@curbengh
Copy link
Contributor Author

curbengh commented Sep 7, 2019

I can load url_for with

const url_for = require('hexo/lib/plugins/helper/url_for')
// or
const url_for =  require('../../../node_modules/hexo/lib/plugins/helper/url_for')

But url_for can't access the config.root

TypeError: Cannot destructure property `config` of 'undefined' or 'null'.
    at urlForHelper (/home/user/dev/hexo-testing/node_modules/hexo/lib/plugins/helper/url_for.js:10:9)
    at Renderer.paragraph (/home/user/dev/hexo-testing/node_modules/hexo-renderer-marked/lib/renderer.js:90:12)

Edit: Fixed

@curbengh curbengh changed the title prepend root to image path feat: prepend root to image path Sep 8, 2019
@curbengh curbengh marked this pull request as ready for review September 8, 2019 02:23
@curbengh
Copy link
Contributor Author

curbengh commented Sep 8, 2019

This PR could also be added to Renderer.prototype.link, in a separate PR.

@curbengh curbengh changed the title feat: prepend root to image path WIP feat: prepend root to image path Sep 8, 2019
@curbengh curbengh changed the title WIP feat: prepend root to image path feat: prepend root to image path Sep 8, 2019
@curbengh
Copy link
Contributor Author

curbengh commented Sep 8, 2019

Doesn't work with relative_link yet, haven't figure out how to parse this.path value.

We can make a note prependRoot is not compatible with relative_link.

@SukkaW
Copy link
Member

SukkaW commented Sep 8, 2019

#111 (comment)

@curbengh

https://hexo.io/api/helper#How-do-I-use-another-registered-helper-inside-my-custom-helper

Try this?

const url_for = hexo.extend.helper.get('url_for').bind(hexo);

@curbengh
Copy link
Contributor Author

curbengh commented Sep 8, 2019

Thanks. The latest commit resolve it. Currently I'm stuck at relative_link which uses relative_url() helper.

  const url_forCfg = Object.assign({}, {
    config: {
      root: this.config.root,
      relative_link: this.config.relative_link
    },
    path: data.path,
    relative_url: this.extend.helper.get('relative_url')
  });

But data.path in this case is file path, e.g. /home/user/project/source/_posts/hello-world.md, which results in extra ../../. It's not url path /2018/01/01/hello-world/index.html as used by relative_link(). I guess url path is only known after render.

Edit: I added a check so that url_for() doesn't execute when relative_link is enabled.

Edit: I just realized url_for() is actually separate from relative_link, meaning url_for() will execute relative_url() and (sort of) skip itself when relative_link is enabled. relative_url() could not function here in renderer due to lack of path.

@curbengh
Copy link
Contributor Author

curbengh commented Sep 11, 2019

Pending hexojs/hexo-util#82

Edit: Now utilize hexo-util url_for().

@curbengh curbengh requested a review from a team September 18, 2019 10:50
lib/renderer.js Outdated Show resolved Hide resolved
@curbengh curbengh merged commit 6fe419b into hexojs:master Sep 23, 2019
@curbengh curbengh deleted the image branch September 23, 2019 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants