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

Is it possible to refer a config variable in Markdown content? #2756

Closed
Muyangmin opened this Issue Sep 14, 2017 · 12 comments

Comments

6 participants
@Muyangmin
Copy link

Muyangmin commented Sep 14, 2017

Environment Info

Node version(node -v): v6.5.0

Hexo and Plugin version(npm ls --depth 0):
├── hexo@3.3.9
├── hexo-deployer-git@0.3.1
├── hexo-generator-archive@0.1.4
├── hexo-generator-category@0.1.3
├── hexo-generator-index@0.2.1
├── hexo-generator-tag@0.2.0
├── hexo-renderer-ejs@0.3.1
├── hexo-renderer-marked@0.3.0
├── hexo-renderer-stylus@0.3.3
└── hexo-server@0.2.2

For question

I have a translation blog powered by Hexo. In many articles I need to provide links to original English pages, and most of them starts with the same prefix. Here is a example:

This [content][1] is available on [GitHub][2].

// in the page end 
[1]: https://example.com/blob/master/example
[2]: https://example.com/example

In Jekyll maybe we can use this:

[1]: { site.url }/blob/master/example
[2]: { site.url }/example

I try to define a var named userDefUrl in config.yml and use {% config.userDefUrl %} in the page content(NOT front-matter), and get a render error:

Template render error: (unknown path) [Line 2, Column 160]
unknown block tag: config
at Object.exports.prettifyError...

I've searching for similar issues, and found #2209 , but seems not matching my case. Does Hexo actually support this usage? Thanks!


以下为中文(貌似Hexo有很多中文大神在维护,因此再用中文描述一遍)

我尝试使用Hexo为某个项目做翻译站,但是翻译文章需要大量引用原文链接,而原文链接基本上都是开始于同一个网站地址,原项目使用Jekyll, 它的引用方式是 {site.url}/relative_links。我希望在我的Hexo站点上也能这么使用,避免硬编码站点地址,也可以减小译文和原文的差异。

但是我在 #2209 上看到的方式似乎只是在页面模板中使用。请问在Markdown文件中能否套用这种方案(即使需要额外插件也可以)?

谢谢指教!

@NoahDragon NoahDragon added the question label Sep 14, 2017

@NoahDragon

This comment has been minimized.

Copy link
Member

NoahDragon commented Sep 14, 2017

可以写个tag来实现,但好像目前没有其它好办法,因为Post中以文本信息为主,并不支持编程。

@Muyangmin

This comment has been minimized.

Copy link

Muyangmin commented Sep 15, 2017

@NoahDragon 谢谢,不过我不是很明白,是写个标签插件么?

我目前的想法是在 config.yml里定义一个特殊配置:

replace:
  from: { site.url }
  to: https://example.com

然后写一个脚本文件,提取文本中的这些值并替换它。

我仔细阅读了 插件文档, 以及这篇文章,但还是不很清楚,感觉这个思路可以达成,就是不知道这个脚本应该怎么写:

  1. 它如何读取配置?
  2. 它如何提取文本内容? (cheerio?)
  3. 它应该在什么时机调用?

抱歉,我对Hexo和前端确实不熟悉,可能这些问题在您看来很基础,但能否提供另一些相关资料呢?

@NoahDragon

This comment has been minimized.

Copy link
Member

NoahDragon commented Sep 15, 2017

@Muyangmin 是的,通过标签插件访问config参数。
由于md文件主要是用来写文章,如果再加入访问变量参数等功能,会有些混淆,无法很好的分离数据和逻辑。
这里应该还不用插件,只需要脚本即可。
如何使用脚本请参考Hexo自身网站脚本https://github.com/hexojs/site/blob/master/scripts/tags.js
如果还有问题,请尽管提出。

@Muyangmin

This comment has been minimized.

Copy link

Muyangmin commented Sep 15, 2017

@NoahDragon 问题解决了一半……
我在项目路径下创建了一个目录 scripts,在其中新建了一个site_url.js,以下是内容:

/* global hexo */

'use strict';

hexo.extend.tag.register("site_url", function (args, content) {
    return "http://example.com";
})

然后在post文章正文中这么使用:

<!-- 这里尝试直接输出 -->
{% site_url %}

额外添加[``选项(Options)``][2]

[2]: {% site_url %}/somelink

最终结果:

http://example.com

额外添加[选项(Options)][2]

[2]: http://example.com/somelink

可以看到,标签内容确实插入成功了。不过插入时机不能满足我的想法,我希望在Markdown文件解析之前替换,这样[2]的链接才能正确解析,请问可以做到么?

@apei830

This comment has been minimized.

Copy link

apei830 commented Nov 13, 2017

请问你这个问题完美解决了没?我也有类似的需求

@unnamed42

This comment has been minimized.

Copy link

unnamed42 commented Jan 3, 2018

@Muyangmin 注册针对before_post_render的filter插件即可,修改data.content就行了

@Muyangmin

This comment has been minimized.

Copy link

Muyangmin commented Jan 3, 2018

@unnamed42 谢谢 👍

@Muyangmin Muyangmin closed this Jan 3, 2018

@g13013

This comment has been minimized.

Copy link

g13013 commented Dec 26, 2018

Japanese ! realy ?

@YoshinoriN

This comment has been minimized.

Copy link
Member

YoshinoriN commented Dec 27, 2018

Japanese ! realy ?

No, it's chinese :)
But, I pretty much understand what they say. Do you need a help about this issue ?

@g13013

This comment has been minimized.

Copy link

g13013 commented Jan 2, 2019

@YoshinoriN thank you very much! I thought it would be helpful to access some configuration entries from markdown files ex: root

@YoshinoriN

This comment has been minimized.

Copy link
Member

YoshinoriN commented Jan 6, 2019

@g13013

Currently hexo can not get _config.yml valuse.
But you can get these valuse using by tag helper make by yourself like this.

1. Create tag register helper

At first, create tag helper's JavaScript and put it theme/script.

'use strict';
const config = hexo.config;

hexo.extend.tag.register("site_url",() => {
    return config.url;
});

2. use helper

Second, use that tag helper in your post like this.

{% site_url %}

Other

I wrote example. Please see it.

@YoshinoriN

This comment has been minimized.

Copy link
Member

YoshinoriN commented Jan 7, 2019

I made a plugin.
Please try it if you'd like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment