如何存储Markdown评论
我想使用 Markdown 作为我网站的评论系统,但我偶然发现了以下问题:我应该在数据库中存储什么 - Markdown 中的原始评论、HTML 中解析后的评论,还是两者都存储?
如果用户需要编辑他的评论,我需要 HTML 版本来查看和 Markdown 版本。如果我存储 Markdown 版本,我必须在运行时解析注释。如果我存储 HTML 版本,当用户需要编辑评论时,我需要将评论转换回 Markdown (我发现 Markdownify 为此,但它并不是完美的)。如果我存储两个版本,则使用的空间会增加一倍。
最好的选择是什么?另外,Stack Overflow 如何处理这个问题?
I want to use Markdown for my website's commenting system but I have stumbled upon the following problem: What should I store in the database - the original comment in Markdown, the parsed comment in HTML, or both?
I need the HTML version for viewing and the Markdown version if the user needs to edit his comment. If I store the Markdown version, I have to parse the comments at runtime. If I store the HTML version, I need to convert the comment back to Markdown when the user needs to edit it (I found Markdownify for this but it isn't flawless). If I store both versions, I'm doubling the used space.
What would be the best option? Also, how does Stack Overflow handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
存储两者。它违反了数据库规范化的规则,但我认为在这种情况下对于速度优化是值得的 - 解析大量文本是一个非常慢的操作。
你只需要存储它两次,但你可能需要提供它数千次,所以这是一个时空权衡。
Store both. It goes against the rules for database normalization, but I think it's worth it for the speed optimisation in this case - parsing large amounts of text is a very slow operation.
You only need to store it twice, but you might need to serve it thousands of times, so it's a space-time trade-off.
存储原始 Markdown 并在运行时解析。将转换后的版本存储在数据库中存在一些问题。
Store the original markdown and parse at runtime. There are a few problems with storing the converted version in the database.
只需在运行时将 Markdown 渲染为 HTML 即可。
如果您的网站遇到性能问题,Markdown 将是您最后要考虑调整的内容之一。即便如此,我怀疑它是否有意义。
只需看一下 SO 使用的实时 JavaScript 渲染器即可。速度很快。
编辑:
抱歉,我应该说得更清楚。我的意思是只用 PHP 渲染。你会省去很多麻烦——而且你可能还有更重要的事情要担心。
Just render the Markdown to HTML at runtime.
If your site runs into performance issues, the Markdown will be one of the last things you'll look into tweaking. And even then, I doubt it'll make sense.
Just take a look at the realtime JavaScript renderer that SO uses. It's fast.
Edit:
Sorry, I should've been more clear. I meant just render in PHP. You'll save yourself a lot of headache -- and you probably have more important things to worry about.