如何将 Web 应用程序从 HTML 切换到 Markdown?
我有一个 django 驱动的网站。 目前,所有文本字段都只是采用原始 HTML 的普通旧文本输入。 我没有漂亮的编辑器或任何东西。 我想开始使用 Markdown 和 WMD 让每个人的事情变得更容易。
我是否必须运行某种脚本来遍历数据库中的每个文本字段,以将所有 HTML 转换为 Markdown? 如果我通过 markdown 过滤器运行 HTML,它在另一端的结果会相同吗? WMD编辑器会从服务器读取HTML并将其转换为Markdown供用户编辑吗?
这里正确的做法是什么?
I've got a django-powered site. Currently, all of the textfields are just plain old text inputs that take raw HTML. I have no fancy editor or anything. I would like to start using Markdown and WMD to make things easier for everyone.
Do I have to run some sort of script to go over every text field in the database to convert all the HTML to markdown? If I run the HTML through the markdown filter, will it come out the same on the other side? Will the WMD editor read the HTML from the server and convert it to Markdown for the user to edit?
What's the correct course of action here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如我在你的其他问题中提到的,使用 Markdown 时的一个好模式是将文本存储在数据库中的 markdown 和 html 格式。 这样,您就不需要每次查看页面时都转换为 html,并且可以轻松地在后台编辑原始 Markdown。
例如,博客应用程序可能有
然后在模板中,您可以使用
{{body_html|safe}}
。我希望这能让您了解如何更改模型。 恐怕我没有关于如何处理遗留 html 数据的建议。
As I mentioned on your other question, a good pattern when using markdown is to store the text in markdown and html format in your database. That way, you don't need to convert to html every time the page is viewed, and it is easy to edit the original markdown in the admin.
For example, a blog app might have
Then in the template, you can use
{{body_html|safe}}
.I hope that gives you an idea of how you may want to alter your models. I'm afraid I don't have suggestions for how to handle the legacy html data.
WMD 不做 Html-to-markdown,反之亦然; 您可以使用 html2text 将 HTML 转换为 Markdown。 默认情况下,WMD 仅使用您的第一个文本区域作为 Markdown 编辑器,但您可以覆盖它并添加预览 - 只需按照 WMD 附带的简单示例中的说明进行操作即可。 无论如何,API 正在发生变化(值得称赞的是,作者对此毫不神秘),因为整个事情都转向了更真正的开源概念,所以现在不值得详细解释它,也不值得尝试任何花哨的东西! -)
WMD does not do Html-to-markdown, only viceversa; you can convert HTML to markdown with html2text. WMD by default only uses your first text area as a markdown editor, but you can override that as well as add previews -- just follow the instructions in the simple examples that come with WMD. The API is changing anyway (and, to his credit, the author makes no mystery of this) as the whole things moves to a more truly open-source concept, so it's not worth explaining it in detail right now, nor trying anything fancy!-)
显然,将旧的 HTML 转换为 Markdown 是完全没有必要的。 我将 django.contrib.markup.markdown 过滤器应用到我的模板中,并且遗留数据库记录中的 HTML 被直接传递。 非遗留记录中的 Markdown 也可以正确呈现。
当然,我的 Web 应用程序不允许用户修改这些字段,因此让 HTML 直接通过是可以的。 如果这是用户可编辑的字段,例如评论或维基,则此解决方案还不够。 您必须将安全参数传递给 Markdown 模板过滤器,这将删除所有 HTML,并且对于用 HTML 编写的旧帖子,需要进行一些 HTML 到 Markdown 的转换。
在这种情况下,另一个解决方案是编写一个新的模板过滤器来包装 Markdown 过滤器。 它将允许旧的 HTML 帖子通过,但将安全 Markdown 过滤器应用于非旧帖子。
Apparently converting the old HTML to markdown is completely unnecessary. I applied the django.contrib.markup.markdown filter to my templates, and the HTML in the legacy database records was passed straight through. Markdown in non-legacy records was also rendered correctly.
Granted, my web application does not allow users to be modifying these fields, so it is ok to let HTML pass straight through. If this was a user-editable field, like comments or a wiki, this solution would not suffice. You would have to pass the safe parameter to the markdown template filter, which would strip out all the HTML, and some HTML to markdown conversion would be necessary for legacy posts written in HTML.
Another solution, in that case, would be to write a new template filter that wrapped the markdown filter. It would allow old HTML posts to pass through, but apply the safe markdown filter to non-legacy posts.
使用已接受的答案时要小心。 默认情况下,标记文本不会删除不安全的标签。 恶意用户可以轻松插入 html,甚至可以插入脚本标签。 在模板中使用 {{body_html|safe}} 意味着执行用户的 html 或脚本。 自己尝试一下。
Be careful when using the accepted answer. By default, marking down your text doesn't take care of removing unsafe tags. A malicious user can easily insert html and he can even insert script tags. Using {{body_html|safe}} in the template would mean executing the user's html or script. Try it yourself.