ruby Jekyll:如何在GitHub页面中使用自定义插件?

kmpatx3s  于 5个月前  发布在  Ruby
关注(0)|答案(5)|浏览(63)

事实证明,由于security concerns,自定义ruby插件在GitHub页面上无法工作。
我试图添加一个插件(this one)到我的Jekyll项目的_plugins文件夹中,但当我将其部署到GitHub时,它被忽略了。

**问题:**有没有办法解决这个问题?有没有人找到解决方案?
**注意:**显然我可以在本地生成html文件并将其提交到我的仓库。但这不是我想要的。

zmeyuzjn

zmeyuzjn1#

无插件

一个阅读时间脚本不需要插件。我已经创建了一个脚本集合,可以在不使用插件的情况下添加。你可以找到它们hereA reading time script是其中之一。
在这里你可以找到代码:

{% capture words %}
{{ content | number_of_words | minus: 180 }}
{% endcapture %}
{% unless words contains '-' %}
{{ words | plus: 180 | divided_by: 180 | append: ' minutes to read' }}
{% endunless %}

字符串
请注意,这段代码只包含Liquid而不包含Ruby。因此,它可以用于您的布局或包含(不含插件)。

优化脚本

假设你有这样的东西:

<p>lorem ipsum</p>
<p>lorem ipsum</p>
<code>lorem ipsum</code>
<p>lorem ipsum</p>
<code>lorem ipsum</code>
<p>lorem ipsum</p>


然后你可以像这样删除上面的代码块:

{% assign preprocessed_content=post.content | replace: '<p>', '__p__' %}
{% assign preprocessed_content=preprocessed_content | replace: '</p>', '__/p__' %}
{% assign truncated_content=preprocessed_content | strip_html %}
{% assign cleaned_content=truncated_content | replace: '__p__', '<p>' %}
{% assign cleaned_content=cleaned_content | replace: '__/p__', '</p>' %}


当然,这可以扩展到支持更多的标签。

还是要使用插件

如果你真的想使用插件,你可以让你的本地机器或CloudCannon构建你的网站,并将结果推送到Github页面。

ryhaxcpt

ryhaxcpt2#

如果你想使用自定义插件,你必须在本地“构建”你的网站,然后自己将其部署到gh-pages分支,作为HTML,CSS和其他文件的集合(不再是Markdown文件)。你可能想尝试jgd命令行来帮助你自动完成这一切。只需安装它,然后运行:

$ jgd

字符串
该站点将被打包,然后部署到您的存储库的gh-pages分支。

r1zhe5dt

r1zhe5dt3#

如果你不喜欢本地解决方案,因为它很耗时,你可以自动化一个过程,这样当你把更改推到master时,它会自动在本地构建你的网站,并把更改推到gh-pages分支。
您可以通过在repo(.git/hooks/pre-push)中创建一个pre-push钩子来实现这一点,其中包含以下内容:

#!/bin/sh

# If any command fails in the bellow script, exit with error
set -e

# Set the name of the folder that will be created in the parent
# folder of your repo folder, and which will temporarily
# hold the generated content.
temp_folder="_gh-pages-temp"

# Make sure our main code runs only if we push the master branch
if [ "$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)" == "master" ]; then
    # Store the last commit message from master branch
    last_message=$(git show -s --format=%s master)

    # Build our Jekyll site
    jekyll build

    # Move the generated site in our temp folder
    mv _site ../${temp_folder}

    # Checkout the gh-pages branch and clean it's contents
    git checkout gh-pages
    rm -rf *

    # Copy the site content from the temp folder and remove the temp folder
    cp -r ../${temp_folder}/* .
    rm -rf ../${temp_folder}

    # Commit and push our generated site to GitHub
    git add -A
    git commit -m "Built \`$last_message\`"
    git push

    # Go back to the master branch
    git checkout master
else
    echo "Not master branch. Skipping build"
fi

字符串
更多详情请点击see my blog post

e4eetjau

e4eetjau4#

在Github Pages上使用自定义插件的一个更好、更直接的解决方案是使用Github Actions。所有步骤都记录在Jekyll的官方页面上:https://jekyllrb.com/docs/continuous-integration/github-actions/。在这里复制上面文章中的步骤:
1.转到存储库上的设置选项卡。
1.在代码和自动化下,单击页面(最后一个选项)。
1.将构建和部署下的Source从分支部署更改为GitHub操作
1.转到存储库上的操作选项卡。
1.启动新工作流,搜索Jekyll
1.单击【Jekyll】工作流(非【GitHub Pages Jekyll】工作流)下的【配置】。
1.查看更改并单击提交更改
在将任何本地更改推送到默认分支时,将触发操作并开始构建。
生成可能会因为以下错误而失败:

Invalid date '<%= Time.now.strftime('%Y-%m-%d %H:%M:%S %z') %>': Document 'vendor/cache/gems/jekyll-3.2.1/lib/site_template/_posts/0000-00-00-welcome-to-jekyll.markdown.erb' does not have a valid date in the YAML front matter.

字符串
要解决这个问题,请将行exclude: [vendor]放置在_config.yml中。更多讨论请参见:https://github.com/jekyll/jekyll/issues/5267

utugiqy6

utugiqy65#

你需要一个替代这些插件。
如“Building a Series List with Hugo Shortcodes“中所述:
Ruby插件在Github页面上完全被禁用:
GitHub Pages上的插件GitHub Pages由Jekyll提供支持。

**但是,出于安全原因,所有Pages网站都是使用-safe选项来禁用自定义插件的。**不幸的是,这意味着如果您部署到GitHub Pages,您的插件将无法工作。

您仍然可以使用GitHub Pages来发布您的网站,但您需要在本地转换网站,并将生成的静态文件推送到您的GitHub存储库,而不是Jekyll源文件
我知道你提到:
显然,我可以在本地生成html文件并将它们提交到仓库中,但这不是我想要的。
尽管如此,静态网站生成器(与GitHub页面兼容)如**Hugo在您的情况下可以考虑。
R.J Lorimer添加:
Hugo有
Shortcodes**的概念,这很像Jekyll中的“液体标签”。
也像Jekyll一样,您可以创建自定义短代码标记。
然而,主要的区别是,在Hugo中,你可以创建它们,而不需要实际编写Go代码-参见Create Your Own Shortcodes
因为Hugo使用Go Templates来渲染页面,所以简码可以使用其中的任何和所有Go模板函数,以及一系列自定义的Hugo函数。这使得它可以说比liquid模板解决方案更强大,但仍然在一个模板文件中,可以轻松地动态更新。
另外,Hugo确实支持MathJax,如seen in this article
2018年11月更新:使用Hugo 0.52,此tweet confirms(参考this thread):

inline shortcode类似于Jekyll允许您在Markdown中使用Liquid标签的方式

相关问题