Total Pageviews

Sunday 6 March 2022

基于elixir 的静态网站程序NimblePublisher

A minimal filesystem-based publishing engine with Markdown support.

Online Documentation.

NimblePublisher is a minimal filesystem-based publishing engine with Markdown support and code highlighting.

use NimblePublisher,
  build: Article,
  from: Application.app_dir(:app_name, "priv/articles/**/*.md"),
  as: :articles,
  highlighters: [:makeup_elixir, :makeup_erlang]

The example above will get all articles in the given directory, call Article.build/3 for each article, passing the filename, the metadata, and the article body, and define a module attribute named @articles with all built articles returned by the Article.build/3 function.

Each article in the articles directory must have the format:

%{
  title: "Hello world"
}
---
Body of the "Hello world" article.

This is a *markdown* document with support for code highlighters:

```elixir
IO.puts "hello world".
```

Options

  • :build - the name of the module that will build each entry

  • :from - a wildcard pattern where to find all entries. Files with the .md or .markdown extension will be converted to Markdown with Earmark. Other files will be kept as is.

  • :as - the name of the module attribute to store all built entries

  • :highlighters - which code highlighters to use. NimblePublisher uses Makeup for syntax highlighting and you will need to add its .css classes. You can generate the CSS classes by calling Makeup.stylesheet(:vim_style, "makeup") inside iex -S mix. You can replace :vim_style by any style of your choice defined here.

  • :earmark_options - an %Earmark.Options{} struct

  • :parser - custom module with a parse/2 function that receives the file path and content as params. It must return a 2 element tuple with attributes and body.

Examples

Let's see a complete example. First add nimble_publisher with the desired highlighters as a dependency:

def deps do
  [
    {:nimble_publisher, "~> 0.1.0"},
    {:makeup_elixir, ">= 0.0.0"},
    {:makeup_erlang, ">= 0.0.0"}
  ]
end

In this example, we are building a blog. Each post stays in the "posts" directory with the format:

/posts/YEAR/MONTH-DAY-ID.md

A typical blog post will look like this:

# /posts/2020/04-17-hello-world.md
%{
  title: "Hello world!",
  author: "José Valim",
  tags: ~w(hello),
  description: "Let's learn how to say hello world"
}
---
This is the post.

Therefore, we will define a Post struct that expects all of the fields above. We will also have a :date field that we will build from the filename. 

from https://github.com/dashbitco/nimble_publisher

-----

demo site: https://elixirschool.com/blog

the demo site's source code: https://github.com/elixirschool/elixirschool

No comments:

Post a Comment