繁体   English   中英

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

[英]Jekyll: How to use custom plugins with GitHub pages?

事实证明,出于安全考虑,自定义 ruby​​ 插件在 GitHub 页面上不起作用。

我正在尝试向我的 Jekyll 项目的_plugins文件夹添加一个插件(这个),但是当我将它部署到 GitHub 时它被忽略了。

问题:有没有办法解决这个问题? 有没有人找到解决办法?

注意:显然我可以在本地生成 html 文件并将它们提交到我的存储库。 但这不是我想要的。

没有插件

阅读时间脚本不需要插件。 我创建了一组无需使用插件即可添加的脚本。 您可以在此处找到它们。 阅读时间脚本就是其中之一。

在这里你可以找到代码:

{% 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 Pages。 另见: https : //learn.cloudcannon.com/jekyll/using-jekyll-plugins/

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

$ jgd

该站点将被打包,然后部署到您的 repo 的gh-pages分支。 在我的这篇博文中了解更多信息:将Jekyll 部署到 GitHub Pages

如果你想让Jekyll 站点像本地一样运行,比如让自定义插件正常工作,这里有一种非常方便的方法来构建 Jekyll 站点并将其部署到 Github Pages。

🪂 一个 Github Action 可以方便地为 GitHub Pages 部署 Jekyll 站点。 https://github.com/jeffreytse/jekyll-deploy-action

有了这个动作,我想你的问题就可以完美解决了。

您需要这些插件的替代品。

如“使用 Hugo Shortcodes 构建系列列表”中所述:

在 Github 页面上完全禁用 Ruby 插件执行:

GitHub Pages 上的插件 GitHub Pages 由 Jekyll 提供支持。
但是,出于安全原因,所有 Pages 站点都是使用-safe选项生成的,以禁用自定义插件 不幸的是,这意味着如果您部署到 GitHub Pages,您的插件将无法工作。

您仍然可以使用 GitHub Pages 来发布您的站点,但您需要在本地转换站点并将生成的静态文件推送到您的 GitHub 存储库而不是 Jekyll 源文件

我明白你提到:

显然我可以在本地生成 html 文件并将它们提交到我的存储库。 但这不是我想要的。

尽管如此,在您的情况下仍应考虑使用像Hugo这样的静态网站生成器(与 GitHub 页面兼容)。

RJ Lorimer补充道:

Hugo 有Shortcodes的概念,它很像 Jekyll 中的“液体标签”。
也像 Jekyll 一样,您可以创建自定义短代码标签。

但是,主要区别在于,在 Hugo 中,您无需实际编写 Go 代码即可创建它们 - 请参阅创建您自己的短代码
因为 Hugo 使用 Go 模板来渲染页面,短代码可以使用其中的任何和所有 Go 模板函数,以及添加的完整自定义 Hugo 函数列表以提供帮助。 这使得它可以说比液体模板解决方案更强大,但仍然在一个可以轻松动态更新的模板文件中。

另外,Hugo 确实支持 MathJax ,如本文所示

2018 年 11 月更新:使用Hugo 0.52 ,这条推文确认(参考 此线程):

内联简码类似于 Jekyll 允许您在 Markdown 中使用 Liquid 标签的方式

如果您不喜欢本地解决方案,因为它很耗时,您可以自动化一个过程,这样当您将更改推送到 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

有关更多详细信息,请参阅我的博客文章

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM