适用于 UNIX 系统的 less-style markdown 查看器

发布于 2024-12-06 22:50:51 字数 313 浏览 1 评论 0 原文

我有一个 JavaScript 中的 Markdown 字符串,我想在 less (或者,我想是 more)风格的查看器中显示它(用粗体等)对于命令行。

例如,对于一个字符串,

"hello\n" + 
"_____\n" + 
"*world*!"

我希望弹出带有可滚动内容的输出,如下所示

你好

世界

这可能吗?如果可能的话,如何实现?

I have a Markdown string in JavaScript, and I'd like to display it (with bolding, etc) in a less (or, I suppose, more)-style viewer for the command line.

For example, with a string

"hello\n" + 
"_____\n" + 
"*world*!"

I would like to have output pop up with scrollable content that looks like

hello

world

Is this possible, and if so how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

我是男神闪亮亮 2024-12-13 22:50:51

Pandoc 可以将 Markdown 转换为 groff 手册页。

这(感谢 nenopera 的评论):

 pandoc -s -f markdown -t man foo.md | man -l -

应该可以解决问题。 -s 选项告诉它生成正确的页眉和页脚。

可能还有其他 markdown 到 *roff 转换器; Pandoc 恰好是我发现的第一个。

另一种替代方法是 markdown 命令(Debian 系统上的 apt-get install markdown),它将 Markdown 转换为 HTML。例如:(

markdown README.md | lynx -stdin

假设您有基于终端的 lynx Web 浏览器)。

或者(感谢 Danny 的建议)您可以执行以下操作:

markdown README.md > README.html && xdg-open README.html

其中 xdg-open (在某些系统上)在首选应用程序中打开指定的文件或 URL。这可能会在您首选的 GUI Web 浏览器中打开 README.html(这并不完全是“较少风格”,但可能很有用)。

Pandoc can convert Markdown to groff man pages.

This (thanks to nenopera's comment):

 pandoc -s -f markdown -t man foo.md | man -l -

should do the trick. The -s option tells it to generate proper headers and footers.

There may be other markdown-to-*roff converters out there; Pandoc just happens to be the first one I found.

Another alternative is the markdown command (apt-get install markdown on Debian systems), which converts Markdown to HTML. For example:

markdown README.md | lynx -stdin

(assuming you have the lynx terminal-based web browser).

Or (thanks to Danny's suggestion) you can do something like this:

markdown README.md > README.html && xdg-open README.html

where xdg-open (on some systems) opens the specified file or URL in the preferred application. This will probably open README.html in your preferred GUI web browser (which isn't exactly "less-style", but it might be useful).

瑾兮 2024-12-13 22:50:51

我试图在上面的评论中写下这个,但我无法正确格式化我的代码块。要编写“less 过滤器”,请尝试将以下内容保存为 ~/.lessfilter

#!/bin/sh

case "$1" in
    *.md)
        extension-handler "$1"
        pandoc -s -f markdown -t man "$1"|groff -T utf8 -man -
        ;;
    *)
        # We don't handle this format.
        exit 1
esac

# No further processing by lesspipe necessary
exit 0

然后,您可以输入 less FILENAME.md,它将被格式化就像手册页一样。

I tried to write this in a comment above, but I couldn't format my code block correctly. To write a 'less filter', try, for example, saving the following as ~/.lessfilter:

#!/bin/sh

case "$1" in
    *.md)
        extension-handler "$1"
        pandoc -s -f markdown -t man "$1"|groff -T utf8 -man -
        ;;
    *)
        # We don't handle this format.
        exit 1
esac

# No further processing by lesspipe necessary
exit 0

Then, you can type less FILENAME.md and it will be formatted like a manpage.

时光沙漏 2024-12-13 22:50:51

如果您喜欢颜色,那么也许这也值得检查:

terminal_markdown_viewer

< img src="https://raw.githubusercontent.com/axiros/terminal_markdown_viewer/master/samples/1.png" width="300">

它也可以在其他程序中直接使用,或者python 模块。

它有很多样式,比如超过 200 种 markdown 和可以组合的代码。

pic2

免责声明

  • 这还是 alpha 版本,可能仍然存在错误

  • 我是它的作者,也许有些人喜欢它;-)

If you are into colors then maybe this is worth checking as well:

terminal_markdown_viewer

It can be used straightforward also from within other programs, or python modules.

And it has a lot of styles, like over 200 for markdown and code which can be combined.

pic2

Disclaimer

  • It is pretty alpha there may be still bugs

  • I'm the author of it, maybe some people like it ;-)

陪我终i 2024-12-13 22:50:51

完全不同的替代方案是 mad。这是我刚刚发现的一个shell脚本。它非常容易安装,并且可以很好地在控制台中渲染 markdown。

A totally different alternative is mad. It is a shell script I've just discovered. It's very easy to install and it does render markdown in a console pretty well.

摇划花蜜的午后 2024-12-13 22:50:51

我根据 Keith 的回答编写了几个函数:

mdt() {
    markdown "$*" | lynx -stdin
}

mdb() {
    local TMPFILE=$(mktemp)
    markdown "$*" > $TMPFILE && ( xdg-open $TMPFILE > /dev/null 2>&1 & )
}

如果您使用的是 zsh,只需将这两个函数位于 ~/.zshrc 中,然后从终端调用它们,例如

mdt README.md
mdb README.md

“t”代表“终端”,“b”代表浏览器。

I wrote a couple functions based on Keith's answer:

mdt() {
    markdown "$*" | lynx -stdin
}

mdb() {
    local TMPFILE=$(mktemp)
    markdown "$*" > $TMPFILE && ( xdg-open $TMPFILE > /dev/null 2>&1 & )
}

If you're using zsh, just place those two functions in ~/.zshrc and then call them from your terminal like

mdt README.md
mdb README.md

"t" is for "terminal", "b" is for browser.

删除→记忆 2024-12-13 22:50:51

使用 OSX 我更喜欢使用此命令

brew install pandoc
pandoc -s -f markdown -t man README.md | groff -T utf8 -man | less

转换 markupm,使用 groff 格式化文档,并通过管道减少

信用:http://blog.metamat.com/blog/2013/01/09/previewing-markdown-files-from-the-terminal/

Using OSX I prefer to use this command

brew install pandoc
pandoc -s -f markdown -t man README.md | groff -T utf8 -man | less

Convert markupm, format document with groff, and pipe into less

credit: http://blog.metamatt.com/blog/2013/01/09/previewing-markdown-files-from-the-terminal/

盛装女皇 2024-12-13 22:50:51

这是封装函数的别名:

alias mdless='_mdless() { if [ -n "$1" ] ; then if [ -f "$1" ] ; then cat <(echo ".TH $1 7 `date --iso-8601` Dr.Beco Markdown") <(pandoc -t man $1) | groff -K utf8 -t -T utf8 -man 2>/dev/null | less ; fi ; fi ;}; _mdless '

说明

  • alias mdless='...' :为 mdless 创建别名
  • _mdless() {...}; :创建一个稍后调用的临时函数
  • _mdless :最后调用它(上面的函数)

在函数内部:

  • if [ -n "$1" ] ; then :如果第一个参数不为空,则...
  • if [ -f "$1" ] ; then :另外,如果文件存在并且是常规的,那么...
  • cat arg1 arg2 | groff ... :cat 将这两个参数连接到 groff 发送;论据是:
    • arg1: <(echo ".TH $1 7date --iso-8601Dr.Beco Markdown") :启动文件和 groff 将理解为页眉和页脚注释。这会替换 pandoc-s 键中的空标头。
    • arg2: <(pandoc -t man $1) :文件本身,通过 pandoc 过滤,输出 man 风格的文件$1
  • <代码>| groff -K utf8 -t -T utf8 -man 2>/dev/null :将生成的串联文件通过管道传输到 groff:
    • -K utf8groff 理解输入文件代码
    • -t 因此它可以正确显示文件中的表格
    • -T utf8 因此它以正确的格式输出
    • -man 因此它使用 MACRO 包以 man 格式输出文件
    • 2>/dev/null 忽略错误(毕竟,它是一个原始文件,在 man 中手动转换,我们不关心错误,只要我们可以在一种不太难看的格式)。
  • <代码>| less :最后,显示使用 less 对其进行分页的文件(我尝试通过使用 groffer 而不是 groff 来避免此管道>,但是 groffer 不如 less 强大,有些文件会挂起它或者根本不显示,所以,让它再通过一个管道,管它呢!

将其添加到您的~/.bash_aliases (或类似的)

This is an alias that encapsulates a function:

alias mdless='_mdless() { if [ -n "$1" ] ; then if [ -f "$1" ] ; then cat <(echo ".TH $1 7 `date --iso-8601` Dr.Beco Markdown") <(pandoc -t man $1) | groff -K utf8 -t -T utf8 -man 2>/dev/null | less ; fi ; fi ;}; _mdless '

Explanation

  • alias mdless='...' : creates an alias for mdless
  • _mdless() {...}; : creates a temporary function to be called afterwards
  • _mdless : at the end, call it (the function above)

Inside the function:

  • if [ -n "$1" ] ; then : if the first argument is not null then...
  • if [ -f "$1" ] ; then : also, if the file exists and is regular then...
  • cat arg1 arg2 | groff ... : cat sends this two arguments concatenated to groff; the arguments being:
    • arg1: <(echo ".TH $1 7date --iso-8601Dr.Beco Markdown") : something that starts the file and groff will understand as the header and footer notes. This substitutes the empty header from -s key on pandoc.
    • arg2: <(pandoc -t man $1) : the file itself, filtered by pandoc, outputing the man style of file $1
  • | groff -K utf8 -t -T utf8 -man 2>/dev/null : piping the resulting concatenated file to groff:
    • -K utf8 so groff understands the input file code
    • -t so it displays correctly tables in the file
    • -T utf8 so it output in the correct format
    • -man so it uses the MACRO package to outputs the file in man format
    • 2>/dev/null to ignore errors (after all, its a raw file being transformed in man by hand, we don't care the errors as long as we can see the file in a not-so-much-ugly format).
  • | less : finally, shows the file paginating it with less (I've tried to avoid this pipe by using groffer instead of groff, but groffer is not as robust as less and some files hangs it or do not show at all. So, let it go through one more pipe, what the heck!

Add it to your ~/.bash_aliases (or alike)

孤独难免 2024-12-13 22:50:51

我个人使用这个脚本:

#!/bin/bash
id=$(uuidgen | cut -c -8)
markdown $1 > /tmp/md-$id
google-chrome --app=file:///tmp/md-$id

它将 markdown 渲染为 HTML,将其放入 /tmp/md-... 中的文件中,并在没有 URI 栏等的 kiosk chrome 会话中打开它。只需将 md 文件作为参数传递或将其通过管道传递到 stdin 即可。需要 Markdown 和 Google Chrome。 Chromium 也应该可以工作,但你需要将最后一行替换为

chromium-browser --app=file:///tmp/md-$id

如果你想喜欢它,你可以使用一些 css 使其看起来不错,我编辑了脚本并使其使用来自 CDN 的 Bootstrap3 (过度杀伤)。

#!/bin/bash
id=$(uuidgen | cut -c -8)
markdown $1 > /tmp/md-$id
sed -i "1i <html><head><style>body{padding:24px;}</style><link rel=\"stylesheet\" type=\"text/css\" href=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css\"></head><body>" /tmp/md-$id
echo "</body>" >> /tmp/md-$id
google-chrome --app=file:///tmp/md-$id > /dev/null 2>&1 &

I personally use this script:

#!/bin/bash
id=$(uuidgen | cut -c -8)
markdown $1 > /tmp/md-$id
google-chrome --app=file:///tmp/md-$id

It renders the markdown into HTML, puts it into a file in /tmp/md-... and opens that in a kiosk chrome session with no URI bar etc.. You just pass the md file as an argument or pipe it into stdin. Requires markdown and Google Chrome. Chromium should also work but you need to replace the last line with

chromium-browser --app=file:///tmp/md-$id

If you wanna get fancy about it, you can use some css to make it look nice, I edited the script and made it use Bootstrap3 (overkill) from a CDN.

#!/bin/bash
id=$(uuidgen | cut -c -8)
markdown $1 > /tmp/md-$id
sed -i "1i <html><head><style>body{padding:24px;}</style><link rel=\"stylesheet\" type=\"text/css\" href=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css\"></head><body>" /tmp/md-$id
echo "</body>" >> /tmp/md-$id
google-chrome --app=file:///tmp/md-$id > /dev/null 2>&1 &
凡尘雨 2024-12-13 22:50:51

我也会在这里发布我的unix页面答案恕我

直言,这是一个被严重低估的命令行 Markdown 查看器是 markdown-cli

安装

npm install markdown-cli --global

使用

markdown-cli <file>

功能

可能没有引起太多注意,因为它缺少任何文档...
但据我通过一些 Markdown 文件示例可以看出,有些事情让我信服:

  • 更好地处理格式错误的文件(类似于atom、github 等;例如,当列表之前缺少空白行时)
  • 格式化更稳定 (列表中的粗体文本会破坏某些其他查看器中的子列表)
  • 的表格格式
  • 语法突出显示
  • 可以解析脚注链接以显示链接而不是脚注编号(不是每个人都可能想要这样)

正确

在标题或列表中 src="https://github.com/orzechow/markdown-cli/raw/master/example.png" alt="example.png">

缺点

我已经意识到以下问题

  • 代码块被展平(所有前导空格消失)
  • 列表前出现两个空行

I'll post my unix page answer here, too:

An IMHO heavily underestimated command line markdown viewer is the markdown-cli.

Installation

npm install markdown-cli --global

Usage

markdown-cli <file>

Features

Probably not noticed much, because it misses any documentation...
But as far as I could figure out by some example markdown files, some things that convinced me:

  • handles ill formatted files much better (similarly to atom, github, etc.; eg. when blank lines are missing before lists)
  • more stable with formatting in headers or lists (bold text in lists breaks sublists in some other viewers)
  • proper table formatting
  • syntax highlightning
  • resolves footnote links to show the link instead of the footnote number (not everyone might want this)

Screenshot

example.png

Drawbacks

I have realized the following issues

  • code blocks are flattened (all leading spaces disappear)
  • two blank lines appear before lists
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文