20 个版本

0.2.1 2024年1月20日
0.2.0 2024年1月20日
0.1.7 2023年8月12日
0.1.5 2023年7月26日
0.0.8 2023年6月18日

#51模板引擎

Download history 4/week @ 2024-07-06

87 每月下载量

MIT/Apache

3MB
1.5K SLoC

RustyInk

Art by DALL·E

crates.io Crates.io Build & test Publish to Pages

注意 我正在公开构建这个项目。您可以在 Twitter 上关注进度 @arjunz

A sleek and minimalist static site generator written in Rust. Designed with simplicity in mind, RustyInk makes website creation a breeze.

这里是一个 实时演示,我的博客是用 RustyInk 构建的。

特性

  • Markdown 支持
  • 自定义主题
  • SEO 优化(OpenGraph,Twitter Cards,Sitemap)
  • AMP 支持
  • 热重载
  • 自定义元数据传递

安装

您可以使用 Cargo 安装 RustyInk

cargo install rustyink

使用方法

创建新项目

您可以使用 new 命令初始化新项目。

rustyink new <folder>

您还可以指定一个主题。

rustyink new <folder> -t pico

项目结构

RustyInk 预期以下文件夹结构

docs/
├─ public/
├─ pages/
│  ├─ page.md
│  ├─ path/
│  │  ├─ page.md
│  │  ├─ custom-url.md
├─ theme/
│  ├─ global.css
│  ├─ app.hbs
│  ├─ custom-template.hbs
├─ Settings.toml

以下是 docs 文件夹,它是项目的输入目录,始终在运行开发服务器或构建时指定。您可以通过这种方式指定不同的输入目录

rustyink dev <input-dir-path>
  • Settings.toml 文件包含网站的设置,您可以通过更改此文件中的值来自定义网站。
  • public 文件夹包含网站的所有静态资产,这些文件将原样复制到输出目录。
  • pages 文件夹包含所有 Markdown 文件,这是您编写内容的地方。
  • theme 文件夹包含所有网站模板和样式。它使用 handlebars 语法编写。
  • global.css 文件包含网站的全球 CSS,您可以在该文件中编写自己的 CSS。

构建自定义页面

一个很好的例子是博客索引页面,其中您显示一系列帖子并将它们链接起来。这可以通过访问传递给每个页面的网站目录来实现。可以通过根对象访问网站目录,这在每个页面上都可用,它代表整个网站结构及其元数据,因此我可以这样渲染一个博客索引页面

自定义模板称为blog,列出了blog文件夹下的所有页面。

<ul>
  {{#each root.blog}}
    {{#if (not (eq @key "_self"))}}
      <hgroup>
        <h4><a href="{{@key}}/">{{this.title}}</a></h4>
        <h2>{{this.author}}</h2>
      </hgroup>
    {{/if}}
  {{/each}}
</ul>

然后在blog文件夹下定义一个新页面,并将模板指定为上述创建的blog

--
template: blog
title: ~/RustyInk/blog
--

### This is a blog index

《Settings.toml》文件

Settings.toml 文件包含网站的设置,您可以通过更改此文件中的值来自定义网站。

[dev]
port = 3000 # The port on which the dev server runs
ws_port = 3001 # The port on which the dev server websocket runs, for hot reloading

[site]
script_urls = [] # List of script urls to be included in the site
style_urls = [ # List of style urls to be included in the site
  'https://cdn.jsdelivr.net.cn/npm/@picocss/pico@1/css/pico.min.css',
  'https://cdn.jsdelivr.net.cn/npm/prismjs@1.29.0/themes/prism-tomorrow.min.css',
]

[meta]
title = "~/RustyInk" # The title of the website
description = "Blazing fast static site generator written in Rust" # The description of the website
og_image_url = "https://rustyink.cli.rs/images/og.png" # The og image url of the website
base_url = "https://rustyink.cli.rs" # The base url of the website, used for building sitemap

[navigation] # The navigation links of the website
links = [
  { label = "~/", url = "/" },
  { label = "GitHub", url = "https://github.com/arjunkomath/rustyink" },
  { label = "Twitter", url = "https://twitter.com/arjunz" },
  { label = "Blog", url = "/blog/" },
  { label = "About", url = "/about/" },
]

[data] # The data to be passed to every page, can be accessed using `data` object in every page
author = "Arjun Komath"
author_link = "https://twitter.com/arjunz"

[remote_data] # The remote data to be fetched and passed to every page, can be accessed using `remote_data` object
repo_meta = "https://api.github.com/repos/arjunkomath/rustyink" # The url of the remote data

AMP支持

AMP支持是内置的,您可以通过将amp: true添加到页面元数据中来启用页面的AMP。在构建AMP页面时,将使用amp模板而不是app作为基本模板,该模板应存在于theme文件夹中。您可以在文档文件夹中找到一个示例。

Handlebars辅助函数

RustyInk提供了一些handlebars辅助函数,使您的生活更加轻松。此项目使用handlebars-rust,因此它提供的所有辅助函数都可用。除此之外,RustyInk还提供了以下辅助函数

  • slice:切分数组并返回切分的数组。
  • sort-by:按键对对象数组进行排序。
  • format-date:使用给定的格式格式化日期。
  • stringify:将值转换为字符串,这在调试中非常有用。

您可以在示例项目中找到这些辅助函数的示例。

部署

您可以使用构建命令构建网站

rustyink build <input-dir-path>

构建输出保存在_site文件夹中。因此,您可以通过将_site文件夹复制到您的Web服务器来部署网站。您还可以使用GitHub pages托管您的网站。以下是一个将您的网站部署到GitHub pages的GitHub动作示例

# Simple workflow for deploying static content to GitHub Pages
name: Publish to Pages

on:
  # Runs on pushes targeting the default branch
  push:
    branches: ["main"]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
  group: "pages"
  cancel-in-progress: false

jobs:
  # Single deploy job since we're just deploying
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Setup Pages
        uses: actions/configure-pages@v3
      - name: Install
        run: cargo install rustyink
      - name: Build
        run: rustyink build src # Replace src with your input directory
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v1
        with:
          # Upload entire repository
          path: './_site'
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v2

许可协议

您可以在此处找到许可协议。

依赖关系

~26–40MB
~711K SLoC