在 Emacs 缓冲区中渲染 Markdown

发布于 2024-09-12 17:25:04 字数 327 浏览 7 评论 0原文

是否可以使用 Emacs 自己的缓冲区文本格式化功能在 Emacs 缓冲区中呈现 Markdown?图形环境中的 Emacs 具有丰富的文本呈现功能(字体样式、颜色、链接甚至图像),因此这应该是完全可能的。有现成的实现吗?

请注意,我们的想法是让渲染的 Markdown 成为本机 Emacs 格式的文本,可以像 Emacs 中的任何其他文本一样进行导航和操作。因此,渲染到嵌入 Emacs 缓冲区中的图像的解决方案在这里并不理想。

另请注意,这不是编辑 Markdown 的模式,而是在 Emacs 缓冲区中呈现渲染的 Markdown 模式。为了可移植性,它最好是一个纯 Emacs Lisp 解决方案。

Is it possible to present Markdown rendered in an Emacs buffer using Emacs' own buffer text formatting capabilities? Emacs in graphical environments has rich text presentation capabilities (font styles, colors, links and even images) so it should be quite possible. Are there any existing implementations?

Note that the idea is to have the rendered Markdown be native Emacs formatted text that can be navigated and operated on as any other text in Emacs. Therefore solutions that render to an image that is embedded in an Emacs buffer are not desirable here.

Also note that this is not about a mode for editing Markdown, but for presenting rendered Markdown in an Emacs buffer. It should preferably be a pure Emacs Lisp solution for portability.

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

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

发布评论

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

评论(9

沉睡月亮 2024-09-19 17:25:05

根据您想要查看渲染文本的上下文,您只需调整 markdown 模式的字体锁定规则就可以非常接近您需要的内容。

Depending on the context where you'd like to see the rendered text, you might be able to get pretty close to what you need by just tweaking markdown-mode's font-lock rules.

指尖凝香 2024-09-19 17:25:05

除了 pretty-lambda (已经被多次建议)之外,您还可以查看 org-mode 及其语法高亮规则。特别是,org-hide-emphasis-markers变量或多或少可以做你想要的事情(参见例如org-do-emphasis-faces 隐藏标记字符)。

Apart from pretty-lambda (which has already been suggested multiple times), you could look into org-mode and its syntax highliting rules. In particular, the org-hide-emphasis-markers variable does more or less what you want (see e.g. how org-do-emphasis-faces hide markup characters).

债姬 2024-09-19 17:25:05

由于我不知道任何基于 emacs 的 Markdown 解析器,我不得不说你必须从头开始编写一个。也许这个问题可以给你一些提示。

如果您选择仅使用 Emacs,那么 Semantic 是一个出色的 API工作(它为您提供了词法分析器、解析器生成器和解析器;它已经存在十多年了,并且有文档!)。拥有语言解析器后,您必须为每种标记类型创建一些渲染函数。这样你就可以定制一切。

虽然这将是一次启发性的旅程,但可以肯定的是,我仍然选择在单独的后台进程上使用现有的 Markdown->html 转换器,然后使用 w3(m) 进行 emacs 预览(正如 Bozhidar 建议的那样)。它可以完成工作,而且做起来要简单得多。也没有重大的性能问题 - 您应该很少运行这个工具,这样您就可以轻松地节省一些额外的毫秒)。

一个混合的解决方案是让 Markdown 解析器直接生成 HTML,并在 w3(m) 缓冲区上预览它(它减轻了你肩上的渲染负担,你只需要将 markdown 音译为 html,这看起来很直接语义转发)。

As I don't know about any emacs-based Markdown parser, I'd have to say that you have to code one from scratch. Maybe this SO question can throw some pointers at you.

If you opt to go through the Emacs-only road, then Semantic is an excellent API for that job (it offers you a lexer, parser-generator, and parser; it's been around for more than a decade, and it has documentation!). After having the language parser, you'll have to make some rendering functions for each token type. And this way you can customize everything.

Although this would be an enlightening trip, for sure, I'd still opt for using an existing Markdown->html converter on a separate background process and then use w3(m) for emacs preview (as Bozhidar suggested). It gets the job done and it's much simpler to do. No major performance issues, neither - you should run this tool rather scarcely, so you can easily spare some extra milliseconds).

A mixed solution would be to make the Markdown parser generate HTML directly, and preview it on a w3(m) buffer (it takes away the rendering weight from your shoulders, you only need to transliterate the markdown into html, and that seems pretty straight forward with Semantic).

尤怨 2024-09-19 17:25:05

如果只是关于渲染,请按照 Bozhidar 的建议进行 Markdown 到 HTML 的转换,然后在 W3 缓冲区中显示 HTML。 markdown-mode 具有调用外部 Markdown 命令的代码以及一些好处。

但如果你真的想在 Emacs Lisp 中完成所有事情,你必须首先编写一个 Markdown 解析器。

If it is only about the rendering, go with Bozhidar's suggestion and do a Markdown to HTML conversion, then display the HTML in a W3 buffer. markdown-mode has code to call the external Markdown command with a few goodies.

But if you really want to do everything within Emacs Lisp, you'll have to write a Markdown parser first.

碍人泪离人颜 2024-09-19 17:25:05

我刚刚发现 markdown-mode 有一种叫做 markdown-视图模式 自 2.4 起< /a>.听起来这可能就是您正在寻找的!

I just found out that markdown-mode has something called markdown-view-mode since 2.4. It sounds like it might be what you're looking for!

琉璃梦幻 2024-09-19 17:25:05

我也是,我非常一直在寻找这样的东西。不过,我能找到的最好的不是 Emacs 解决方案,它是一个独立的伟大软件,名为 重新文本

Me too, I've been looking for something like this for a very long time. The best I could find, though, is not am Emacs solution, it is an independent great piece of software called ReText.

池木 2024-09-19 17:25:04

就我个人而言,我使用以下工作流程:

  • Cc Cc m 上运行,在当前缓冲区上运行 Markdown 并在另一个缓冲区中预览输出。
  • 移动到另一个缓冲区上的 html-mode (Mx html-mode)
  • 隐藏 html 标签以显示接近输出的内容 (Mx sgml-tags-invisible

然后每次你想刷新渲染时,只需在 Markdown 缓冲区上再次运行 Cc Cc m 即可。

但我承认到目前为止,对于 Markdown 编辑/预览来说,没有什么比 Textmate 及其 Markdown 预览面板更好的了。实际上,从个人角度来看,我唯一喜欢运行 Textmate 而不是 Emacs 的情况是当我想编辑 Markdown 文件时。然而,在 emacs 上获得相同质量的预览并不是那么困难,也许我应该研究一下。在我看来,这很简单:

  • 获取 Textmate 用于渲染预览的内部 css
  • 使用 w3 或 w3m 来使用此 css 预览 markdown 输出

Personally, I use the following workflow:

  • run on a C-c C-c m to run Mark­down on the cur­rent buffer and pre­view the out­put in an­other buffer.
  • move to html-mode on this other buffer (M-x html-mode)
  • hide the html tags to display something close to the output (M-x sgml-tags-invisible)

Then every time you want to refresh the rendering, simply run again C-c C-c m on the markdown buffer.

Yet I confess until now for Markdown editing/previewing, nothing beats for me Textmate and its markdown preview panel. Actually, from a personal perspective, the only case where I prefer to run Textmate rather than Emacs is when I want to edit markdown files. Yet the path to have the same quality of preview on emacs is not so difficult and probably I should investigate it. As I see it, it's simply:

  • get the internal css used by Textmate for rendering the preview
  • use w3 or w3m to preview the markdown output using this css
汹涌人海 2024-09-19 17:25:04

我想您可以使用 latex-preview 的源代码来获取灵感或pretty-lambda(一个更简单的软件)。

或者,您可以在后台进程中将 markdown 转换为 html 并预览 html。

在 Emacs 中一切皆有可能,但并非一切都能轻松实现:-)

I guess you can use the source code of latex-preview for inspiration or pretty-lambda(a much simpler piece of software).

Alternatively you can convert the markdown to html in a background process and preview the html.

Everything is possible in Emacs, but not everything is easily achieved :-)

熟人话多 2024-09-19 17:25:04

我的 .emacs 文件中有这个:

(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(markdown-header-delimiter-face ((t (:inherit font-lock-function-name-face :underline t :weight bold))) t)
 '(markdown-header-face-1 ((t (:inherit markdown-header-face :height 1.5))) t)
 '(markdown-header-face-2 ((t (:inherit markdown-header-face :height 1.3))) t)
 '(markdown-header-face-3 ((t (:inherit markdown-header-face :underline t :height 1.2))) t)
 '(markdown-header-face-4 ((t (:inherit markdown-header-face :underline t :height 1.1))) t)
 '(markdown-header-face-5 ((t (:inherit markdown-header-face :underline t))) t)
 '(markdown-header-face-6 ((t (:inherit markdown-header-face :underline t))) t))
(put 'set-goal-column 'disabled nil)

它放大了所有标题。 Markdown 模式本身会将加星号的文本和粗体双星号的文本显示为斜体,因此这将帮助您实现目标。但是,它不会使控制字符不可见。

如果您想要这样,您可能应该查看 pretty-lambda 示例(如 博日达尔·巴特索夫建议

I have this in my .emacs file:

(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(markdown-header-delimiter-face ((t (:inherit font-lock-function-name-face :underline t :weight bold))) t)
 '(markdown-header-face-1 ((t (:inherit markdown-header-face :height 1.5))) t)
 '(markdown-header-face-2 ((t (:inherit markdown-header-face :height 1.3))) t)
 '(markdown-header-face-3 ((t (:inherit markdown-header-face :underline t :height 1.2))) t)
 '(markdown-header-face-4 ((t (:inherit markdown-header-face :underline t :height 1.1))) t)
 '(markdown-header-face-5 ((t (:inherit markdown-header-face :underline t))) t)
 '(markdown-header-face-6 ((t (:inherit markdown-header-face :underline t))) t))
(put 'set-goal-column 'disabled nil)

which enlarges all the headlines. Markdown-mode itself will italicize starred text and boldface double-starred text, so this will get you there. However, it will not make the control characters invisible.

If you want that, you should probably look into pretty-lambda for examples (as Bozhidar Batsov suggested)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文