{"id":13760616,"url":"https://github.com/dashbitco/nimble_publisher","last_synced_at":"2025-05-14T20:00:18.640Z","repository":{"id":41656480,"uuid":"271851168","full_name":"dashbitco/nimble_publisher","owner":"dashbitco","description":"A minimal filesystem-based publishing engine with Markdown support and code highlighting","archived":false,"fork":false,"pushed_at":"2025-04-15T10:30:43.000Z","size":45,"stargazers_count":505,"open_issues_count":0,"forks_count":32,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-05-07T19:47:27.436Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dashbitco.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-06-12T17:10:56.000Z","updated_at":"2025-05-07T08:34:02.000Z","dependencies_parsed_at":"2023-01-21T03:00:37.600Z","dependency_job_id":"51d22909-176d-44a7-95ef-483e81b9fd32","html_url":"https://github.com/dashbitco/nimble_publisher","commit_stats":{"total_commits":22,"total_committers":10,"mean_commits":2.2,"dds":0.5,"last_synced_commit":"65f8712c895dbcd5523770f10138c4b9dde0aa4d"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dashbitco%2Fnimble_publisher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dashbitco%2Fnimble_publisher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dashbitco%2Fnimble_publisher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dashbitco%2Fnimble_publisher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dashbitco","download_url":"https://codeload.github.com/dashbitco/nimble_publisher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254219350,"owners_count":22034394,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T13:01:14.208Z","updated_at":"2025-05-14T20:00:18.478Z","avatar_url":"https://github.com/dashbitco.png","language":"Elixir","funding_links":[],"categories":["Elixir","Static Page Generation"],"sub_categories":[],"readme":"# NimblePublisher\n\n[Online Documentation](https://hexdocs.pm/nimble_publisher).\n\n\u003c!-- MDOC !--\u003e\n\n`NimblePublisher` is a minimal filesystem-based publishing engine with Markdown support and\ncode highlighting.\n\n```elixir\nuse NimblePublisher,\n  build: Article,\n  from: Application.app_dir(:app_name, \"priv/articles/**/*.md\"),\n  as: :articles,\n  highlighters: [:makeup_elixir, :makeup_erlang]\n```\n\nThe example above will get all articles in the given directory,\ncall `Article.build/3` for each article, passing the filename,\nthe metadata, and the article body, and define a module attribute\nnamed `@articles` with all built articles returned by the\n`Article.build/3` function.\n\nEach article in the articles directory must have the format:\n\n    %{\n      title: \"Hello world\"\n    }\n    ---\n    Body of the \"Hello world\" article.\n\n    This is a *markdown* document with support for code highlighters:\n\n    ```elixir\n    IO.puts \"hello world\"\n    ```\n\n## Options\n\n  * `:build` - the name of the module that will build each entry\n\n  * `:from` - a wildcard pattern where to find all entries. Files with the\n    `.md` or `.markdown` extension will be converted to HTML with\n    `Earmark`. Other files will be kept as is.\n\n  * `:as` - the name of the module attribute to store all built entries\n\n  * `:highlighters` - which code highlighters to use. `NimblePublisher`\n    uses `Makeup` for syntax highlighting and you will need to add its\n    `.css` classes. You can generate the CSS classes by calling\n    `Makeup.stylesheet(:vim_style, \"makeup\")` inside `iex -S mix`.\n    You can replace `:vim_style` by any style of your choice\n    [defined here](https://elixir-makeup.github.io/makeup_demo/elixir.html).\n\n  * `:earmark_options` - an [`%Earmark.Options{}`](https://hexdocs.pm/earmark/Earmark.Options.html) struct\n\n  * `:parser` - custom module with a `parse/2` function that receives the file path\n    and content as params. See [Custom parser](#module-custom-parser) for more details.\n\n  * `:html_converter` - custom module with a `convert/4` function that receives the\n    extension, body, and attributes of the markdown file, as well as all options\n    as params. See [Custom HTML converter](#module-custom-html-converter) for more details.\n\n## Examples\n\nLet's see a complete example. First add `nimble_publisher` with\nthe desired highlighters as a dependency:\n\n    def deps do\n      [\n        {:nimble_publisher, \"~\u003e 1.0\"},\n        {:makeup_elixir, \"\u003e= 0.0.0\"},\n        {:makeup_erlang, \"\u003e= 0.0.0\"}\n      ]\n    end\n\nIn this example, we are building a blog. Each post stays in the\n\"posts\" directory with the format:\n\n    /posts/YEAR/MONTH-DAY-ID.md\n\nA typical blog post will look like this:\n\n    # /posts/2020/04-17-hello-world.md\n    %{\n      title: \"Hello world!\",\n      author: \"José Valim\",\n      tags: ~w(hello),\n      description: \"Let's learn how to say hello world\"\n    }\n    ---\n    This is the post.\n\nTherefore, we will define a Post struct that expects all of the fields\nabove. We will also have a `:date` field that we will build from the\nfilename. Overall, it will look like this:\n\n```elixir\ndefmodule MyApp.Blog.Post do\n  @enforce_keys [:id, :author, :title, :body, :description, :tags, :date]\n  defstruct [:id, :author, :title, :body, :description, :tags, :date]\n\n  def build(filename, attrs, body) do\n    [year, month_day_id] = filename |\u003e Path.rootname() |\u003e Path.split() |\u003e Enum.take(-2)\n    [month, day, id] = String.split(month_day_id, \"-\", parts: 3)\n    date = Date.from_iso8601!(\"#{year}-#{month}-#{day}\")\n    struct!(__MODULE__, [id: id, date: date, body: body] ++ Map.to_list(attrs))\n  end\nend\n```\n\nNow, we are ready to define our `MyApp.Blog` with `NimblePublisher`:\n\n```elixir\ndefmodule MyApp.Blog do\n  alias MyApp.Blog.Post\n\n  use NimblePublisher,\n    build: Post,\n    from: Application.app_dir(:my_app, \"priv/posts/**/*.md\"),\n    as: :posts,\n    highlighters: [:makeup_elixir, :makeup_erlang]\n\n  # The @posts variable is first defined by NimblePublisher.\n  # Let's further modify it by sorting all posts by descending date.\n  @posts Enum.sort_by(@posts, \u0026 \u00261.date, {:desc, Date})\n\n  # Let's also get all tags\n  @tags @posts |\u003e Enum.flat_map(\u0026 \u00261.tags) |\u003e Enum.uniq() |\u003e Enum.sort()\n\n  # And finally export them\n  def all_posts, do: @posts\n  def all_tags, do: @tags\nend\n```\n\n**Important**: Avoid injecting the `@posts` attribute into multiple functions,\nas each call will make a complete copy of all posts. For example, if you want\nto define `recent_posts()` as well as `all_posts()`, DO NOT do this:\n\n```elixir\ndef all_posts, do: @posts\ndef recent_posts, do: Enum.take(@posts, 3)\n```\n\nInstead do this:\n\n```elixir\ndef all_posts, do: @posts\ndef recent_posts, do: Enum.take(all_posts(), 3)\n```\n\n### Other helpers\n\nYou may want to define other helpers to traverse your published resources.\nFor example, if you want to get posts by ID or with a given tag, you can\ndefine additional functions as shown below:\n\n```elixir\ndefmodule NotFoundError do\n  defexception [:message, plug_status: 404]\nend\n\ndef get_post_by_id!(id) do\n  Enum.find(all_posts(), \u0026(\u00261.id == id)) ||\n    raise NotFoundError, \"post with id=#{id} not found\"\nend\n\ndef get_posts_by_tag!(tag) do\n  case Enum.filter(all_posts(), \u0026(tag in \u00261.tags)) do\n    [] -\u003e raise NotFoundError, \"posts with tag=#{tag} not found\"\n    posts -\u003e posts\n  end\nend\n```\n\n### Custom parser\n\nYou may want to define a custom function to parse the content of your files.\n\n```elixir\n  use NimblePublisher,\n    ...\n    parser: Parser,\n\ndefmodule Parser do\n  def parse(path, contents) do\n    [attrs, body] = :binary.split(contents, [\"\\n---\\n\"])\n    {Jason.decode!(attrs), body}\n  end\nend\n```\n\nThe `parse/2` function from this module receives the file path and content as params.\nIt must return:\n\n  * a 2 element tuple with attributes and body - `{attrs, body}`\n  * a list of 2 element tuple with attributes and body - `[{attrs, body} | _]`\n\n### Custom HTML converter\n\nYou can also define a custom HTML converter that will be used to convert the\nfile body (typically Markdown) into HTML. For example, you may wish to use an\nalternative Markdown parser such as [md](https://github.com/am-kantox/md).\nIf you want to use the built-in highlighting, you need to call it manually.\n\n```elixir\n  use NimblePublisher,\n    ...\n    html_converter: MarkdownConverter,\n    highlighters: [:makeup_elixir]\n```\n\n```elixir\ndefmodule MarkdownConverter do\n  def convert(filepath, body, _attrs, opts) do\n    if Path.extname(filepath) in [\".md\", \".markdown\"] do\n      highlighters = Keyword.get(opts, :highlighters, [])\n      body |\u003e Md.generate() |\u003e NimblePublisher.highlight(highlighters)\n    end\n  end\nend\n```\n\nThe `convert/4` function from this module receives an extension name, a body,\nthe parsed attributes from the file, and the options passed to\n`NimblePublisher`. It must return the converted body as a string.\n\n### Live reloading\n\nIf you are using Phoenix, you can enable live reloading by simply telling Phoenix to watch the “posts” directory. Open up \"config/dev.exs\", search for `live_reload:` and add this to the list of patterns:\n\n```elixir\nlive_reload: [\n  patterns: [\n    ...,\n    ~r\"posts/*/.*(md)$\"\n  ]\n]\n```\n\n## Learn more\n\n  * [Dashbit's blog post which was the foundation for NimblePublisher](https://dashbit.co/blog/welcome-to-our-blog-how-it-was-made)\n  * [Elixir School's lesson on using NimblePublisher, complete with Phoenix integration](https://elixirschool.com/en/lessons/libraries/nimble-publisher/)\n\n\u003c!-- MDOC !--\u003e\n\n## Nimble*\n\nAll nimble libraries by Dashbit:\n\n  * [NimbleCSV](https://github.com/dashbitco/nimble_csv) - simple and fast CSV parsing\n  * [NimbleOptions](https://github.com/dashbitco/nimble_options) - tiny library for validating and documenting high-level options\n  * [NimbleParsec](https://github.com/dashbitco/nimble_parsec) - simple and fast parser combinators\n  * [NimblePool](https://github.com/dashbitco/nimble_pool) - tiny resource-pool implementation\n  * [NimblePublisher](https://github.com/dashbitco/nimble_publisher) - a minimal filesystem-based publishing engine with Markdown support and code highlighting\n  * [NimbleTOTP](https://github.com/dashbitco/nimble_totp) - tiny library for generating time-based one time passwords (TOTP)\n\n## License\n\nCopyright 2020 Dashbit\n\n  Licensed under the Apache License, Version 2.0 (the \"License\");\n  you may not use this file except in compliance with the License.\n  You may obtain a copy of the License at\n\n      http://www.apache.org/licenses/LICENSE-2.0\n\n  Unless required by applicable law or agreed to in writing, software\n  distributed under the License is distributed on an \"AS IS\" BASIS,\n  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n  See the License for the specific language governing permissions and\n  limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdashbitco%2Fnimble_publisher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdashbitco%2Fnimble_publisher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdashbitco%2Fnimble_publisher/lists"}