适用于 UNIX 系统的 less-style markdown 查看器
我有一个 JavaScript 中的 Markdown 字符串,我想在 less
(或者,我想是 more
)风格的查看器中显示它(用粗体等)对于命令行。
例如,对于一个字符串,
"hello\n" +
"_____\n" +
"*world*!"
我希望弹出带有可滚动内容的输出,如下所示
你好
世界
这可能吗?如果可能的话,如何实现?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Pandoc 可以将 Markdown 转换为 groff 手册页。
这(感谢 nenopera 的评论):
应该可以解决问题。
-s
选项告诉它生成正确的页眉和页脚。可能还有其他 markdown 到 *roff 转换器; Pandoc 恰好是我发现的第一个。
另一种替代方法是
markdown
命令(Debian 系统上的apt-get install markdown
),它将 Markdown 转换为 HTML。例如:(假设您有基于终端的
lynx
Web 浏览器)。或者(感谢 Danny 的建议)您可以执行以下操作:
其中
xdg-open
(在某些系统上)在首选应用程序中打开指定的文件或 URL。这可能会在您首选的 GUI Web 浏览器中打开 README.html(这并不完全是“较少风格”,但可能很有用)。Pandoc can convert Markdown to groff man pages.
This (thanks to nenopera's comment):
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:(assuming you have the
lynx
terminal-based web browser).Or (thanks to Danny's suggestion) you can do something like this:
where
xdg-open
(on some systems) opens the specified file or URL in the preferred application. This will probably openREADME.html
in your preferred GUI web browser (which isn't exactly "less-style", but it might be useful).我试图在上面的评论中写下这个,但我无法正确格式化我的代码块。要编写“less 过滤器”,请尝试将以下内容保存为
~/.lessfilter
:然后,您可以输入
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
:Then, you can type
less FILENAME.md
and it will be formatted like a manpage.如果您喜欢颜色,那么也许这也值得检查:
terminal_markdown_viewer
< img src="https://raw.githubusercontent.com/axiros/terminal_markdown_viewer/master/samples/1.png" width="300">
它也可以在其他程序中直接使用,或者python 模块。
它有很多样式,比如超过 200 种 markdown 和可以组合的代码。
免责声明
这还是 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.
Disclaimer
It is pretty alpha there may be still bugs
I'm the author of it, maybe some people like it ;-)
完全不同的替代方案是 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.
我根据 Keith 的回答编写了几个函数:
如果您使用的是
zsh
,只需将这两个函数位于~/.zshrc
中,然后从终端调用它们,例如“t”代表“终端”,“b”代表浏览器。
I wrote a couple functions based on Keith's answer:
If you're using
zsh
, just place those two functions in~/.zshrc
and then call them from your terminal like"t" is for "terminal", "b" is for browser.
使用 OSX 我更喜欢使用此命令
转换 markupm,使用 groff 格式化文档,并通过管道减少
信用:http://blog.metamat.com/blog/2013/01/09/previewing-markdown-files-from-the-terminal/
Using OSX I prefer to use this command
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/
这是封装函数的别名:
说明
alias mdless='...'
:为mdless
创建别名_mdless() {...};
:创建一个稍后调用的临时函数_mdless
:最后调用它(上面的函数)在函数内部:
if [ -n "$1" ] ; then
:如果第一个参数不为空,则...if [ -f "$1" ] ; then
:另外,如果文件存在并且是常规的,那么...cat arg1 arg2 | groff
... :cat 将这两个参数连接到 groff 发送;论据是:<(echo ".TH $1 7
date --iso-8601Dr.Beco Markdown")
:启动文件和groff
将理解为页眉和页脚注释。这会替换pandoc
上-s
键中的空标头。<(pandoc -t man $1)
:文件本身,通过pandoc
过滤,输出man
风格的文件$1
-K utf8
让groff
理解输入文件代码-t
因此它可以正确显示文件中的表格-T utf8
因此它以正确的格式输出-man
因此它使用 MACRO 包以man
格式输出文件2>/dev/null
忽略错误(毕竟,它是一个原始文件,在 man 中手动转换,我们不关心错误,只要我们可以在一种不太难看的格式)。less
对其进行分页的文件(我尝试通过使用groffer
而不是groff
来避免此管道>,但是groffer
不如less
强大,有些文件会挂起它或者根本不显示,所以,让它再通过一个管道,管它呢!将其添加到您的
~/.bash_aliases
(或类似的)This is an alias that encapsulates a function:
Explanation
alias mdless='...'
: creates an alias formdless
_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:<(echo ".TH $1 7
date --iso-8601Dr.Beco Markdown")
: something that starts the file andgroff
will understand as the header and footer notes. This substitutes the empty header from-s
key onpandoc
.<(pandoc -t man $1)
: the file itself, filtered bypandoc
, outputing theman
style of file$1
| groff -K utf8 -t -T utf8 -man 2>/dev/null
: piping the resulting concatenated file togroff
:-K utf8
sogroff
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 inman
format2>/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 withless
(I've tried to avoid this pipe by usinggroffer
instead ofgroff
, butgroffer
is not as robust asless
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)我个人使用这个脚本:
它将 markdown 渲染为 HTML,将其放入
/tmp/md-...
中的文件中,并在没有 URI 栏等的 kiosk chrome 会话中打开它。只需将 md 文件作为参数传递或将其通过管道传递到 stdin 即可。需要 Markdown 和 Google Chrome。 Chromium 也应该可以工作,但你需要将最后一行替换为如果你想喜欢它,你可以使用一些 css 使其看起来不错,我编辑了脚本并使其使用来自 CDN 的 Bootstrap3 (过度杀伤)。
I personally use this script:
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 withIf 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.
我也会在这里发布我的unix页面答案:恕我
直言,这是一个被严重低估的命令行 Markdown 查看器是 markdown-cli。
安装
使用
功能
可能没有引起太多注意,因为它缺少任何文档...
但据我通过一些 Markdown 文件示例可以看出,有些事情让我信服:
正确
在标题或列表中 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
Usage
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:
Screenshot
Drawbacks
I have realized the following issues