Hexo: How to access page URL inside hexo tag plugin

Created on 7 Dec 2016  路  13Comments  路  Source: hexojs/hexo

I'm creating a hexo tag plugin and need to get page url inside the tag. How can I access it?

Most helpful comment

I found that an object "this" contains some attributes:

title : foobar
date : Sun Nov 20 2016 20:36:56 GMT+0900
updated : Mon Nov 21 2016 00:00:00 GMT+0900
source : _posts/foobar.md
slug : foobar
layout : post
path : 2016/11/foobar/
permalink : https://blog.example.com/2016/11/foobar/
full_source : /home/seaoak/blog/test/source/_posts/foobar.md
asset_dir : /home/seaoak/blog/test/source/_posts/foobar/
  :
  :
(omit the rest)

For example, in the source file of the "hexo-include" tag plugin,
you can see above results with following modification:

https://github.com/PirtleShell/hexo-include/blob/master/lib/include.js

$ git diff lib/include.js
diff --git a/lib/include.js b/lib/include.js
index 450b384..7a8c8e0 100644
--- a/lib/include.js
+++ b/lib/include.js
@@ -10,6 +10,16 @@ var fs = require('hexo-fs');

 module.exports = function(ctx) {
   return function includeTag(args) {
+
+    for (var key in this) {
+         if (this[key].length && this[key].length > 100) continue;
+         if (this[key].toString) {
+               console.warn(key + ' : ' + this[key].toString());
+         } else {
+               console.warn(key + ' : ' + this[key]);
+         }
+    }
+
     var path = pathFn.join(ctx.source_dir, args[0]);

     // exit if path is not defined

I hope this information helps you.

All 13 comments

Can't use the url variable? https://hexo.io/docs/variables.html

How to access that within a plugin hexo.url ?

url is not defined inside the plugin

Base on current Hexo architecture, it is not easy to retrieve page info inside a tag plugin. How about passing in the URL or the post/page object as an argument to the tag?

A similar approach would be using a helper. https://hexo.io/api/helper.html

Can you please give me and example how to pass URL or post/page object to tag without hard cording url.

I tried using helper but helper functions doesn't work inside source.

I couldn't find such samples without hard coding URL. Why not let the user provide the URL as input? To be honest, I don't really understand why it is necessary to derive the URL inside a tag plugin.

@NoahDragon

User to pass the post URL isn't user friendly. can user pass the url as a variable parameter?

It's bit on to social sharing thing where url need to URL encoded.

Thank you.

This is the plugin I'm building https://github.com/iamchathu/hexo-tag-tweetable-quote

Now only need to add post URL to tweet.

You help highly appreciated.

I see. Hexo couldn't support it right now.

Will there be any plans to add that API support?

Recently I figure out, how about using Hexo filter? You could add a placeholder for the permalink. Then register a filter to replace it with actual URL.

Inspired by the excerpt filter:
https://github.com/hexojs/hexo/blob/master/lib/plugins/filter/after_post_render/excerpt.js

I found that an object "this" contains some attributes:

title : foobar
date : Sun Nov 20 2016 20:36:56 GMT+0900
updated : Mon Nov 21 2016 00:00:00 GMT+0900
source : _posts/foobar.md
slug : foobar
layout : post
path : 2016/11/foobar/
permalink : https://blog.example.com/2016/11/foobar/
full_source : /home/seaoak/blog/test/source/_posts/foobar.md
asset_dir : /home/seaoak/blog/test/source/_posts/foobar/
  :
  :
(omit the rest)

For example, in the source file of the "hexo-include" tag plugin,
you can see above results with following modification:

https://github.com/PirtleShell/hexo-include/blob/master/lib/include.js

$ git diff lib/include.js
diff --git a/lib/include.js b/lib/include.js
index 450b384..7a8c8e0 100644
--- a/lib/include.js
+++ b/lib/include.js
@@ -10,6 +10,16 @@ var fs = require('hexo-fs');

 module.exports = function(ctx) {
   return function includeTag(args) {
+
+    for (var key in this) {
+         if (this[key].length && this[key].length > 100) continue;
+         if (this[key].toString) {
+               console.warn(key + ' : ' + this[key].toString());
+         } else {
+               console.warn(key + ' : ' + this[key]);
+         }
+    }
+
     var path = pathFn.join(ctx.source_dir, args[0]);

     // exit if path is not defined

I hope this information helps you.

@seaoak Answer perfectly works. this object has the property permalink which i wanted.

Was this page helpful?
0 / 5 - 0 ratings