从Ghost博客自定义句柄助手访问帖子html

时间:2018-03-23 21:18:32

标签: javascript ajax api handlebars.js ghost-blog

Ghost Blog在将内容输出到帖子时功能有限,而且通常通过{{content}}帮助程序完成。我试图通过创建我自己的把手帮助器来为内容助手添加更多细微差别的功能,以便从ghost {{content}}帮助器中输出内容块。

我一直在利用这两种资源来创建自己的问题解决方案https://www.neoito.com/ghost-cms-on-steroids-with-custom-block-helpers/ https://github.com/TryGhost/Ghost/wiki/Apps-Getting-Started-for-Ghost-Devs

大多数情况下一切正常,但是当我尝试将html从帖子{{content}}移植到我已注册的把手帮助程序时,我遇到了麻烦。我设法使用jquery从后端通过npm在注册的帮助文件夹中安装它来进行ajax调用。该文件夹是通过第二个链接(在ghost内容文件夹中创建应用程序)中描述的方法设置的。

在索引文件中,帮助程序存储在一个单独的函数中,并在应用程序激活时调用。我的问题是让helper函数接受ajax调用并从返回的值中提取html。

我没有设置这种方法,并且有一种方法可以在第一个链接中完成(但是,它是为了解决老版鬼的问题而创建的 - pre 1.0和I&#39 ; m使用Ghost 1.2.0,因此我意识到一些"重大改变"已经完成了。

我需要一种方法从服务器端提取post html(如果可能的话)。除非在激活函数内部调用内部api,否则内部api不起作用,但是我似乎无法让它在激活函数范围之外的辅助函数中工作...我很乐意帮助我

这里我的索引文件给出了一些上下文。如果您需要更多信息,请告诉我并发布(现在我无法想到您可能需要的任何其他信息)

const $ = require('jquery');

var App = require('ghost-app'),
    hbs = require('express-hbs'),
    ghost_api = require('ghost/core/server/public/ghost-sdk'),
    proxy = require('ghost/core/server/helpers/proxy'),

helpers_briefcase;

helpers_briefcase = App.extend({

// content: function () {$.get(ghost.url.api('posts', {formats:["html"]})).done(function (data){
//     console.log('posts', data.posts["html"]);
//     return ('posts', data.posts["html"])
//     console.log('it worked');
//     debug('it worked');
//     }).fail(function (err){
//     console.log(err);
//     });
// },

//Filter handling
filters: {
   ghost_head: 'handleGhostHead',
   ghost_foot: 'handleGhostFoot'
},

handleGhostHead: function () {},
handleGhostFoot: function () {},

install: function () {

},

uninstall: function () {

},

//Register Handlebars Helpers on activate
activate: function (posts) {
    //Test for getting post content
    this.ghost.api.posts.read(0).then(function (post) {
        console.log(post.title);
        return post.title
    });
    this.ghost.helpers.register('content_block', this.content_block_helper);
    this.ghost.helpers.register('if_eq', this.if_eq);
},

deactivate: function () {

},

content_block_helper: function(node, posts) {
    var content = post.data.root.post.html;
    var regexstring = '<content_block'+ node +'>[\\s\\S]*?<\/content_block'+ 
node + '>'
    var regexp = new RegExp(regexstring);
    if(content.match(regexp)){
      var match = content.match(regexp)
      match = match.replace('<content_block'+ node + '>', '');
      match = match.replace('</content_block'+ node + '>', '');
      return new hbs.SafeString(match)
    } else {
       return('My first Handlebars Helper');
    }

},

if_eq: function(a, b, opts) {
    if (a == b) {
      return opts.fn(this);
    } else {
      return opts.inverse(this);
    }
}

});

module.exports = helpers_briefcase;

提前致谢。

免责声明:我认为自己是专业爱好者和专家之间的事情,所以在你指出我犯下的javascript罪之前,请记住这一点(轻松指出问题之外的错误我和#39;我试图解决)*感谢SO社区。

0 个答案:

没有答案