Show HN:一个独立/GitHub 命令行扩展程序,用于预览GitHub风格的Markdown
Show HN: A Standalone/GitHub CLI Extension to Preview GitHub Flavored Markdown

原始链接: https://github.com/thiagokokada/gh-gfm-preview

`gh-gfm-preview` 是一个 Go 语言编写的程序,用于离线预览 GitHub Flavored Markdown (GFM) 文件。作为 `gh-markdown-preview` 的硬分支,它优先考虑离线功能和速度,通过使用 `yuin/goldmark` 和自定义扩展来渲染 Markdown,以模拟 GitHub 的渲染效果。它可以作为独立的二进制文件或 GitHub CLI 扩展使用。 主要功能包括:离线运行、速度快、零依赖、无需配置、实时重载、自动打开浏览器、端口查找以及优雅降级。使用方式很简单,只需在包含 Markdown 文件的目录中运行命令,或指定文件路径即可。它会自动打开浏览器窗口显示渲染后的输出。 配置选项允许强制使用深色/浅色模式,禁用自动打开/重载,指定主机名、端口或启用详细输出。该程序的静态二进制文件使其非常适合与 Neovim 等编辑器集成,以实现实时 Markdown 预览。

Hacker News 最新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Show HN:一个独立/GitHub 命令行扩展程序,用于预览 GitHub Flavored Markdown (github.com/thiagokokada) 7 分 kokada 发布 3 小时前 | 隐藏 | 过去 | 收藏 | 讨论 加入我们,参加 6 月 16-17 日在旧金山举办的 AI 初创公司学校! 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系我们 搜索:

原文

A Go program to preview GitHub Flavored Markdown (GFM) 📓.

The gh-gfm-preview command start a local web server to serve the markdown document. gh gfm-preview renders the HTML using yuin/goldmark and some extensions and frontend tricks to have similar features and look to how GitHub renders a markdown.

It may also be used as a GitHub CLI extension.

This is a hard fork of yusukebe/gh-markdown-preview, that uses the GitHub Markdown API, but this means it doesn't work offline. The code of this repository tries to emulate the look of GitHub Markdown rendering as close as possible, but the original project will be even closer to the actual result if you don't need offline rendering.

  • Works offline - You don't need an internet connection.
  • Fast - Since it doesn't rely on external services it is really fast.
  • No-dependencies - You can just run the standalone binary (or optionally via gh as an extension).
  • Zero-configuration - You don't have to set the GitHub access token.
  • Live reloading - You don't need reload the browser.
  • Auto open browser - Your browser will be opened automatically.
  • Auto find port - You don't need find an available port if default is used.
  • Graceful degradation - Basic functionality works even without JavaScript.

You need to have Go installed.

go run github.com/thiagokokada/gh-gfm-preview

Assuming that you have Flakes enabled:

nix run github:thiagokokada/gh-gfm-preview
gh extension install thiagokokada/gh-gfm-preview

Upgrade:

gh extension upgrade markdown-preview

The usage:

Or this command will detect README file in the directory automatically.

Then access the local web server such as http://localhost:3333 with Chrome, Firefox, or Safari.

Available options:

    --dark-mode           force dark mode
    --disable-auto-open   disable auto opening your browser
    --disable-reload      disable live reloading
-h, --help                help for gh-gfm-preview
    --host string         hostname this server will bind (default "localhost")
    --light-mode          force light mode
    --markdown-mode       force "markdown" mode (rather than default "gfm")
-p, --port int            TCP port number of this server (default 3333)
    --verbose             show verbose output

Since the binary is static and it works offline, this is a good program to use to preview how a Markdown is looking in e.g.: neovim. For example, you can add this in your $HOME/.config/nvim/init.lua:

local function preview_markdown()
  local file = vim.fn.expand("%")
  local on_exit_cb = function(out)
    print("Markdown preview process exited with code:", out.code)
  end
  local process = vim.system(
    -- assuming that the extension were installed using gh
    -- the reason we are not using `gh gfm-preview` instead is because this
    -- can cause an issue where the gh process is killed but not the
    -- gh-gfm-preview, since the kill signal will not reach the child process
    {vim.fn.expand("$HOME/.local/share/gh/extensions/gh-gfm-preview/gh-gfm-preview"), file},
    on_exit_cb
  )

  vim.api.nvim_create_autocmd({ "BufUnload", "BufDelete" }, {
    buffer = vim.api.nvim_get_current_buf(),
    callback = function()
      process:kill("sigterm")
      -- timeout (in ms), will call SIGKILL upon timeout
      process:wait(500)
    end,
  })
end

-- create a shortcut only in Markdown files, mapped to `<Leader>P`
vim.api.nvim_create_autocmd({ "FileType" }, {
  pattern = { "markdown" },
  callback = function()
    vim.keymap.set("n", "<Leader>P", preview_markdown, {
      desc = "Markdown preview", buffer = true
    })
  end,
})

You can run the following command to (re-)generate assets:

And you can run the following command to build:

联系我们 contact @ memedata.com